📜 ⬆️ ⬇️

Statistics from the Android application on your GAE site



Android, Google Analytics, App Engine - products of one company. It would seem - what could be easier than to show data from one product to another? For example, Google Analytics statistics from an Android application on the App Engine website? It turned out that to make it really easy. Only not all steps are obvious.

In this article I will try to tell in steps how to achieve the desired. The part concerning the transfer of data from the Android application to Google Analytics is deliberately omitted, because does not contain any difficulties .

Step 1. Access


  1. In order for your GAE application (website) to use data from Google Analytics, you need to enable the Analytics API in the APIs section of your project in the Google Developers Console . If necessary, you can immediately set your limits (quotas) for using the Analytics API.
  2. In Google Analytics itself, in the User Management section, you must give Read and Analysis rights to the service user - your-app-id@appspot.gserviceaccount.com . It is under this user that we will request information from Google Analytics. Fortunately, data from Google Analytics (unlike some other Google services, for example, Google Play Services) can be obtained under the service user. Using a regular (non-service) user would require additional steps for oauth authorization.
  3. In order to be able to test your code locally, without uploading to Google servers, you need to create another service user. Detailed instructions on how to create and use it are given in response to StackOverflow. your-app-id@appspot.gserviceaccount.com works only in a combat environment.

')

Step 2. Libraries and Tools


  1. To simplify access to the Analytics API, download the Google API Python Client for GAE (if you use Python). Of course, we need exactly the version for GAE. Documentation for this library is here .
  2. To test queries to Google Analytics, you can use Google Analytics Query Explorer 2 .


Step 3. Data acquisition


Now, getting data from Google Analytics is quite simple -
from oauth2client.appengine import AppAssertionCredentials from apiclient.discovery import build from google.appengine.api import memcache import httplib2 credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/analytics.readonly') http = credentials.authorize(httplib2.Http(memcache)) service = build('analytics', 'v3', http=http) active_users = None response = service.data().ga().get( ids='ga:'+profile_id, #        start_date='2014-03-03', end_date='2014-03-09', metrics='ga:visitors').execute() if response.get('rows'): active_users = int(response.get('rows')[0][0]) 

This code allows you to find out the number of active users in the application for a specified period of time.

In my case, I request the necessary data from Google Analytics once a week, on Tuesdays (precisely on Tuesdays, because one additional day is set aside for transferring data from Android users to the Google Analytics server). Data is stored in NDB, and the site is displayed using Google Charts .

Source: https://habr.com/ru/post/230591/


All Articles