0
votes

So I am using the following code to get my Google Analytics data:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

def initialize_analyticsreporting():
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)
  analytics = build('analyticsreporting', 'v4', credentials=credentials)

  return analytics


def get_report(analytics):
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '2019-01-01', 'endDate': '2019-11-25'}],
         'metrics': [{'expression':i} for i in METRICS],
         'dimensions': [{'name':j} for j in DIMENSIONS],
         'pageSize':'100000'
        }]
      }
  ).execute()

analytics = initialize_analyticsreporting()
response = get_report(analytics)

However, it doesn't matter what do I specify as my start date and end date. I am always getting the same number of rows which is 2538 in case of session dimension. Moreover, its not even the full dataset. Pulling the very same dimension via Power BI's Google Analytics connector gives me 2845...

I have checked the documentation but it should work for dateRanges property. Maybe there is something about API I am missing?

Thank you in advance for any advice!

2
can you show the response for isgolden and sampling level? try and do smaller date range to avoid sampling say a week. - DaImTo

2 Answers

0
votes

Your code seems fine, try to add 'samplingLevel' attribute. Your code will be like this

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

def initialize_analyticsreporting():
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)
  analytics = build('analyticsreporting', 'v4', credentials=credentials)

  return analytics


def get_report(analytics):
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '2019-01-01', 'endDate': '2019-11-25'}],
          'samplingLevel':'LARGE',
         'metrics': [{'expression':i} for i in METRICS],
         'dimensions': [{'name':j} for j in DIMENSIONS],
         'pageSize':'100000'
        }]
      }
  ).execute()

analytics = initialize_analyticsreporting()
response = get_report(analytics)

I hope this helps.

0
votes

Is it possible that there might rows being returned where all the metric values equal 0?

As well as sampling, something else that might help could be adding in the "includeEmptyRows" parameter to True, which will return these rows, as the default is for the API to not return these.

As well as Ruben's answer, try this:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

def initialize_analyticsreporting():
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)
  analytics = build('analyticsreporting', 'v4', credentials=credentials)

  return analytics


def get_report(analytics):
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
         'viewId': VIEW_ID,
         'dateRanges': [{'startDate': '2019-01-01', 'endDate': '2019-11-25'}],
         'samplingLevel':'LARGE',
         "includeEmptyRows" : True,
         'metrics': [{'expression':i} for i in METRICS],
         'dimensions': [{'name':j} for j in DIMENSIONS],
         'pageSize':'100000'
        }]
      }
  ).execute()

analytics = initialize_analyticsreporting()
response = get_report(analytics)