使用 GTM 进行跨域跟踪。
在特定页面上,单击按钮即可将用户从domainA 发送到domainB。
我想在目标链接内发送一个值(domainA 的引荐来源网址)作为 url 参数。
然后我将此参数值检索到domainB 上的数据层中。
<小时/>但是,每次按钮点击都会被 Google Analytics(分析)跨域跟踪覆盖。
在域 A 上:
- 按钮链接目标 = https://domainB/page
- 我要传递的参数=referrer
点击:
<小时/>这是我尝试过的一些代码...
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 控制台显示新的更新链接...但在浏览时,它没有按预期显示。
最佳答案
为了充分披露,我不知道谷歌分析的内部结构,所以如果有人知道,他们可能知道一些我不知道的事情。但是,我确实非常了解浏览器机制,并且由于安全限制,您所做的事情是不可能的。实现此目的的一种方法是,假设两个域(窗口)将在同一浏览器 session 中运行,则使用消息传递 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);
});
如果您遇到的情况不会在同一浏览器中运行,您将以简单的后端服务器/服务的形式设置某种“共享内存”。
关于javascript - 如何正确添加跨域 session 跟踪的url参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56007739/