8

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!

Vlad
  • 612
  • 1
  • 7
  • 13
  • While Google suggests a UUID format for the client id you can actually use any string or number. UUID is recommended because it avoids collisions, if you need the sheer number of conversions without getting individual users you can simply use the same number for all pixels (or one per conversion). Else generate a random number in JS and use that. – Eike Pierstorff May 12 '15 at 15:07
  • If I used a static tracking pixel, how would that affect how goals, events, and users are tracked? Would GA think these are all fired by the same person? Would goals and events fire multiple times as needed? When you say "sheer number of conversions", what metric/report do you mean? – Vlad May 12 '15 at 20:42

2 Answers2

4

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);
Philip Walton
  • 29,693
  • 16
  • 60
  • 84
  • Thanks, Philip. I probably will take the existing CID then. But if I do need to generate a CID, the one provided by Philippe Sawicki in his answer looks more complete. – Vlad May 12 '15 at 20:37
  • To get the CID via tracker.get('clientId') I need to create a setInterval loop to wait until GA is ready. That makes me think there is no advantage to using the 3rd party's CID anyway, since that id is meaningless to another GA instance. I'm thinking might as well just generate a new CID. – Vlad Jun 12 '15 at 15:30
  • FWIW, you don't have to use `setInterval`. If you pass the `ga()` function a function as the argument, it'll be executed as soon as the library is ready (as I show in my answer). – Philip Walton Jun 12 '15 at 15:54
  • What if ga() doesn't exit yet? I believe I initially tried to fire it on DOM ready and got an error. That's why I added setInterval. – Vlad Jun 14 '15 at 02:58
  • That's what the snippet code does. It initialized the `ga()` function. https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced – Philip Walton Jun 14 '15 at 05:37
  • Maybe it was the tracker object. The link you sent says the snippet asynchronously loads the analytics library. So if I refer to tracker before the library is ready, pehaps tracker doesn't exist yet OR it does but the get method returns nothing. – Vlad Jun 14 '15 at 15:44
  • If you look at what the actual code is doing (described on that page), you see it first creates a placeholder `ga()` function and then it loads the full *analytics.js* library. So as long as the snippet code has already been executed, you can always use the `ga()` function as normal without worrying about whether or not the library has loaded. – Philip Walton Jun 14 '15 at 17:17
  • Try running var cid; ga(function(tracker) { cid = tracker.get('clientId'); }) right after the GA snippet and then do alert(cid). I don't know about you, but I get "undefined". If I add a pause, then it works. This tells me the tracker object is not ready immediately, which is what I would expect since the code is asynchronous. It looks to me like ga() waits until the tracker is ready and then fires the function(tracker) callback. – Vlad Jun 16 '15 at 19:11
  • I'm not saying the tracker object is available immediately, I'm saying the `ga()` function is, so no `setTimeout` is needed since you can use the callback to know when the tracker is available for use. – Philip Walton Jun 16 '15 at 19:31
  • You're right. I will replace timeout with event handler. – Vlad Jun 17 '15 at 18:35
4

Only to complement @Philip Walton excellent answer, Google Analytics expects a random UUID (version 4) as the Client ID, according to the official Documentation.

Client ID

Required for all hit types.

This anonymously identifies a particular user, device, or browser instance. For the web, this is generally stored as a first-party cookie with a two-year expiration. For mobile apps, this is randomly generated for each particular instance of an application install. The value of this field should be a random UUID (version 4) as described in http://www.ietf.org/rfc/rfc4122.txt

@broofa provided a simple way to generate a RFC4122-compliant UUID in JavaScript here. Quoting it here for the sake of completeness:

'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
});
Community
  • 1
  • 1
Philippe Sawicki
  • 852
  • 14
  • 22
  • Thanks for quoting that and clarifying about Google official requirement. – Vlad May 12 '15 at 20:38
  • Do you know what the consequences are of using a static CID for all pixel hits? Curious if that's advisable or even an option. – Vlad May 12 '15 at 20:44
  • By using a static CID for all hits, Analytics will assume that all PageViews, Events, Dimensions, Metrics, etc. all come from the same device/browser. If you only care about the "volume" of Events/PageViews, it will not matter much, but you that will not record data for Sessions/Users/Returning Visitors, etc., then you will need a UUID for each visitor. Moreover, [Goal Conversions happen only once per visit](https://support.google.com/analytics/answer/6083326?hl=en), so you might be limited to only one per Session Duration and risk missing Conversions if Client IDs are shared. – Philippe Sawicki May 12 '15 at 23:12
  • 1
    This answer is perfectly valid, and arguably a better way to generate a client ID. However, I want to point out that not using a UUID v4 will in no way cause your code to break or your hits to be rejected by Google Analytics. The recommendation is to minimize the risk of duplicates, not for conformity purposes. – Philip Walton May 12 '15 at 23:25
  • Of course, @PhilipWalton 's answer is perfectly valid :) – Philippe Sawicki May 13 '15 at 00:27
  • Understood. Thanks guys so much for all your answers. – Vlad May 13 '15 at 15:23