如何使用Google Analytics for 3rd party网站创建跟踪像素?

时间:2021-05-19 15:49:18

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.

我们需要跟踪第三方网站上发生的转化。我们唯一可以放在该网站上的是一个图像像素,也许还有一些JS逻辑可以解雇它。

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

我知道可以使用“度量协议”触发转换: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).

理想情况下,我只是给第三方一个IMG网址,那就是它。问题是CID(唯一客户端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?

我可以尝试通过URL参数将CID从我们的站点传递给第三方。但是,有很多情况下它不可用(例如,IMG pixcel将在电子邮件中,目标URL在印刷文献上)或第三方不愿意经历麻烦。以这种方式传递此CID是最佳做法吗?

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?

我可以尝试生成一个CID,但我找不到一种简单的方法,例如,var CID = generateCID()。第三方网站在页面上有自己的GA。我可以使用他们的Google Analytics CID并在图片像素网址中使用它吗?

What the best way to do this? Thank you!

最好的方法是什么?谢谢!

2 个解决方案

#1


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:

如果第三方站点已运行analytics.js,则使用该客户端ID可能是最佳选择。您可以通过执行以下操作来获取它:

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:

如果analytics.js未运行,或者由于某种原因无法访问ga变量,则只能随机生成客户端ID。这与Google的做法差不多。它是一个随机的31位整数,附加了当前日期字符串:

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

#2


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

根据官方文档,仅为了补充@Philip Walton的优秀答案,Google Analytics希望随机UUID(版本4)作为客户端ID。

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

这匿名地标识特定用户,设备或浏览器实例。对于网络,这通常存储为具有两年到期的第一方cookie。对于移动应用程序,这是为应用程序安装的每个特定实例随机生成的。该字段的值应为随机UUID(版本4),如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:

@broofa提供了一种在JavaScript中生成符合RFC4122标准的UUID的简单方法。为了完整起见,请在此引用它:

'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);
});

#1


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:

如果第三方站点已运行analytics.js,则使用该客户端ID可能是最佳选择。您可以通过执行以下操作来获取它:

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:

如果analytics.js未运行,或者由于某种原因无法访问ga变量,则只能随机生成客户端ID。这与Google的做法差不多。它是一个随机的31位整数,附加了当前日期字符串:

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

#2


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

根据官方文档,仅为了补充@Philip Walton的优秀答案,Google Analytics希望随机UUID(版本4)作为客户端ID。

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

这匿名地标识特定用户,设备或浏览器实例。对于网络,这通常存储为具有两年到期的第一方cookie。对于移动应用程序,这是为应用程序安装的每个特定实例随机生成的。该字段的值应为随机UUID(版本4),如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:

@broofa提供了一种在JavaScript中生成符合RFC4122标准的UUID的简单方法。为了完整起见,请在此引用它:

'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);
});