Google Analytics API - retrieve Custom Segment Id

2019-01-25 22:30发布

问题:

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?

回答1:

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.



回答2:

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.