I have a Dataframe (f) of inputs: Page Title (title_), Start date ('date_') and End date (date_date), and I want to iterate The Google analytics API get_report function on every row in the DataFrame, to get a new DataFrame of the results.
我有一个输入的数据帧(f):页面标题(title_),开始日期('date_')和结束日期(date_date),我想在DataFrame的每一行上迭代Google分析API get_report函数,以获得一个新的DataFrame结果。
Here is my Code:
这是我的代码:
for i in f.index:
lista = []
df = print_response(get_report(
config.service,
start_date=[f.loc[i]['date_']],
end_date=[f.loc[i]['date_date']],
view_id='xxxxxxxx',
metrics=[{'expression': 'ga:pageViews'}],
dimensionFilterClauses= [{
"filters": [{
"dimensionName": "ga:pageTitle",
"expressions": [f.loc[i]['title_']]}]
}],
dimensions=[{"name": "ga:pageTitle"}]))
lista.append(df)
time.sleep(2)
print(lista)
I get the error:
我收到错误:
TypeError: Object of type 'date' is not JSON serializable
TypeError:“date”类型的对象不是JSON可序列化的
What is wrong with my code, and how to structure the output as a DataFrame?
我的代码有什么问题,以及如何将输出结构化为DataFrame?
[update] That's my get_report function:
[更新]那是我的get_report函数:
def get_report(analytics, start_date, end_date, view_id, metrics, dimensions, dimensionFilterClauses):
return analytics.reports().batchGet(
body={
"reportRequests":[
{
"viewId": view_id,
"dateRanges":[{"startDate":start_date,"endDate":end_date}],
"metrics":metrics,
"dimensions": dimensions,
"dimensionFilterClauses": dimensionFilterClauses,
}]
}).execute()
1 个解决方案
#1
0
Have you tried following the quick start
您是否尝试过快速启动
def print_response(response):
"""Parses and prints the Analytics Reporting API V4 response.
Args:
response: An Analytics Reporting API V4 response.
"""
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
for row in report.get('data', {}).get('rows', []):
dimensions = row.get('dimensions', [])
dateRangeValues = row.get('metrics', [])
for header, dimension in zip(dimensionHeaders, dimensions):
print header + ': ' + dimension
for i, values in enumerate(dateRangeValues):
print 'Date range: ' + str(i)
for metricHeader, value in zip(metricHeaders, values.get('values')):
print metricHeader.get('name') + ': ' + value
#1
0
Have you tried following the quick start
您是否尝试过快速启动
def print_response(response):
"""Parses and prints the Analytics Reporting API V4 response.
Args:
response: An Analytics Reporting API V4 response.
"""
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
for row in report.get('data', {}).get('rows', []):
dimensions = row.get('dimensions', [])
dateRangeValues = row.get('metrics', [])
for header, dimension in zip(dimensionHeaders, dimensions):
print header + ': ' + dimension
for i, values in enumerate(dateRangeValues):
print 'Date range: ' + str(i)
for metricHeader, value in zip(metricHeaders, values.get('values')):
print metricHeader.get('name') + ': ' + value