Search code examples
androidgoogle-analyticsgoogle-analytics-api

Retrieve stats from Google Analytics for Android App


I have integrated Google Analytics into my Android app. The app is a photo printing app which contains a set of predefined themes that users can choose. However, is it possible to retrieve the stats from Google Analytics (e.g., the top 5 themes selected by user) using some api rather than using the Google Analytics console?


Solution

  • What you are looking for to retrieve the information is the Core Reporting API. Because the Google Analytics API requires all requests to be authenticated and your users are not authorized to access your Google Analytics account It is probably best to set up the API call on the server side using a service account here is an example of how to set up a python application to use a service account to access the API.

    But what should your query be?

    analytics.data().ga().get(
        ids='ga:' + profile_id,
        start_date='30daysAgo',
        end_date='today',
        metrics='ga:totalEvents',
        dimensions='ga:eventLabels').execute()
    

    Your application will need a way to access the results of the query from your own servers. You might also want to look into using the Google Analytics Super Proxy which solves a similar problem of allowing external users to access the results of an authenticated API request.