def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/adsense.readonly') ad_client_id = flags.ad_client_id try: # Let the user pick account if more than one. account_id = get_account_id(service) # Retrieve report in pages and display data as we receive it. start_index = 0 rows_to_obtain = MAX_PAGE_SIZE result_all_pages = None while True: result = service.reports().generate( accountId=account_id, startDate='today-2y', endDate='today', filter=['AD_CLIENT_ID==' + ad_client_id], metric=[ 'PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE', 'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK', 'AD_REQUESTS_RPM', 'EARNINGS' ], dimension=['DATE'], sort=['+DATE'], startIndex=start_index, maxResults=rows_to_obtain).execute() if result_all_pages is None: result_all_pages = result else: result_all_pages['rows'].extend(result['rows']) start_index += len(result['rows']) # Check to see if we're going to go above the limit and get as many # results as we can. if start_index + MAX_PAGE_SIZE > ROW_LIMIT: rows_to_obtain = ROW_LIMIT - start_index if rows_to_obtain <= 0: break if start_index >= int(result['totalMatchedRows']): break print_result(fill_date_gaps(result_all_pages)) except client.AccessTokenRefreshError: print( 'The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/adsense.readonly') ad_client_id = flags.ad_client_id try: # Let the user pick account if more than one. account_id = get_account_id(service) # Retrieve report in pages and display data as we receive it. start_index = 0 rows_to_obtain = MAX_PAGE_SIZE result_all_pages = None while True: result = service.reports().generate( accountId=account_id, startDate='today-2y', endDate='today', filter=['AD_CLIENT_ID==' + ad_client_id], metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE', 'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK', 'AD_REQUESTS_RPM', 'EARNINGS'], dimension=['DATE'], sort=['+DATE'], startIndex=start_index, maxResults=rows_to_obtain).execute() if result_all_pages is None: result_all_pages = result else: result_all_pages['rows'].extend(result['rows']) start_index += len(result['rows']) # Check to see if we're going to go above the limit and get as many # results as we can. if start_index + MAX_PAGE_SIZE > ROW_LIMIT: rows_to_obtain = ROW_LIMIT - start_index if rows_to_obtain <= 0: break if start_index >= int(result['totalMatchedRows']): break print_result(fill_date_gaps(result_all_pages)) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'adsense', 'v1.4', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/adsense.readonly') # Process flags and read their values. saved_report_id = flags.report_id try: # Let the user pick account if more than one. account_id = get_account_id(service) # Retrieve report. if saved_report_id: result = service.accounts().reports().saved().generate( accountId=account_id, savedReportId=saved_report_id).execute() else: result = service.accounts().reports().generate( accountId=account_id, startDate='today-1y', endDate='today', metric=['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE', 'CLICKS', 'AD_REQUESTS_CTR', 'COST_PER_CLICK', 'AD_REQUESTS_RPM', 'EARNINGS'], dimension=['DATE', 'MONTH', 'WEEK'], sort=['+DATE']).execute() result = fill_date_gaps(result) # Display headers. for header in result['headers']: print '%25s' % header['name'], print # Display results. for row in result['rows']: for column in row: print '%25s' % column, print # Display date range. print 'Report from %s to %s.' % (result['startDate'], result['endDate']) print except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize')
def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, "adsense", "v1.4", __doc__, __file__, parents=[argparser], scope="https://www.googleapis.com/auth/adsense.readonly", ) # Process flags and read their values. saved_report_id = flags.report_id try: # Let the user pick account if more than one. account_id = get_account_id(service) # Retrieve report. if saved_report_id: result = ( service.accounts() .reports() .saved() .generate(accountId=account_id, savedReportId=saved_report_id) .execute() ) else: result = ( service.accounts() .reports() .generate( accountId=account_id, startDate="today-1y", endDate="today", metric=[ "PAGE_VIEWS", "AD_REQUESTS", "AD_REQUESTS_COVERAGE", "CLICKS", "AD_REQUESTS_CTR", "COST_PER_CLICK", "AD_REQUESTS_RPM", "EARNINGS", ], dimension=["DATE", "MONTH", "WEEK"], sort=["+DATE"], ) .execute() ) result = fill_date_gaps(result) # Display headers. for header in result["headers"]: print "%25s" % header["name"], print # Display results. for row in result["rows"]: for column in row: print "%25s" % column, print # Display date range. print "Report from %s to %s." % (result["startDate"], result["endDate"]) print except client.AccessTokenRefreshError: print ("The credentials have been revoked or expired, please re-run the " "application to re-authorize")