Search code examples
pythongoogle-analyticsgoogle-analytics-apigoogle-api-python-client

How to mine event data from all the pages in Google Analytics using python?


The code i'm using is given below , the problem is this is returning only maximum of 1000 rows. What should i add in this code in order to get the whole data?

def get_report(analytics):
  """Queries the Analytics Reporting API V4.

  Args:
    analytics: An authorized Analytics Reporting API V4 service object.
  Returns:
    The Analytics Reporting API V4 response.
  """
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': start , 'endDate': end }],
          'metrics': [{'expression': 'ga:totalEvents'}], 
          'dimensions': [{ 'name': 'ga:eventLabel' }],
          'filtersExpression': 'ga:eventLabel=~C_NOTI_TRAIL*' 


        }]
      }
  ).execute()

Solution

  • Core Reporting API provides paging fields in a response object so you can access the next report page based on response token https://developers.google.com/analytics/devguides/reporting/core/v4/basics#pagination

    Also if you're sure that your report wouldn't exceed 10 000 rows you may manually set the limit value with your request with pageSize filed:

    body={
            'reportRequests': [
            {
              'viewId': VIEW_ID,
              'pageSize': 10000,
              'dateRanges': [{'startDate': start , 'endDate': end }],
              'metrics': [{'expression': 'ga:totalEvents'}], 
              'dimensions': [{ 'name': 'ga:eventLabel' }],
               ...
          }
    

    Make sure to check the description of pageSize and pageToken in the docs: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#ReportRequest