Search code examples
regexfiltergoogle-analyticsquery-string

Remove empty query string parameter from Google Analytics


I was wondering if anyone knows how to remove an empty query string parameter from Google Analytics?

An example URL would be: http://www.domain.com/index.asp?a=test&b=test&c=&d=test

On this example I would want Google Analytics to filter out "c" because it's empty.

Thanks for any help that can be provided!


Solution

  • Expending upon @user1494396; refactor your async tracking code to pass the page name from the regex like so:

    (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);
    })();
    

    See: