I have been using Google Analytics for basic analytics for my web app - just tracking page impressions using javascript calls like this:
ga('create', 'UA-XXXXXXXXX-1', 'mydomain.com');ga('send', 'pageview')
This approach has always frustrated me because I couldn't reliably capture some server-side events. I have just discovered I can use Measurement Protocol to record events server-side. Recording the events on my server looks easy, except regarding the cid (clientid) parameter...
What I understand is that on the browser, with the javascript I currently have the cid gets randomly created and then stored in the _ga cookie. I also understand that I should share that clientid/cid value between client ('page view') and server (other events) calls for the same client so that they are correlated together.
This StackOverflow link was a helpful reference for me.
Question is: should I
- Create a clientid on the server and then share it with the client; or
- Should I let the javascript on the client create the clientid and then try to share it with my server? (I suspect this is the better answer)
For (1), what I was thinking I could do is:
- Store a UUID in the session on the server (which is google app engine)
- Directly use that UUID when I use Measurement Protocol to create events directly server-side
Use the same UUID when I create a ga object on a page using jsp:
ga('create', 'UA-XXXXXXXXX-1', 'mydomain.com', {'clientId': '<%=[value from the session]%>'});
The thing that worries me about this approach is that the ID will only persist across the session on the server. I think the intent of the clientId (cid) is that it persists for the client over an extended period... So I think I will lose track of who is new versus returning user?
For (2), frankly I don't know how to do this... I know from the above StackOverflow link that I can get the cid out of the clientId parameters in the ga object. I don't know how I would then send it back to my server (this is probably a simple javascript question).
Would definitely appreciate advice on which approach to use.... thank you!