Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create tracking pixel with Google Analytics for 3rd party site?

We need to track conversions that happen on a 3rd party site. The only thing we can place on that site is an image pixel and maybe some JS logic for when to fire it.

I know it is possible to fire a conversion using the Measurement Protocol: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#visitor

Ideally, I'd just give the 3rd party an IMG url and that would be it. The problem is the CID (unique client id).

I can try passing the CID from our site to the 3rd party via URL parameter. However, there are many cases where its not available (e.g., IMG pixcel will be in an email, the goal URL is on printed literature) or the 3rd party is not willing to go through the hassle. Is it best practice to pass this CID in this way?

I can try generating a CID, but I can't find a dead simple way of doing that e.g., var CID = generateCID(). The 3rd party site has its own GA on the page. Can I just take their Google Analytics CID and use it in the image pixel URL?

What the best way to do this? Thank you!

like image 806
Vlad Avatar asked May 12 '15 14:05

Vlad


1 Answers

If the 3rd-party site has analytics.js already running then using that client ID is probably best. You can get it by doing the following:

var cid;
ga(function(tracker) {
  cid = tracker.get('clientId'));
});

If analytics.js is not running, or if you can't access the ga variable for some reason, you can just generate the client ID randomly. This is approximately what Google does. It's a random 31-bit integer with the current date string appended:

var cid = Math.floor(Math.random() * 0x7FFFFFFF) + "." +
          Math.floor(Date.now() / 1000);
like image 196
Philip Walton Avatar answered Sep 18 '22 17:09

Philip Walton