【发布时间】:2019-09-25 20:47:54
【问题描述】:
def get_first_profile_id(service):
# Use the Analytics service object to get the first profile id.
# Get a list of all Google Analytics accounts for this user
accounts = service.management().accounts().list().execute()
if accounts.get('items'):
# Get the first Google Analytics account.
account = accounts.get('items')[0].get('id')
# Get a list of all the properties for the first account.
properties = service.management().webproperties().list(
accountId=account).execute()
if properties.get('items'):
# Get the first property id.
property = properties.get('items')[0].get('id')
# Get a list of all views (profiles) for the first property.
profiles = service.management().profiles().list(
accountId=account,
webPropertyId=property).execute()
if profiles.get('items'):
# return the first view (profile) id.
return profiles.get('items')[0].get('id')
return None
大家好,我正在使用 Google Analytics API 通过 Python 提取数据。以上是谷歌提供的示例代码。
我想使用循环从每个帐户下的每个属性中提取每个“视图(配置文件)ID”。但是,上面的代码只提取了第一个帐户的第一个属性的第一个视图(配置文件)ID。
你能分享一些建议吗?感谢您的帮助!
【问题讨论】:
标签: python python-3.x api loops for-loop