[英]Manually generate GA linker parameter with gtag (GA4)
To pass a client_id from one domain to another, Google supports adding a "linker" parameter to outgoing Links that are part of the cross-domain tracking setup.要将 client_id 从一个域传递到另一个域,Google 支持向作为跨域跟踪设置一部分的传出链接添加“链接器”参数。 This linker parameter contains the client_id
, session_id
(I believe, information about Google Ads, eg gclid
) and a basic fingerprint + timestamp.这个 linker 参数包含client_id
、 session_id
(我相信,关于 Google Ads 的信息,例如gclid
)和一个基本的指纹 + 时间戳。 On the receiving domain, if the browser fingerprint matches and the timestamp is not too far in the past, the passed client_id
and session_id
are stored in a first party cookie on the 2nd domain and consequently used.在接收域上,如果浏览器指纹匹配并且时间戳不是过去太远,则传递的client_id
和session_id
将存储在第二个域的第一方 cookie 中并随后使用。
With analytics.js
(GA-UA) you could easily do the following, to decorate URLs manually:使用analytics.js
(GA-UA),您可以轻松地执行以下操作,手动修饰 URL:
function decorateUrl(urlString) {
var ga = window[window['GoogleAnalyticsObject']];
var tracker;
if (ga && typeof ga.getAll === 'function') {
tracker = ga.getAll()[0]; // Uses the first tracker created on the page
urlString = (new window.gaplugins.Linker(tracker)).decorate(urlString);
}
return urlString;
}
Yet, when only gtag is loaded, window.ga
and window.gaplugins
are not defined.然而,当只加载 gtag 时, window.ga
和window.gaplugins
没有定义。 As far as I see, there is currently no documented way to manually generate links with the linker parameter with gtag.据我所知,目前没有记录的方法来手动生成带有 gtag 的 linker 参数的链接。
In Google's documentation, they suggest setting up the linker manually.在 Google 的文档中,他们建议手动设置 linker。 ( https://support.google.com/analytics/answer/10071811?hl=en#zippy=%2Cmanual-setup ) But this has several disadvantages, eg I have to create a custom "fingerprint" logic (so that decorated URLs are not shared) and eg Google Ads information is not included. ( https://support.google.com/analytics/answer/10071811?hl=en#zippy=%2Cmanual-setup ) 但这有几个缺点,例如我必须创建一个自定义的“指纹”逻辑(这样修饰的 URL不共享),例如不包括 Google Ads 信息。 Either way, I would like to use the internal gtag logic to decorate URLs.无论哪种方式,我都想使用内部 gtag 逻辑来修饰 URL。
gtag automatically decorates a
tags (as soon as they're clicked) that lead to a cross-domain-tracking domain specified in the GA4 data stream settings (eg "test.com"), but I specifically need to decorate URLs manually (ie without immediately redirecting to them). gtag a
自动修饰标签(只要它们被点击),导致在 GA4 数据 stream 设置中指定的跨域跟踪域(例如“test.com”),但我特别需要手动修饰 URL(即而不会立即重定向到他们)。 I thought about doing the following:我考虑过执行以下操作:
a
tag with the URL to decorate创建一个假人,隐藏a
标签 URL 来装饰onclick='event.preventDefault();'
使用onclick='event.preventDefault();'
防止重定向function decorateUrlGtag(urlString) {
var tempAnchorEl = document.createElement("a");
tempAnchorEl.setAttribute("type", "hidden");
tempAnchorEl.setAttribute("href", urlString);
tempAnchorEl.setAttribute("onclick", "event.preventDefault(); return false");
document.body.appendChild(tempAnchorEl);
tempAnchorEl.click();
var urlWithLinker = tempAnchorEl.href;
tempAnchorEl.remove();
return urlWithLinker;
}
This also does not work, because gtag does not seem to register the tempAnchorEl.click();
这也不起作用,因为 gtag 似乎没有注册tempAnchorEl.click();
call.称呼。 If I click the link manually, the URL is decorated - as expected.如果我手动单击该链接,则 URL 会按预期进行装饰。
The solutions outlined here ( Google Analytics gtag.js Manually adding the linker cross-domain parameter to URLs ) also do not work for me:此处概述的解决方案( Google Analytics gtag.js Manually adding the linker cross-domain parameter to URLs )对我也不起作用:
ga
defined)答:同样的问题(没有定义ga
)Do you (1) know if there is a way to generate the linker parameter manually with gtag that I have overlooked, (2) know how to make my "hacky" solution work or (3) have another possible solution?您是否 (1) 知道是否有一种方法可以使用我忽略的 gtag 手动生成 linker 参数,(2) 知道如何使我的“hacky”解决方案起作用或 (3) 有其他可能的解决方案?
I haven't grokked this solution, and I am not sure it answers your question directly, but Simo does give an outline of how to configure GA4 cross domain tracking here:我没有摸索过这个解决方案,我不确定它是否直接回答了您的问题,但 Simo 确实在此处概述了如何配置 GA4 跨域跟踪:
https://www.simoahava.com/gtm-tips/cross-domain-tracking-google-analytics-4/#how-to-configure-cross-domain-tracking-manually https://www.simoahava.com/gtm-tips/cross-domain-tracking-google-analytics-4/#how-to-configure-cross-domain-tracking-manually
He breaks the problem down into steps but does not go into great detail.他将问题分解为多个步骤,但没有 go 非常详细。 He provides one code snippet:他提供了一个代码片段:
"...you could also load the URL parameter values directly into the GA4 configuration with something like: “...您还可以将 URL 参数值直接加载到 GA4 配置中,例如:
gtag('config', 'G-12345', {
// Namespace roll-up trackers
cookie_prefix: 'roll-up',
// Pull in the Client ID from the URL
client_id: (new URLSearchParams(document.location.search)).get('client_id'),
// Pull in the Session ID from the URL
session_id: (new URLSearchParams(document.location.search)).get('session_id')
});
" “
Hope that helps!希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.