【发布时间】:2020-06-15 05:18:12
【问题描述】:
我正在尝试将我的 facebook 广告中的 UTM 参数传递到我网站上的所有页面。
示例查询字符串:www.mysite.com/?utm__source=facebook&utm_medium=psocial&utm_campaign=summer&utm_content=pool&fbclid=1234Jfshio
通常,utm 参数将存在于用户访问的第一页上,但一旦他们导航到新页面,查询字符串就会丢失。
我希望将查询字符串附加到用户在会话期间导航到的所有页面的 URL 末尾。
我必须这样做,以便当他们打开与我们的 LiveChat 服务的聊天时,我们的服务捕获的起始 URL 包含他们点击的广告的 utm 参数。
这是我当前使用的代码 - 但它没有按预期工作:
<script type="text/javascript">
(function() {
var utmInheritingDomain = "brilliance.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
"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>
【问题讨论】:
标签: javascript google-tag-manager