Google Analytics API - retrieve Custom Segment Id by its name

大兔子大兔子 提交于 2019-11-29 08:13:55

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.

The answer is that the Analytics Core Reporting API doesn't have access to the custom segments. They can only be accessed by the Analytics Management API as referenced here:

Retrieve a user's Custom Segments to apply them to Core Reporting API queries. https://developers.google.com/analytics/devguides/config/mgmt/v3/

So looks like I will have to make call to Analytics Management API first to get list of custom segments that I want, and then use that list in Analytics Core Reporting API call to get data for just those segments.

https://developers.google.com/analytics/devguides/reporting/core/v3/reference#segment

Alternatively, I can forget about using the custom segments and instead retrieve Session Hostname and Source in Analytics Core Reporting API call data, so that I can do same filter in the query or in report as my custom segment. Drawback is that the referral spam is about 80% + of Google Analytics data so I would be retrieving more data than i need on regular basis.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!