admin管理员组文章数量:1391989
I need to track when someone is downloading a file from my server, so I created an endpoint that take as parameter a filename.
This endpoint simply sends a GA event and returns the file to the user.
But, as I am server side, I can't easily get the Client ID to send it back to Google Analytics. After some research, this CID is available in the _ga
cookie but Google advise to not count on it as the cookie might change in the future:
().
So, how can I gather the Client ID server-side to send it back with my event ?
I need to track when someone is downloading a file from my server, so I created an endpoint that take as parameter a filename.
This endpoint simply sends a GA event and returns the file to the user.
But, as I am server side, I can't easily get the Client ID to send it back to Google Analytics. After some research, this CID is available in the _ga
cookie but Google advise to not count on it as the cookie might change in the future:
(https://developers.google./analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the_client_id_from_the_cookie).
So, how can I gather the Client ID server-side to send it back with my event ?
Share Improve this question edited Mar 17, 2017 at 19:14 Lee Taylor 7,98416 gold badges37 silver badges53 bronze badges asked Mar 17, 2017 at 19:01 Anthony GrangerAnthony Granger 81010 silver badges23 bronze badges1 Answer
Reset to default 6The _ga cookie is generated and used by the analytics.js library and it is the way that Google Analytics identifies users on your site. This value is generated to be stored in the cookie so as far as I know you cannot generate it from an API call for example. Even if you have done your research allow me provide you some information on the _ga value. The value of the cookie as you have seen is something like GA1.2.XXXXXXXXX.XXXXXXXXXX
The part you have to extract to get the CID value that you will send with your server side request is the two last parts of the value separated by a dot(.) meaning:
GA1.2.XXXXXXXXX.XXXXXXXXXX <-- The bold part including the dot in the middle
Also as you have referenced, Google reports that you should not count on the 'name' of the cookie. Meaning that a function like getCookie('_ga') will not be guaranteed to work in the future. But if you keep on reading the analytics tracker object provides a function callback that will provide you with this value guaranteed.
ga(function(tracker) {
var clientId = tracker.get('clientId');
});
The way I would go about it is use this callback, get the CID, send it encrypted in the payload of my request(ex. as an extra parameter), decode it at the server and then send the Measurement Protocol request.
本文标签: javascriptHow to gather Google Analytics39 Client ID serverside from a GET requestStack Overflow
版权声明:本文标题:javascript - How to gather Google Analytics' Client ID server-side from a GET request? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744744918a2622819.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论