Statistics from the Android app on their GAE site

  • Tutorial


Android, Google Analytics, App Engine - products of one company. It would seem - what could be simpler than showing data from one product to another? For example, Google Analytics statistics from an Android app on an App Engine site? It turned out that to do this is really easy. Only not all steps are obvious.

In this article I will try to tell you step by step about how to achieve what you want. The part regarding the transfer of data from the Android application to Google Analytics has been deliberately skipped because does not contain any difficulties .

Step 1. Access


  1. In order for your GAE application (site) to be able to use data from Google Analytics, you need to enable the Analytics API in the Google Developers Console in the APIs section for your project . 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 need to give Reading 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 authentication.
  3. In order to be able to test your code locally, without uploading to a Google server, you need to create another service user. Detailed instructions for creating and using it are provided in the answer to StackOverflow. your-app-id@appspot.gserviceaccount.com only works 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. The documentation for this library is here .
  2. To test queries to Google Analytics, you can use Google Analytics Query Explorer 2 .


Step 3. Getting Data


Now getting data from Google Analytics is really easy -
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 given period of time.

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

Also popular now: