Google Analytics(分析)API-按名称检索自定义细分ID [英] Google Analytics API - retrieve Custom Segment Id by its name

查看:100
本文介绍了Google Analytics(分析)API-按名称检索自定义细分ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Analytics(分析)API检索20多个个人资料的数据,这样我就可以将它们全部合并为一组数据进行分析. (单独的配置文件用于具有相同内容的不同域)

我正在每个配置文件上使用自定义细分"来删除引荐垃圾邮件.我为自定义细分指定了相同的名称,例如引荐来源垃圾邮件".

因此,我想在通过API检索记录时使用这些自定义细分.但是遇到了挑战.

我认为我可以遍历细分受众群,并通过自定义细分受众群"名称(例如引荐来源垃圾邮件")获取每个配置文件的ID.我要做的第一件事是遍历细分,以了解它会给我带来什么.所以我做了以下事情:

# Authenticate and construct service.
service = get_service('analytics', 'v3', scope, key_file_location,
service_account_email)

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

for segment in segments.get('items', []):
  print 'Segment ID ' + segment.get('id') + " - " + segment.get('name')

但这不会检索自定义细分,而只会检索标准" Google细分,例如:

Segment ID -1 - All Sessions
Segment ID -2 - New Users
Segment ID -3 - Returning Users
Segment ID -4 - Paid Traffic
Segment ID -5 - Organic Traffic
Segment ID -6 - Search Traffic
Segment ID -7 - Direct Traffic
Segment ID -8 - Referral Traffic
Segment ID -9 - Sessions with Conversions
Segment ID -10 - Sessions with Transactions
etc etc

因此,我似乎无法访问自定义细分.

我认为这与我正在使用服务帐户并且没有访问自定义细分的权限有关?

我已授予所有配置文件API生成的开发人员gmail地址/用户读取权限.我想这不足以获取自定义细分.

我错过了什么吗?有没有一种方法可以使用服务帐户获得自定义细分?我需要使用服务帐户以外的身份进行身份验证吗?

解决方案

我找到了一种从api获取自定义细分详细信息的简便方法.

首先,您需要具有用户管理访问权限,管理员可能可以轻松地做到这一点.

管理细分受众群#设置细分受众群的可用性

  • 访问此链接,然后转到其中的设置细分的可用性"部分.
  • 应用协作权限"选项,如链接中所示.

申请许可后,只需从您的服务帐户和api运行与您在问题中提到的代码相同的代码,或按照下面的代码链接列出所有细分的详细信息.

列出用户细分

这次,您将可以获取自定义细分ID.自最近3-4天以来,我一直在尝试获取自定义细分的详细信息.希望对您有所帮助.

Using Google Analytics API to retrieve 20+ Profiles' data so i can combine all of them into one set of data for analysis. (separate profiles are for different domains that all have identical content)

I am using a Custom Segment on each Profile to remove referral spam. I have given the Custom Segments the same name eg 'referrer spam'.

So I want to use these Custom Segments when retrieving records with the API. However have run into a challenge.

I thought I could loop through the segments and get each Profile's Id by the Custom Segment name eg 'referrer spam'. The first thing I did was loop through the Segments to see what it would give me. So I did the following:

# Authenticate and construct service.
service = get_service('analytics', 'v3', scope, key_file_location,
service_account_email)

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

for segment in segments.get('items', []):
  print 'Segment ID ' + segment.get('id') + " - " + segment.get('name')

But this doesn't retrieve the Custom Segments, only the 'standard' Google segments eg things like:

Segment ID -1 - All Sessions
Segment ID -2 - New Users
Segment ID -3 - Returning Users
Segment ID -4 - Paid Traffic
Segment ID -5 - Organic Traffic
Segment ID -6 - Search Traffic
Segment ID -7 - Direct Traffic
Segment ID -8 - Referral Traffic
Segment ID -9 - Sessions with Conversions
Segment ID -10 - Sessions with Transactions
etc etc

So it looks like I am unable to access the Custom Segments.

I believe this has something to do with fact that I am using a Service Account and it doesn't have permissions to access the Custom Segments?

I have given the API generated developer gmail address/user read permission to all of the Profiles. I guess that isn't sufficient to get the Custom Segments.

Am I missing something? Is there a way to get Custom Segment using a Service Account? Do I need to authenticate with other than Service Account?

解决方案

I found an easy way to get custom segments details from api.

First, you need to have user management access, probably an administrator can easily do that.

Manage Segments#Set Segment availability

  • Visit this link and go to 'Set Segment availability' section in there.
  • Apply 'Collaborate permission' option as shown in the link.

After applying permission, just from your service account and api run the same code as you mention in you question or follow the below link of code to list all segments details.

Listing a User's Segments

This time you will be able to get custom segment ids. I was trying to get the custom segment details since last 3-4 days. Hope it helps.

这篇关于Google Analytics(分析)API-按名称检索自定义细分ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆