regex 从Google Analytics中删除空查询字符串参数

gv8xihay  于 2023-04-13  发布在  Go
关注(0)|答案(3)|浏览(114)

我想知道是否有人知道如何从Google Analytics中删除空查询字符串参数?
URL示例如下:http://www.domain.com/index.asp?a=test&b=test&c=&d=test
在这个例子中,我希望谷歌分析过滤掉“c”,因为它是空的。
感谢您提供的任何帮助!

n6lpvg4x

n6lpvg4x1#

@user1494396;重构你的异步跟踪代码,从正则表达式中传递页面名称,如下所示:

(function (window) {
    function cleanQs() {
        if (!window.location.search) {
            return window.location.pathname;
        }
        var locSearchArr = window.location.search.match(/[^\=\&\?]+=[^\=\&\?]+/g);
        var locPathName = window.location.pathname;
        if (locSearchArr && locSearchArr.length > 0) {
            locPathName += "?" + locSearchArr.join("&");
        }
        return locPathName;
    }

    var _gaq = window._gaq || (window._gaq = []);
    window._gaq.push(['_setAccount', 'UA-XXXXX-X']);
    window._gaq.push(['_trackPageview', cleanQs()]);
})(this);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

参见:

gmxoilav

gmxoilav2#

"http://www.domain.com/index.asp?a=test&b=test&c=&d=test".match(/[^\=\&\?]+=[^\=\&\?]+/g) --JavaScript
应该返回一个数组[“a=test”,“B=test”,“d=test”]。

cotxawn7

cotxawn73#

有必要区分两种实现GA4的方法,通过gtag.js或通过Google Tag Manager。

Gtag.js

对于通过gtag.js实现的Google Analytics,将page_location字段添加到GA4代码段:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=...your id..."> 
</script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'YOUR-ID-HERE', {
    // here the below function goes..
    'page_location': cleanPageLocation(),
  });
</script>

GTM

如果使用Google Tag Manager,则需要将下面的函数添加到JavaScript变量中,并将其应用于GA4配置标签中的page_location字段:

过滤查询参数

function cleanPageLocation() {
    // define parameters to exclude
    var excludeStrings = [
        "Go_Away"
    ];
    var addressString = new URL(document.location);
    var queryString = addressString.search;
    
    // check if query string holds any parameters, otherwise just return the url 
    without them
    if (queryString.indexOf("?") != -1) {
        // https://stackoverflow.com/questions/901115/how-can-i-get-query-string- 
    values-in-javascript
        var getQueryParamsFromURL = function getQueryParamsFromURL() {
            var match,
                search = /([^&=]+)=?([^&]*)/g,
                decode = function decode(s) {
                    return decodeURIComponent(s);
                },
                query = addressString.search.substring(1);
    
            var urlParams = {};
    
            while ((match = search.exec(query))) {
                urlParams[decode(match[1])] = decode(match[2]);
            }
    
            return urlParams;
        };
        // create param object from query string
        var urlParams = getQueryParamsFromURL();
    
        // if it holds any of the defined parameters, remove the key and keep the 
    rest
        Object.keys(urlParams).map(function (key) {
            if (excludeStrings.includes(key)) delete urlParams[key];
        });
    
        // Create filtered query string
        var queryString = new URLSearchParams(urlParams).toString();
    
        // add ? to querystring unless it's empty
        if (queryString != "") queryString = "?" + queryString;
    }
    
    // return cleaned URL
        return addressString.origin + addressString.pathname + queryString;

参考:https://bluerivermountains.com/en/ga4-query-parameter-exclusion

相关问题