admin 管理员组文章数量: 1086019
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:
What was suggested here: , 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/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.
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/analytics.js', 'ga');ga('create', '@gaToken', 'auto');
I do not have access to Tag Manager, which I thought might be an option based on the following: /
Any suggestions would be very greatly appreciated, am rather at the end of my wits :(
(Apologies for the formatting, it was not co-operating :(
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:
What was suggested here: https://stackoverflow./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./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.
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./analytics.js', 'ga');ga('create', '@gaToken', 'auto');
I do not have access to Tag Manager, which I thought might be an option based on the following: http://www.lunametrics./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 :(
Share Improve this question edited May 23, 2017 at 11:46 CommunityBot 11 silver badge asked May 24, 2016 at 16:25 Froom2Froom2 1,2792 gold badges15 silver badges26 bronze badges 3- You can also leave the tracking code unchanged an remove the query strings via a filter within the GA view. – Eike Pierstorff Commented May 24, 2016 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 Commented May 25, 2016 at 8:35
- In that case @dorians answer is the correct one (it's even part of the official docs: developers.google./analytics/devguides/collection/…) 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 Commented May 25, 2016 at 8:40
2 Answers
Reset to default 7Your 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.
本文标签: javascriptStrip query parameters from url before sending to google analyticsStack Overflow
版权声明:本文标题:javascript - Strip query parameters from url before sending to google analytics - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744012764a2518458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论