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
For (1), what I was thinking I could do is:
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!
The Google Analytics Platform lets you measure user interactions with your business across various devices and environments. The platform provides all the computing resources to collect, store, process, and report on these user-interactions. Analytics collection can take place on both the client and server side.
Google's servers process their request logs and use the information about that request to process the data and reconstruct the session based on the hits. If you installed this script at the bottom of every page, every time someone loads a page the script will embed, download ga.
A Client ID represents a unique browser/device and is created and assigned by Universal Analytics cookie _ga. Client ID is assigned to each unique user of your website. You can find the client ids through the 'User Explorer' report (under 'Audience') in your GA reporting view.
I would also recommend you go with option 2: Let Google handle setting of cid
parameter.
Since the parameter is already set in the _ga cookie on your domain, it is passed to your app server with every HTTP request.
All that you need to do is on the server side :
For example, this answer provides the Rails code:
def save_google_analytics_client_id
if current_user && cookies["_ga"]
client_id = cookies["_ga"].split(".").last(2).join(".")
if current_user.google_analytics_client_id != client_id
current_user.google_analytics_client_id = client_id
current_user.save
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With