Pandas google analytics API - 如何切换帐户?
Pandas google analytics API - how to switch account?
我有多个网站的 Google 分析帐户 link。
我在 http://blog.yhathq.com/posts/pandas-google-analytics.html 之后为 pandas 设置了 GA api
但是我不明白如何从一个网站切换到另一个网站。
我正在通过 python 连接到 GA,使用:
temp_df = ga.read_ga(metrics,
dimensions=dimensions,
start_date=start_date,
end_date=end_date,
index_col=0,
filters=filters,
start_index=start_index
)
但是如何更改主机?
您需要添加 account_id
、property_id
和 profile_id
参数:
temp_df = ga.read_ga(metrics,
dimensions=dimensions,
start_date=start_date,
end_date=end_date,
index_col=0,
filters=filters,
account_id=account,
property_id=property,
profile_id=profile,
start_index=start_index)
可以在此处找到更多文档:http://pandas.pydata.org/pandas-docs/stable/remote_data.html#remote-data-ga
我发现将我的所有帐户信息打包到一个单独的 JSON 文件中很有用:
{
"site1": {
"acct": "123456",
"prop": "UA-123456-1",
"view": "098345983"
},
"site2": {
"acct": "987654",
"prop": "UA-987654-1",
"view": "398475987"
},
"site3": {
"acct": "456789",
"prop": "UA-456789-1",
"view": "938745876"
}
}
然后,像这样将这些帐户导入我的脚本:
import json
# p as path, gc as google analytics credentials
with open('/Path/To/Your/JSON/Goes/Here/google_analytics_accounts.json') as p:
gc = json.load(p)
p.close()
这样我就可以在读取 Google Analytics 到 DataFrame 时使用我需要的任何凭据:
temp_df = ga.read_ga(metrics,
dimensions=dimensions,
start_date=start_date,
end_date=end_date,
index_col=0,
filters=filters,
account_id=gc['site1']['acct'],
property_id=gc['site1']['prop'],
profile_id=gc['site1']['view'],
start_index=start_index)
我有多个网站的 Google 分析帐户 link。 我在 http://blog.yhathq.com/posts/pandas-google-analytics.html 之后为 pandas 设置了 GA api 但是我不明白如何从一个网站切换到另一个网站。
我正在通过 python 连接到 GA,使用:
temp_df = ga.read_ga(metrics,
dimensions=dimensions,
start_date=start_date,
end_date=end_date,
index_col=0,
filters=filters,
start_index=start_index
)
但是如何更改主机?
您需要添加 account_id
、property_id
和 profile_id
参数:
temp_df = ga.read_ga(metrics,
dimensions=dimensions,
start_date=start_date,
end_date=end_date,
index_col=0,
filters=filters,
account_id=account,
property_id=property,
profile_id=profile,
start_index=start_index)
可以在此处找到更多文档:http://pandas.pydata.org/pandas-docs/stable/remote_data.html#remote-data-ga
我发现将我的所有帐户信息打包到一个单独的 JSON 文件中很有用:
{
"site1": {
"acct": "123456",
"prop": "UA-123456-1",
"view": "098345983"
},
"site2": {
"acct": "987654",
"prop": "UA-987654-1",
"view": "398475987"
},
"site3": {
"acct": "456789",
"prop": "UA-456789-1",
"view": "938745876"
}
}
然后,像这样将这些帐户导入我的脚本:
import json
# p as path, gc as google analytics credentials
with open('/Path/To/Your/JSON/Goes/Here/google_analytics_accounts.json') as p:
gc = json.load(p)
p.close()
这样我就可以在读取 Google Analytics 到 DataFrame 时使用我需要的任何凭据:
temp_df = ga.read_ga(metrics,
dimensions=dimensions,
start_date=start_date,
end_date=end_date,
index_col=0,
filters=filters,
account_id=gc['site1']['acct'],
property_id=gc['site1']['prop'],
profile_id=gc['site1']['view'],
start_index=start_index)