首页 > 解决方案 > 使用 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 跟踪代码管理器中有我常用的域。)

我想要做的就是将我的查询字符串传递给访问者在我的站点中单击的任何页面。

标签: javascriptutm

解决方案


推荐阅读