google-api-python-client icon indicating copy to clipboard operation
google-api-python-client copied to clipboard

Issue with Python Library

Open AhmadMaherIsmail opened this issue 2 years ago • 0 comments

I am getting an invalid argument response, even though it was working fine without changing the code Here is a screenshot for reference: image Also, I was getting an empty report when running the code when it was working, can someone help with knowing why? I am not able to add filters neither am I able to generate a report Here is the code: `def main(argv): profile_id = input("Please enter your profile ID: ") name = "report 1" floodlight_ids = [input("Enter a list of floodlight IDs separated by commas: ").split(',')]

Retrieve command line arguments.

flags = dfareporting_utils.get_arguments(argv, doc,)

Authenticate and construct service.

service = dfareporting_utils.setup(flags)

try: # 1. Create a report resource. report = create_report_resource(name, floodlight_ids, profile_id)

# 2. Define the report criteria.
report = define_report_criteria(floodlight_ids, report)

# 3. (optional) Look up compatible fields.
report = find_compatible_fields(service, profile_id, report)

# 4. Add dimension filters to the report criteria.
report = add_dimension_filters(service, profile_id, report)

# 5. Save the report resource.
report = insert_report_resource(service, profile_id, report)

# 6. Download the report.
# dfareporting_utils.download_file(service, report['id'], report['fileName'])

except client.AccessTokenRefreshError: print('The credentials have been revoked or expired, please re-run the application to re-authorize')

def create_report_resource(name, floodlight_ids, profile_id): """Creates a report resource.""" report = { # Set the required fields "name" and "type". 'name': name, 'type': 'STANDARD', 'floodlight_ids' : floodlight_ids, 'profileId': profile_id , # Set optional fields. 'fileName': name, 'format': 'EXCEL' }

print ('Creating %s report resource with name "%s".' % (report['type'], report['name']))

return report

def define_report_criteria(floodlight_ids, report): """Defines a criteria for the report."""

Define a date range to report on. This example uses explicit start and end

dates to mimic the "LAST_30_DAYS" relative date range.

end_date = date.today() start_date = end_date - timedelta(days=7)

Create a report criteria.

criteria = { 'dateRange': { 'startDate': start_date.strftime('%Y-%m-%d'), 'endDate': end_date.strftime('%Y-%m-%d') }, "activities": { "filters": [{ "id": floodlight_ids, }], "activity": "Show each Activity", "metricNames": ['totalConversions'], }, 'metricNames': ['totalConversions'], }

Add the criteria to the report resource.

report['criteria'] = criteria

print ('\nAdded report criteria:\n%s' % criteria)

return report

def find_compatible_fields(service, profile_id, report): """Finds and adds a compatible dimension/metric to the report.""" fields = service.reports().compatibleFields().query(profileId=profile_id, body=report).execute()

report_fields = fields['reportCompatibleFields']

if report_fields['dimensions']: # Add a compatible dimension to the report. report_fields['dimensions'].append({'name': report_fields['dimensions'][0]['name']}) elif report_fields['metrics']: # Add a compatible metric to the report. report['criteria']['metricNames'].append(report_fields['metrics'][0]['name'])

print('\nUpdated report criteria (with compatible fields):\n%s' % report_fields['dimensions'])

return report

def add_dimension_filters(service, profile_id, report): end_date = date.today() start_date = end_date - timedelta(days=7) """Finds and adds a valid dimension filter to the report."""

Query advertiser dimension values for report run dates.

request = { 'dimensionName': 'advertiser', 'endDate': end_date.strftime('%Y-%m-%d'), 'startDate': start_date.strftime('%Y-%m-%d') }

values = service.dimensionValues().query(profileId=profile_id, body=request).execute()

if values['items']: # Add a value as a filter to the report criteria. report['criteria']['dimensionFilters'] = [values['items'][0]]

print('\nUpdated report criteria (with valid dimension filters):\n%s' % report['criteria'])

return report

def insert_report_resource(service, profile_id, report): """Inserts the report.""" inserted_report = service.reports().insert( profileId=profile_id, body=report).execute()

print('\nSuccessfully inserted new report with ID %s.' % inserted_report['id'])

return inserted_report

if name == 'main': main(sys.argv)` Thank you in advance

AhmadMaherIsmail avatar Mar 07 '23 13:03 AhmadMaherIsmail