Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Google Analytics ClientID between javascript client and java server

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

  1. Create a clientid on the server and then share it with the client; or
  2. 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!

like image 629
nickpharris Avatar asked Mar 23 '15 05:03

nickpharris


People also ask

Is Google Analytics client side?

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.

How does Google Analytics Javascript work?

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.

Where is client ID in Google Analytics?

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.


1 Answers

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 :

  1. Extract the CID from the _ga cookie
  2. Link it to the current user (your app probably uses its own session cookies to identify the user)

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
like image 135
Andy H. Avatar answered Oct 01 '22 00:10

Andy H.