Measurement protocol V2 Google analytics Apps+Web

In Ringostat, historically, we have integration with Google Analytics from the beginning. That is a requirement.

As call tracking, we need to post data about phone calls to sessions of clients, so they will count as events, decrease bounce, and can be used as goals.

With Google Universal Analytics and release of measurement protocol, this integration became much better, and the first call we registered through measurement protocol on December 29th, 2012, it was in closed beta for that moment. As I know, we were the first call tracking service to do that, but I made a big marketing mistake not to use this in communication.

But time moves forward, and Google Analytics has a new stage in beta Apps+Web, and we got a client that uses that type of account. And we discovered that our integration doesn’t work because of new collect endpoint and entirely different way events work there.

But there is still so low amount of information the only useful article I found from David Vallejo, but it’s not currently cover what mandatory fields are and how to get their values.

So in this article, I’ll share a summary of mine tries and fails. Which fields mandatory and how to fill them.

Same as previously, we need to send data to collect endpoint, but now it has another URL. Also changed the protocol version, now it’s 2. And the way how tracking ID now looks like and now it’s not tracking ID but stream ID, and you can have different streams for one property.

Still, you need client ID to post data to the same client, but to get it now, you need to get ‘vid’ property of ‘gaGlobal’ object. Just call ‘gaGlobal.vid’ in javascript instead ga.getAll()[0].get(‘clientId’) as you could do previously. There is a lot of other differences that I’ll not cover in this article. But the main difference for me is new params:

  1. Session ID, for session ID, used UNIX timestamp of session start and stored in the new cookie that has a name like _ga_XXXXXXXXX where XXXXXXXXX is stream ID without G-.
  2. Session count, number of sessions for this clientId. Weird thing, but it looks like this is required, but it also stored in the cookie.

To parse those variables from the cookie in one line I modified suggested snippets of code to:

document.cookie.match('(^|;)\\s*_ga_XXXXXXXXX\\s*=\\s*([^;]+)').pop().split('.')[2]

Just fix XXXXXXXXX with yours stream ID.

Long story short, we need to build the request with the parameters that I found as required and still send it to https://www.google-analytics.com/g/collect. I found strange information that I have to use only POST request with separation some parameters in URL and other in payload, but GET works fine also. So the params are

ParametermeaningWhat should we use here
vProtocol Version2
tidStream IDG-XXXXXXXXX (find in account or your code)
cidClient IDjavascript: gaGlobal.vid
sidSession IDjavascript: document.cookie.match(‘(^|;)\\s*_ga_XXXXXXXXX\\s*=\\s*([^;]+)’).pop().split(‘.’)[2]
sctSession Countjavascript: document.cookie.match(‘(^|;)\\s*_ga_XXXXXXXXX\\s*=\\s*([^;]+)’).pop().split(‘.’)[3]
dlDocument locationjavascript: document.location
enEvent namestring, how you want to name your event ex. ‘call’

Also, it’s good to add _dbg=1 for debugging to see data in realtime at DebugView (extremely cool feature!).

After collection such params you can build URL that would look like:

https://www.google-analytics.com/g/collect?v=2&_dbg=1&tid=G-EYPV5EM8S1&cid=1487972805.1593070957&dl=https%3A%2F%2Flunaxod.com&sid=1593070957&sct=1&en=call

Call it, and in 10 seconds, see the event in DebugView if you have done everything right. After that, you can improve this call with additional event params and user params.