Automating Android App Review Monitoring with Google Cloud Functions and Slack Notifications
Monitoring user feedback is crucial for the continuous improvement of any mobile application. Google Play Store reviews provide direct insights from users, highlighting areas of success and opportunities for improvement. In this blog post, we'll walk through how to automate the retrieval of recent Google Play Store reviews for your Android app and post them to a Slack channel using a Google Cloud Function. This automation ensures that your team stays updated with user feedback in real-time, helping you react quickly to any issues or suggestions.
Before diving into the code, let's discuss the necessary requirements:
1. Google Play Developer API Access:
- To fetch reviews from the Google Play Store, you need access to the Google Play Developer API. This involves creating a service account in your Google Cloud project and granting it the appropriate permissions to access the Play Developer API.
2. Service Account JSON Key:
- The service account credentials are provided in a JSON file. This JSON file will be used by the Google Cloud Function to authenticate requests to the Play Developer API.
3. Slack Incoming Webhook URL:
- To send the reviews to Slack, you'll need to set up an incoming webhook in Slack. This webhook URL will be used to post messages to a specific Slack channel.
4. Google Cloud Function Setup:
- The code provided will be deployed as a Google Cloud Function. This function will be triggered on a scheduled basis, fetching the latest reviews and posting them to Slack.
Let's break down the code into its components to understand how it works.
const { google } = require('googleapis'); const axios = require('axios'); const { getConfig } = require('../config');
- `googleapis`: This library allows us to interact with Google APIs, including the Play Developer API.
- `axios`: A promise-based HTTP client used to send HTTP requests. Here, it's used to send messages to Slack.
- `getConfig`: A custom function (not fully shown here) that retrieves configuration settings such as the Google Play service account credentials.
const SLACK_WEBHOOK_URL = 'Url_Web_hook'; const PACKAGE_NAME = 'com.tigerspike.newlook';
-`SLACK_WEBHOOK_URL`: The webhook URL for posting messages to Slack.
- `PACKAGE_NAME`: The package name of your Android app on the Google Play Store.
async function fetchRecentReviews() { try { const now = new Date(); const twoDaysAgo = new Date(now.getTime() - (2 * 24 * 60 * 60 * 1000)); const twoDaysAgoTimestamp = Math.floor(twoDaysAgo.getTime() / 1000); const reviews = await fetchReviews(twoDaysAgoTimestamp); console.log('Fetched reviews from the last two days:', reviews); await sendToSlack(reviews); } catch (error) { console.error('An error occurred:', error); } }
- fetchRecentReviews : This function calculates the timestamp for two days ago and fetches all reviews from the last two days. It then sends these reviews to Slack.
async function fetchReviews(lastReviewTime) { const config = getConfig(); console.log(`problems on : ${config.playStore.googlePlayServiceAccount}`); let googlePlayServiceAccountJson; try { googlePlayServiceAccountJson = JSON.parse(config.playStore.googlePlayServiceAccount); } catch (error) { console.error('Error parsing googlePlayServiceAccount:', error); throw new Error('Error parsing googlePlayServiceAccount'); } const auth = new google.auth.GoogleAuth({ credentials: googlePlayServiceAccountJson, scopes: ['https://www.googleapis.com/auth/androidpublisher'], }); const authClient = await auth.getClient(); const playDeveloper = google.androidpublisher({ version: 'v3', auth: authClient }); const response = await playDeveloper.reviews.list({ packageName: PACKAGE_NAME, }); const reviews = response.data.reviews || []; return reviews.filter(review => review.comments[0].userComment.lastModified.seconds > lastReviewTime); }
- `fetchReviews`: This function handles the authentication using the Google Play service account JSON file and retrieves the reviews from the Google Play Store.
- `filter`: Reviews are filtered to include only those that were modified after the specified timestamp (two days ago).
async function sendToSlack(reviews) { for (const review of reviews) { const userComment = review.comments[0].userComment; const reviewDateTime = new Date(userComment.lastModified.seconds * 1000).toLocaleDateString('en-GB'); const message = { text: `*New review for Android NewLook App*\n` + `*Rating:* ${userComment.starRating} :star:\n` + `*Comment:* ${userComment.text}\n` + `*Date:* ${reviewDateTime}\n` }; await axios.post(SLACK_WEBHOOK_URL, message); } }
- `sendToSlack`: This function loops through the list of reviews and sends each one to Slack. The message includes the star rating, the comment, and the date of the review.
To deploy this code to Google Cloud Functions, follow these steps:
1. Create a Google Cloud Function:
- Go to the Google Cloud Console.
- Navigate to Cloud Functions and create a new function.
- Choose a trigger type (HTTP or Cloud Scheduler, see below).
2. Set Environment Variables:
- Ensure that the Google Play service account JSON and Slack webhook URL are accessible to the function, either as environment variables or through a configuration file.
3. Deploy the Function:
- Deploy the function, making sure to set the appropriate memory and timeout settings based on your needs.
To run this function periodically, you can set up a scheduled trigger using Google Cloud Scheduler:
1. Create a Cloud Scheduler Job:
- Go to the Cloud Scheduler in the Google Cloud Console.
- Create a new job and set the frequency (e.g., every 24 hours).
- Set the target to the HTTP-triggered Cloud Function.
2. Test the Setup:
- Manually trigger the Cloud Scheduler job to ensure everything is working as expected.
This automation setup lets your team stay updated with the latest Google Play Store reviews without manually checking the Play Store. By leveraging Google Cloud Functions, Google Play Developer API, and Slack, you can streamline the feedback loop and respond to user feedback more efficiently.
Feel free to extend and modify this setup to suit your specific requirements, such as filtering reviews by rating or automatically responding to certain types of feedback.