简体   繁体   English

Google跟踪代码管理器-有时未在自定义Java上设置

[英]Google Tag Manager - Not Set on Custom Javascript Occasionally

I set up a Google Tag to check what zip code my users are searching for. 我设置了一个Google标记,以检查用户正在搜索的邮政编码。 I used the Custom Javascript variable type to set up this variable. 我使用了自定义Javascript变量类型来设置此变量。

 function() {
    var url = new URL({{Page URL}});
    var zip = url.searchParams.get("zip");
    return (zip);
  }

I triggered the tag when users went to an address containing "locations.aspx?term". 当用户转到包含“ locations.aspx?term”的地址时,我触发了标签。 Here is an example URL: 这是一个示例URL:

https://www.example.com/locations.aspx?term=64544&zip=64160&lat=43.3414758&lng=-0.1509269000000586 https://www.example.com/locations.aspx?term=64544&zip=64160&lat=43.3414758&lng=-0.1509269000000586

What should I be looking for to debug why 50% of the returns are (not set) in Google Analytics? 我应该寻找什么来调试为什么Google Analytics(分析有50%的回报(未设置) The other half of the returns are coming back just fine. 另一半的回报也很好。

Thanks! 谢谢!

There is two problems in your JS: 您的JS有两个问题:

  • It is not supported in Internet Explorer Internet Explorer不支持
  • If url doesn't have querstring with zip params, it will return null . 如果url没有带zip参数的querstring,它将返回null It will cause (not set) 会导致(not set)

Best way to do that is: 最好的方法是:

function () {
    function getURLParameter(name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || "";
    }
    var zip = getURLParameter('zip');
    return zip;
}

While this does not address your JavaScript problems, GTM has a built-in way to get query parameters that in my experience is reliable and will probably solve your problem. 虽然这不能解决您的JavaScript问题,但GTM具有内置的获取查询参数的方式,据我所知,该参数是可靠的,并且可能会解决您的问题。 You can use the URL variable type and change the "component type" setting from "Full URL" to "query", and enter your query key (the name of the url parameter). 您可以使用URL变量类型,并将“组件类型”设置从“完整URL”更改为“查询”,然后输入查询键(url参数的名称)。 This will return the value of the parameter, or "undefined" if it doesn't exist. 这将返回参数的值,如果不存在则返回“ undefined”。

获取查询GTM变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM