Google Analytics V3 - How to create custom segments for API Service Accounts

六月ゝ 毕业季﹏ 提交于 2019-12-11 09:42:58

问题


I need to access custom segments in Google Analytics via reporting the API. There is no trouble with it, except that custom segments are not shareable across multiple profiles/users set in Google Analytics, so a segment has to be created for each profile.

And in the case of using a Service Account generated through Google API's Console, segments have to be created for that account – how do I log in to Google Analytics with a Service account having Client ID, email address and public key fingerprint to be able to set segments for it?

It is not currently possible to create segments with the Google Analytics Management API.

Edit:

As of July 9, 2013 Google Analytics added access to custom dimension and metrics. Now it is possible to query custom dimensions without the need to create additional segments, so the roots of my problem have disappeared.


回答1:


It currently is not possible to create segments for a service account. As you suggested you'd need to be able to login to the GA account to create it, which is not possible with a service account. The only option right now is to use dynamic segments when querying the Core Reporting API.




回答2:


You have to share your segment to your collaborators to enable the API account to use it in the queries. Just click on the top right link "Change" and choose "Collaborators and I can apply/edit Segment in this View"




回答3:


If you want to avoid having to create dynamic segments in your API query, you can also do this: create the segment at you would like it in your Analytics account.

Then go to the API query explorer and scroll down to the "segment" field.

When you select the segment you want, the id will appear. You can use that id to query the API as described here.




回答4:


you can access the custom segment by putting in the segment id the segment filter and to get that particular segment information you need to create an analytics service like this

$service = new Google_AnalyticsService($client);

now use the following to get all segments(by default and custom created) information :

$segments = $service->management_segments->listManagementSegments();

var_dump or print_r the $segment array to get the list and use the advanced segment id you find there to get the result by querying it to get the data for example i created an advanced custom segment in my analytics by the name abc and then in the $segments array i get the output array of which one of them does have the name field value abc in that element there will be a field named segment_id and will have value like gaid::12345 use that value while querying to get the data by placing it in the segment option of optparameters array of getdata field allong with different values.



来源:https://stackoverflow.com/questions/16962182/google-analytics-v3-how-to-create-custom-segments-for-api-service-accounts

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