简体   繁体   English

从Google Analytics(分析)python API返回的“错误”配置文件ID

[英]'Wrong' profile ID being returned from Google Analytics python API

I am attempting to set up some code to analyze data from my Google Analytics account via the python API but have run into a problem with the profile_id that is being returned. 我正在尝试设置一些代码以通过python API分析来自我的Google Analytics(分析)帐户的数据,但遇到了要返回的profile_id问题。

I am following the tutorial here . 我在这里关注教程。 What I have done so far is to create I created a service account which provided an address of the form <projectId>-<uniqueId>@developer.gserviceaccount.com - this was then added to the Google Analytics account that I want to work with. 到目前为止,我要做的是创建一个我创建的服务帐户,该帐户提供一个格式为<projectId>-<uniqueId>@developer.gserviceaccount.com -然后将其添加到我要使用的Google Analytics(分析)帐户中。

I then followed the tutorial, and the code below succeeds and returns a profile ID. 然后,我按照该教程进行操作,下面的代码成功执行并返回一个配置文件ID。 The problem is that this profile ID isn't the same one that I can find by going to the dashboard for my Google Analytics account and looking by hand. 问题在于,该个人资料ID与我可以通过转到Google Analytics(分析)帐户的信息中心并手动查找来找到的ID不同。

If I use the string that is returned from the function below to query the API then I get just empty results (ie the code succeeded in querying some metric like users in the past 7 days, but it just returns that the answer is zero). 如果我使用下面的函数返回的字符串来查询API,那么我得到的结果只是空的(即,代码在过去7天中成功查询了某些指标(如用户),但返回的答案是零)。

If instead I just hard code the string I find on the dashboard then everything works fine and the correct results are returned. 相反,如果我只是硬编码在仪表板上找到的字符串,则一切正常,并返回正确的结果。 This obviously isn't very satisfying though and I'd like to understand what I'm doing wrong by trying to extract the profile ID via the python code. 不过,这显然不是很令人满意,我想通过尝试通过python代码提取配置文件ID来了解自己在做什么。

accounts = service.management().accounts().list().execute()

if accounts.get('items'):
  firstAccountId = accounts.get('items')[0].get('id')
  webproperties = service.management().webproperties().list(
    accountId=firstAccountId).execute()

  if webproperties.get('items'):
    firstWebpropertyId = webproperties.get('items')[0].get('id')
    profiles = service.management().profiles().list(
      accountId=firstAccountId,
      webPropertyId=firstWebpropertyId).execute()

    if profiles.get('items'):
      return profiles.get('items')[0].get('id')

return None

In case anyone comes across this problem looking for the solution, it turns out that in my case there had been several profiles created and attached to the account I was querying. 万一有人遇到这个问题寻找解决方案,事实证明,就我而言,已经创建了多个配置文件并将其附加到我所查询的帐户。 By taking the first profile I was picking up a now redundant profile. 通过获取第一个配置文件,我正在获取一个现在多余的配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM