熊猫谷歌分析API - 如何切换帐户?

时间:2015-02-02 12:27:53

标签: python api pandas google-analytics google-analytics-api

我有一个指向多个网站的Google Analytics帐户链接。 我在http://blog.yhathq.com/posts/pandas-google-analytics.html之后为大熊猫设置了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
                )

但我怎样才能更改主机?

1 个答案:

答案 0 :(得分:0)

您需要添加account_idproperty_idprofile_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)
相关问题