4

I am using the standard Google analytics snippet on pages to send info to google analytics.

For various reasons, my page is arrived at with a query parameter.

In an ideal world, this would not be the case but it is not a perfect world so instead I must avoid it being sent to google analytics as it contains personal information.

I have tried the following:

  1. What was suggested here: https://stackoverflow.com/a/3669333/2295284, as follows: ((I am so sorry the formatting is really not playing ball D:)

    _gaq.push(['_trackPageview', location.pathname ]);

    (function(i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject']=r;
        i[r]=i[r]||function() {
            (i[r].q=i[r].q||[]).push(arguments)
        }, i[r].l=1*new Date();
        a=s.createElement(o), m=s.getElementsByTagName(o)[0];
        a.async=1;
        a.src=g;
        m.parentNode.insertBefore(a,m)
    })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
    ga('create', '@gaToken', 'auto');
    ga('send', 'pageview', location.pathname);
    

I have been manually adding a query parameter to the url and using ObservePoint to check the content. The Content Information contains a Document Location URL of "http://localhost/my/url/page?uiop=qwert"... which appears to mean that the _gaq.push line isn't doing anything at all.

  1. I tried digging into the function and manually changing the url, but it just resulted in an endless loop of page refreshing:

    (function(i, s, o, g, r, a, m) { alert(i.location.href); var locn = i.location.href.indexOf("?") i.location.href = i.location.href.substring(0, locn) i['GoogleAnalyticsObject']=r; i[r]=i[r]||function() { (i[r].q=i[r].q||[]).push(arguments) }, i[r].l=1*new Date(); a=s.createElement(o), m=s.getElementsByTagName(o)[0]; a.async=1; a.src=g; m.parentNode.insertBefore(a,m)})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');ga('create', '@gaToken', 'auto');

  2. I do not have access to Tag Manager, which I thought might be an option based on the following: http://www.lunametrics.com/blog/2015/04/17/strip-query-parameters-google-analytics/

Any suggestions would be very greatly appreciated, am rather at the end of my wits :(

(Apologies for the formatting, it was not co-operating :(

Community
  • 1
  • 1
Froom2
  • 1,269
  • 2
  • 13
  • 26
  • You can also leave the tracking code unchanged an remove the query strings via a filter within the GA view. – Eike Pierstorff May 24 '16 at 18:41
  • Yes, but I need to prevent it going to GA in the first place because I don't have the ability to filter out our query string everywhere on ga. – Froom2 May 25 '16 at 08:35
  • In that case @dorians answer is the correct one (it's even part of the official docs: https://developers.google.com/analytics/devguides/collection/analyticsjs/pages#pageview_fields) and you need to figure out why it is not working. Is there a chance that maybe your debugger is playing tricks on you ? Try the GA debugger, or Google Tag Assistant. – Eike Pierstorff May 25 '16 at 08:40

3 Answers3

7

Your first option is the correct one, but the code you used is for a previous version of Google Analytics.

Instead, replace ga('send', 'pageview'); with ga('send', 'pageview', location.pathname); See the page tracking documentation for reference.

dorian
  • 5,667
  • 1
  • 19
  • 36
  • No joy :( I have updated option 1 in my question with the full code I have tried and the resulting content according to ObservePoint – Froom2 May 25 '16 at 08:13
  • This is how I send pageviews, to get "clean" pageview data in GA. A question I've always had - will this have any impact on traffic with utm parameters like utm_campaign and utm_medium? Or are those parameters automatically recorded by the GA snippet regardless of what is sent in `ga('send', 'pageview')` ... – wrydere Jun 15 '17 at 18:55
  • 1
    Good question. I'd say the parameters are not affected, as you override the `page` field and not the `location` field. The latter contains the full URL, `page` is only used for the path segment. But I think you'd need to run a test to know for sure. – dorian Jun 15 '17 at 20:09
  • @dorian - set up a test of this and you are correct, utm parameters are not impacted, even when only sending pathname. – wrydere Jun 20 '17 at 19:47
  • Would love to see a new answer to work with the google tag manager, which seems to be the default configuration for GA now – Simon H Jan 07 '21 at 08:11
0

You could try using Google Tag Manager: in the setup for the tag, there's an option called 'Fields to set', where you can manually edit any of the information that's being sent to Google Analytics (note that this happens locally, so would solve your data protection problem).

Here's an example where I've manually edited the 'Page field', which is what you need to do. In this case, I've chosen a pair of custom variables to feed in, but you'll probably need a custom JavaScript variable that takes the standard {{Page Path}} variable (which is what would be fed by default) and returns everything preceeding the first question mark.

Replacing the 'page' field

J Brazier
  • 864
  • 6
  • 14
  • Unfortunately we don't have access to Tag Manager :( – Froom2 May 26 '16 at 13:59
  • What do you mean when you say that you don't have access to it? It's a separate tool from Google Analytics, and you can set up an account attached to any email address. – J Brazier May 26 '16 at 14:09
  • But how does it link in with google analytics? What about if I left the team? It is not something that is used anywhere else in the organisation and I feel like there must be a simpler way of doing this up without having to set up another system to do it... – Froom2 May 26 '16 at 14:25
  • It links in with Google Analytics because you choose to deploy Google Analytics on it. From within GTM you can assign permissions to other people, and you should always use your work email for GTM accounts anyway, so that there's no risk of being left attached to something. Yes, it's a bit of a leap to GTM, but its free, and will solve your problems. – J Brazier May 26 '16 at 14:27
0

Actually, you should not exclude all parameters, because some are crucial to Google Analytics to work properly, as for example gclid-parameters or utm_-parameters.

Page location without params when you know the query parameter

  • first, create the JS function cleanPageLocation() before the Google Analytics snippet runs. Add the names of the parameters to exclude to the excludeStrings array.
  • Then add it to the GA snippet as shown below
<script>
       function cleanPageLocation() {
    // define parameters to exclude
    var excludeStrings = [
        "lname",
        "fname"
    ];
    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;
    }
</script>

Page location without params when you don't know the query parameter to exclude

  • in that case you exclude all query parameters, except the ones that Google Analytics needs: gclid, anything with utm_ and maybe gtm_debug

  • update the includeStrings array as needed

     <script>
    function cleanPageLocation() {
        // define parameters to keep if available
        var includeStrings = [
            "gclid",
            "utm_",
            "gtm_debug"
        ];
        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) {
            // transpile ES2016 => ES2015
            var _defineProperty = function (obj, key, value) {
                if (key in obj) {
                    Object.defineProperty(obj, key, {
                        value: value,
                        enumerable: true,
                        configurable: true,
                        writable: true
                    });
                } else {
                    obj[key] = value;
                }
                return obj;
            };
    
            /* 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;
            };
    
            var filterParamsFromList = function filterParamsFromList(obj, list) {
                var urlParamKeysFinal = [];
                var urlParamKeys = Object.keys(obj);
    
                /* test each param for availability and create array with final keys */
                for (var i = 0; i < list.length; i++) {
                    urlParamKeysFinal.push(
                        urlParamKeys.filter(function (key) {
                            return key.includes(list[i]);
                        })
                    );
                }
    
                // merge all keys into one list
                /* https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays */
                urlParamKeysFinal = [].concat.apply([], urlParamKeysFinal);
                return urlParamKeysFinal.reduce(function (cur, key) {
                    return Object.assign(cur, _defineProperty({}, key, obj[key]));
                }, {});
            };
    
            // create param object from query string
            var urlParams = getQueryParamsFromURL(); /* Create filtered query string */
    
            queryString = new URLSearchParams(
                // remove any non-matching keys from param object
                filterParamsFromList(urlParams, includeStrings)
            ).toString();
    
            // add ? to querystring unless it's empty
            if (queryString != "") queryString = "?" + queryString;
        }
    
        // return cleaned URL
        return addressString.origin + addressString.pathname + queryString;
    }</script>
    

Add cleanPageLocation() to Google Analytics snippet

(function(i, s, o, g, r, a, m) {
    i['GoogleAnalyticsObject']=r;
    i[r]=i[r]||function() {
        (i[r].q=i[r].q||[]).push(arguments)
    }, i[r].l=1*new Date();
    a=s.createElement(o), m=s.getElementsByTagName(o)[0];
    a.async=1;
    a.src=g;
    m.parentNode.insertBefore(a,m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', '@gaToken', 'auto');
ga('send', 'pageview', cleanPageLocation());

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