javascript - 如何使用跨域会话跟踪正确添加 url 参数?
问题描述
使用 GTM 进行跨域跟踪。
在特定页面上,单击按钮将用户从域 A 发送到域 B。
我想在目标链接中发送一个值(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 控制台显示新更新的链接......但在浏览时,它没有按预期显示。
解决方案
为了全面披露,我不知道谷歌分析内部,所以如果有人知道,他们可能知道我不知道的事情。但是,我确实非常了解浏览器机制,并且由于安全限制,您正在做的事情是不可能的。假设两个域(窗口)都将在同一个浏览器会话中运行,这样做的一种方法是使用消息传递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);
});
如果您正在处理不会在同一浏览器中运行的情况,您将以简单的后端服务器/服务的形式设置某种“共享内存”。
推荐阅读
- python-3.7 - Python 教程中的 NoReverseMatch 错误
- asp.net-core - .net core web api上传文件失败,请求错误
- unit-testing - 在 BDD 中测试副作用
- javascript - Three.js 动画混合器不播放?
- arrays - 如何找到多个数组中的最大元素值?
- jquery - JW Player - 禁用快进在 Microsoft Edge/IE11/Safari 中不起作用
- sql - 在子查询中使用自连接
- excel-formula - 如果结果高于阈值,则减去金额,没有重复的公式
- excel - VBA 出现问题,旨在防止在保存时编辑具有值的单元格
- javascript - 使用模式 Bootstrap-vue 在背景上获取事件点击