简体   繁体   中英

Passing a variable to a batchGet() request - Google Analytics Reporting API v4 / Python / loop

I'm building a Python project to pull down transaction level Google Analytics data for a day at a time. When I run the api for the month, the figures don't tie back to the GA UI. Therefore, I've tried creating a loop to run through each day of the month at a time and run the report. I'll then merge the results at the end.

Here is a snippet of the code:

`start_date = date(2019, 9, 1)
end_date = date(2019, 9, 30)
dateX = date(2019, 9, 1)

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
      yield start_date + timedelta(n)

for single_date in daterange(start_date, end_date):
  dateX = single_date


  def get_report(analytics):
    return analytics.reports().batchGet(
        body={
          'reportRequests': [
          {
            'viewId': VIEW_ID,
            'dateRanges': [{'startDate': "'" + str(dateX) + "'", 'endDate': "'" + str(dateX) + "'"}],
            'metrics': [{'expression': 'ga:Transactions'}],
            'dimensions': [{"name": "ga:transactionId"},{"name": "ga:sourceMedium"},
            {"name": "ga:keyword"},{"name": "ga:deviceCategory"},{"name": "ga:campaign"},{"name": "ga:dateHourMinute"}],
            'samplingLevel': 'LARGE',
            "pageSize": 100000
          }]
        }
  ).execute()`

The issue I'm having is passing a date parameter to the batchGet() request. Although the value of dateX is in the correct format (YYYY-MM-DD) I get the following error:

googleapiclient.errors.HttpError: https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned "Invalid Date specified: '2019-09-01'"

dateX

If I ran this with hardcoded dates like '2019-09-01' ect, this works. Any help would be extremely appreciated.

Found a solution.

I used the isoformat() function to convert the date to a serialised format. I didn't need to convert the date to a string.

`def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
      yield start_date + timedelta(n)

for single_date in daterange(start_date, end_date):


  def get_report(analytics):
    return analytics.reports().batchGet(
        body={
          'reportRequests': [
          {
            'viewId': VIEW_ID,
            'dateRanges': [{'startDate': single_date.isoformat(), 'endDate': single_date.isoformat()}],
            'metrics': [{'expression': 'ga:Transactions'}],
            'dimensions': [{"name": "ga:transactionId"},{"name": "ga:sourceMedium"},
            {"name": "ga:keyword"},{"name": "ga:deviceCategory"},{"name": "ga:campaign"},{"name": "ga:dateHourMinute"}],
            'samplingLevel': 'LARGE',
            "pageSize": 100000
          }]
        }
  ).execute()`

The first would work if you remove adding the extra "'" as it is unnecessary, adding extra 's. The error is saying the date is being sent as ''2019-09-01'' rather than a string '2019-01-01'.

eg.

def get_report(analytics):
    return analytics.reports().batchGet(
        body={
              'reportRequests': [
              {
                'viewId': VIEW_ID,
                'dateRanges': [{'startDate':  str(dateX) , 'endDate':  str(dateX)}],
                'metrics': [{'expression': 'ga:Transactions'}],
                'dimensions': [{"name": "ga:transactionId"},{"name": "ga:sourceMedium"},
                {"name": "ga:keyword"},{"name": "ga:deviceCategory"},{"name": "ga:campaign"},{"name": "ga:dateHourMinute"}],
                'samplingLevel': 'LARGE',
                "pageSize": 100000
              }]
            }

Why not just parse the dates using datetime.strftime?

def get_report(analytics):
    return analytics.reports().batchGet(
        body={
              'reportRequests': [
              {
                'viewId': VIEW_ID,
                'dateRanges': [{'startDate':  datetime.strftime(dateX, "%Y-%m-%d") , 'endDate':  datetime.strftime(dateX, "%Y-%m-%d")}],
                'metrics': [{'expression': 'ga:Transactions'}],
                'dimensions': [{"name": "ga:transactionId"},{"name": "ga:sourceMedium"},
                {"name": "ga:keyword"},{"name": "ga:deviceCategory"},{"name": "ga:campaign"},{"name": "ga:dateHourMinute"}],
                'samplingLevel': 'LARGE',
                "pageSize": 100000
              }]
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM