简体   繁体   中英

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

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.

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.

替换“页面”字段

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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