Google Analytics API - retrieve Custom Segment Id by its name - google-analytics-api

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.

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.

Related

AWS API Gateway with API keys - get usage insights

I have my public API running on AWS API Gateway, and now I want to add API keys to it, basically to see "who is using which endpoint and how many time per month/week/whatever". My API already has user management logic, with its own users db table.
The part I need help with is the analytics part - say I have this up and running, I need to be able to extract some sort of report saying, for each user (by name), how many calls were made to each endpoint. So far, the closest I've gotten was https://stackoverflow.com/a/52361117/1514576 which gets me the info I need by API key. The part that I'm missing is how I could potentially cross-reference this data with my "users" database table.
I was hoping one of you had faced a similar problem and could share how you handled it.

Facebook marketing API Does not select the correct pixel conversion event

I'v built an app that creates ads using the marketing API. When it creates the adset it sets a specific pixel and a page view event.
All our users get the correct pixel and the correct event except for one user.
Here is the object from the post request that is being sent to Facebook where we can see that they are equal
User one
"promoted_object":"{\"pixel_id\":xxxxxxxxxx63,\"custom_event_type\":\"CONTENT_VIEW\",\"pixel_rule\":\"{\\\"event\\\":{\\\"eq\\\":\\\"PageView\\\"}}\"
User two
"promoted_object":"{\"pixel_id\":xxxxxxxxxx63,\"custom_event_type\":\"CONTENT_VIEW\",\"pixel_rule\":\"{\\\"event\\\":{\\\"eq\\\":\\\"PageView\\\"}}\"
Both get the correct pixel with the correct id but one gets no conversion event, and say Missing conversion event.
Both ad accounts have full access to the pixel under the business settings in ads manager.
Here is an image that describes the issue.
Anyone have any ideas here?
make sure the conversion event is defined in that account.
The conversion event is usually set on Campaign Level and supplied when you create a campaign via API.
As you're trying to use a custom conversion it might be that you've created it for one account but didn't create it for another. That's why you don't see it in you campaign setup view. You can create it via API as described here:
Custom Conversion API
Or just do it via Business Manager and use it afterward when creating you campaigns via API or manually.
It's definitely not a request that fails in this case but a proper account configuration.

How to grant/revoke an access to Cloud Object Storage resource automatically?

I have an iOS App. Would like to explore what is needed to be done to achieve the following:
1) The user taps on the map
2) US Census Tract info is requested from database
3) Later the user wants to purchase this tract info.
The US Census Tract info would be uploaded to Cloud Object Storage.
There are 70,000 Tracts grouped by US States = 50 + 1 (DC)
I could use SQL Query to select one Census Tract by its ID.
In the iOS App I can use Apple Login and get users' name and email.
The question is how to grant/revoke access to this info automatically
after in-app purchase?
The question is two-fold. Do I have to create 70,000 CSV files and grant them an access to? Or this can be achieved dynamically with SQL?
The second part is - how to automate this process?
Does IBM Cloud has this capability?
I would expect that you would use a single Service ID that would have access to the data sitting in COS, and that a user's access to the underlying data would be handled in your application logic. The Cloud IAM access policies are not intended for end-users as much as for internal development/operations teams to manage access to various cloud resources.
Depending on the format of the census data, SQL Query could be a great way to do it. You could use SQL query to create a new object with the subset of data the user has requested, and then create a presigned URL that will expire in a whatever timeframe is reasonable, allowing the file to be downloaded to the client device.

Google Analytics API Response - "User does not have sufficient permissions for this advanced segment" but they do

Using the GA API with a particular segment, the authenticated token is from an admin who created the segment. The segment was shared with the permissions for "Collaborators and I can apply/edit Segment in this View"
I've verified the Property ID, View ID, and Segment ID from the interface with the API call. The GA API returns "User does not have sufficient permissions for this advanced segment."
The user created the segment and shared it, so I'm at a bit of a loss as to why I get this error response. Any help would be much appreciated. Thanks!
#Ash has the correct answer but here is a more detailed response:
The user that created the Segment must open complete the following in Google Analytics:
Open the segment panel and select 'Custom', then click 'Edit' for the segment you would like to share.
Then click 'Change' in the top right corner.
You can now select the option allowing collaborators to apply and edit the segment within the view.
Save your changes.
This will allow you to query the custom segment using a service API.
1) A segment in Google Analytics belongs to the user who created it and only that user has access to the segment.
2) A service account will not have access to any of the saved advanced segments. For API requests you could try using dynamic segments instead.
So, a User has saved segments and those segments can be applied to any request for views (profiles) that the User has access to.
Example:
Assuming User A creates/saves a segment and User B also has saved segments and your application is authorized to access User A and B's Analytics account (i.e. they give permission to your App to access their data). The App would be able to retrieve the IDs of the saved segments for both users but you can't apply User A's segment ID to a request for User B's data. For that to work, User A would need to have access to User B's view (profile) to begin with.
I was facing same issue and the error was:
HttpError 403 when requesting https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned
"User does not have sufficient permissions for this advanced segment."
Creating shared Segment resolved above issue.
Credits and more info: Google groups

Google Analytics - Can I create VISITOR based dynamic segments in the API?

This is an EXPERT Google analytics question (you've been warned).
In the GA user interface I can create a segment based on a USER and not a VISIT.
When using the dynamic segments in the API I've discovered the segment is VISIT based.
Is is possible to tell the API I want to create a dynamic segment based on USER instead (like in the GUI)?
I looked into this some more and found that as of Dec 13, 2013 it is NOT possible to create dynamic user based segments via the API.
The problem is, that you even cant retrieve Id of Segment with visitor filtration, even if exists in Web interface.