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

2

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!

Sacking answered 12/5, 2015 at 14:51 Comment(2)
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.Preterite
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?Sacking
M
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);
Mukerji answered 12/5, 2015 at 17:56 Comment(10)
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.Sacking
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.Sacking
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).Mukerji
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.Sacking
That's what the snippet code does. It initialized the ga() function. developers.google.com/analytics/devguides/collection/…Mukerji
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.Sacking
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.Mukerji
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.Sacking
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.Mukerji
You're right. I will replace timeout with event handler.Sacking
S
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);
});
Superfuse answered 12/5, 2015 at 20:8 Comment(6)
Thanks for quoting that and clarifying about Google official requirement.Sacking
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.Sacking
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, so you might be limited to only one per Session Duration and risk missing Conversions if Client IDs are shared.Superfuse
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.Mukerji
Of course, @PhilipWalton 's answer is perfectly valid :)Superfuse
Understood. Thanks guys so much for all your answers.Sacking

© 2022 - 2024 — McMap. All rights reserved.