javascript - 使用 GTM 将 UTM 参数从一个页面传输到另一个页面
问题描述
我无法在所有页面之间传递我的 UTM 参数。
这是我的代码:
<script type="text/javascript">
(function() {
var utmInheritingDomain = "mysite.com", // REPLACE THIS DOMAIN
utmRegExp = /(\&|\?)utm_[A-Za-z]+=[A-Za-z0-9]+/gi,
links = document.getElementsByTagName("a"),
utms = [
"utm_source={{url - utm_source}}", // IN GTM, CREATE A URL VARIABLE utm_source
"utm_medium={{url - utm_medium}}", // IN GTM, CREATE A URL VARIABLE utm_medium
"utm_campaign={{url - utm_campaign}}", // IN GTM, CREATE A URL VARIABLE utm_campaign
"utm_content={{url - utm_content}}", // IN GTM, CREATE A URL VARIABLE utm_content
"fbclid={{url - fbclid}}", // IN GTM, CREATE A URL VARIABLE fbclid
];
for (var index = 0; index < links.length; index += 1) {
var tempLink = links[index].href,
tempParts;
if (tempLink.indexOf(utmInheritingDomain) > 0) { // The script is looking for all links with the utmInheritingDomain
tempLink = tempLink.replace(utmRegExp, "");
tempParts = tempLink.split("#");
if (tempParts[0].indexOf("?") < 0 ) {
tempParts[0] += "?" + utms.join("&"); // The script adds UTM parameters to all links with the domain you've defined
} else {
tempParts[0] += "&" + utms.join("&");
}
tempLink = tempParts.join("#");
}
links[index].href = tempLink;
}
}());
</script>
每次我尝试点击我网站中的多个链接时都会中断(我在 mysite.com 中输入了示例代码,但它在 Google 跟踪代码管理器中有我常用的域。)
我想要做的就是将我的查询字符串传递给访问者在我的站点中单击的任何页面。
解决方案
推荐阅读
- node.js - 如何在同一个 GET 请求中呈现文件并将数据发送到页面
- c++ - 为什么我的代码在要求打印长度和宽度的值后不会接受多个输入?
- python - 单括号 df["column"] 和双括号 df[["column"]] 有什么区别
- azure - 应用服务交换槽期间出现多个 Redis 连接异常(No Connection available to service)
- groovy - Groovy 列表包含 [x..y] 上的方法
- google-compute-engine - 将 http 重定向到 https 谷歌云负载均衡
- impala - 在 Impala 中选择 stack() UDTF
- java - 刷新后看不到更新记录
- python - 将文件中的值加载到列表中以及如何隔离它们
- ios - 如何为您的 Swift 框架私下导入 3rd 方框架