首页 > 解决方案 > 如何使用跨域会话跟踪正确添加 url 参数?

问题描述

使用 GTM 进行跨域跟踪。

在特定页面上,单击按钮将用户从域 A 发送到域 B。

我想在目标链接中发送一个值(domainA 的引用者)作为url 参数。

然后我将此参数值检索到 domainB 上的数据层中。


但是,每次按钮点击都会被 Google Analytics 跨域跟踪覆盖。

在域A上:

点击:


这是我尝试过的一些代码...

Onclick,更新链接目标:

<script>
function urlBouton () {
var newUrl= "https://domainB/page?referrer=google";
document.getElementById("btn").onclick = function() {changeUrl()};

function changeUrl() {
  document.getElementById("btn").href = newUrl;
}
  }
  </script>

当页面打开(DOM 就绪)时,更改按钮链接:

<script>
var newUrl = "https://domainB/page?referrer=google";
document.getElementById("btn").href.setAttribute("href", newUrl);
</script>

JS 控制台显示新更新的链接......但在浏览时,它没有按预期显示。

标签: javascriptcross-domainsession-cookiesurl-parameters

解决方案


为了全面披露,我不知道谷歌分析内部,所以如果有人知道,他们可能知道我不知道的事情。但是,我确实非常了解浏览器机制,并且由于安全限制,您正在做的事情是不可能的。假设两个域(窗口)都将在同一个浏览器会话中运行,这样做的一种方法是使用消息传递window.postMessage()。然而,这有点限制,因为在您发布消息之前,必须打开第二个窗口并收听传入的消息。

    window.addEventListener('message', function _handler(event) {
    // Code for handling the message

    // This will automatically remove the listener after receiving one message.
    // Good for when you send only one message and don't want the listener lingering and eating up memory.
    window.removeEventListener('message', _handler);
});

如果您正在处理不会在同一浏览器中运行的情况,您将以简单的后端服务器/服务的形式设置某种“共享内存”。


推荐阅读