简体   繁体   English

使用 gtag (GA4) 手动生成 GA linker 参数

[英]Manually generate GA linker parameter with gtag (GA4)

Background背景

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_idsession_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_idsession_id将存储在第二个域的第一方 cookie 中并随后使用。

analytics.js / GA-UA analytics.js / GA-UA

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.gawindow.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。

"Hacky" Workaround Solution “Hacky”解决方案

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:我考虑过执行以下操作:

  1. Create a dummy, hidden a tag with the URL to decorate创建一个假人,隐藏a标签 URL 来装饰
  2. Prevent redirection with onclick='event.preventDefault();'使用onclick='event.preventDefault();'防止重定向
  3. Simulate click on hidden element so that gtag automatically adds the linker url parameter to the href attribute模拟点击隐藏元素,让gtag自动在href属性中添加linker url参数
  4. Extract new href attribute提取新的 href 属性
  5. Remove hidden element删除隐藏元素
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 会按预期进行装饰。

Suggested Solutions建议的解决方案

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 )对我也不起作用:

  1. Answer: Even after gtag is initiated, I do not see a global ga element回答:即使启动 gtag 后,我也看不到全局 ga 元素
  2. Answer: Same problem (no 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.

相关问题 GA4 谷歌分析。 gtag.js 发送请求有几秒钟的延迟,而不是立即 - GA4 Google Analytics. gtag.js sends a request with a delay of a few seconds, not instantly 在 Google 跟踪代码管理器和 Google Analytics 4 (GA4) 中手动触发 page_view 的规范方法是什么? - What is the canonical way to manually fire page_view in Google Tag Manager and Google Analytics 4 (GA4)? 外部JS未在liferay 7 GA4中加载 - External JS not loading in liferay 7 GA4 Eventlistener 触发了太多的 GA4 事件 - Eventlistener fires too many GA4 events 在 ionic/cordova web 应用程序上从 GA 迁移到 GTAG 的问题 - Issue migrating from GA to GTAG on ionic/cordova web app 如何在GA(gtag.js)中跟踪Facebook销售流量? - How to track Facebook sale traffic in GA (gtag.js)? 如何在没有 Google Tag Manager 的情况下以编程方式将事件发送到 GA4? - How to programatically send an event to GA4 without Google Tag Manager? 如何使用 GA4 创建自定义维度并获得唯一的 PageView? - How to create Custom Dimension & get Unique PageView with GA4? 如何在 Google Analytics(分析)4 (GA4) 媒体资源中添加引荐排除项 - How to add referral exclusions in Google Analytics 4 (GA4) properties 是否仍然可以在 Google Analytics 4 (GA4) 中匿名化用户的 IP? - Is it still possible to anonymize the user's IP in Google Analytics 4 (GA4)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM