How do I create a tracking pixel using Google Analytics for a third-party site?

We need to track conversions that occur on a third-party site. The only thing we can post on this site is an image pixel and possibly some kind of JS logic in order to run it.

I know that you can perform the conversion using the measurement protocol: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#visitor

Ideally, I would just give a third party an IMG URL, and that would be so. The problem is the CID (customer unique identifier).

I can try to pass the CID from our site to a third party through the URL parameter. However, there are many cases when it is not available (for example, IMG-pixcel will be in the email, the target URL is in the printed literature), or the third party does not want to go through the hassle. Is it better to use this CID in this way?

I can try to create a CID, but I cannot find an easy way to do this, for example var CID = generateCID (). A third-party site has its own GA on the page. Can I just use my Google Analytics ID and use it in the image pixel url?

What is the best way to do this? Thanks!

+6
source share
2 answers

If you already have analytics.js on a third-party site, then it is probably best to use this customer ID. You can get it by following these steps:

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

If analytics.js is not running or if you cannot access the ga variable for any reason, you can simply create a client identifier randomly. This is roughly what Google does. This is a random 31-bit integer with the current date string added:

 var cid = Math.floor(Math.random() * 0x7FFFFFFF) + "." + Math.floor(Date.now() / 1000); 
+3
source

Just for the addition of @Philip Walton, an excellent answer, Google Analytics expects a random UUID (version 4) as a client identifier in accordance with official documentation.

Customer id

Required for all types of strokes.

This anonymously identifies a specific user, device or browser example. For the Internet, this is usually stored as the first side of a cookie with a two-year expiration date. For mobile applications, this is randomly generated for each specific application installation instance. the value of this field must be a random UUID (version 4), as described at http://www.ietf.org/rfc/rfc4122.txt

@broofa provided an easy way to create RFC4122 compliant UUIDs in JavaScript here . Quoting it here for 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); }); 
+3
source

Source: https://habr.com/ru/post/987062/


All Articles