Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip query parameters from url before sending to google analytics

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 :(

like image 987
Froom2 Avatar asked Mar 12 '23 05:03

Froom2


1 Answers

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.

like image 152
dorian Avatar answered Apr 25 '23 17:04

dorian