简体   繁体   English

在发送到Google Analytics之前从网址中删除查询参数

[英]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. 我在页面上使用标准Google分析代码段将信息发送到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:) 这里有什么建议: https//stackoverflow.com/a/3669333/2295284 ,如下:((我很抱歉格式化的确不是玩球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. 我一直在手动向网址添加查询参数,并使用ObservePoint检查内容。 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. 内容信息包含文档位置URL“ http:// localhost / my / url / page?uiop = qwert ”...这似乎意味着_gaq.push行根本没有做任何事情。

  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'); (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)})(窗口,文档,'脚本','//www.google-analytics.com/analytics.js','ga'); ga ('创造','@ gaToken','汽车');

  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/ 我无法访问Tag Manager,我认为可能是基于以下内容的选项: 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. 您的第一个选项是正确的选项,但您使用的代码适用于以前版本的Google Analytics。

Instead, replace ga('send', 'pageview'); 相反,替换ga('send', 'pageview'); with ga('send', 'pageview', location.pathname); 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). 您可以尝试使用Google跟踪代码管理器:在代码设置中,有一个名为“要设置的字段”的选项,您可以在其中手动编辑发送给Google Analytics的任何信息(请注意,这是在本地发生的,因此会解决你的数据保护问题)。

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. 在这种情况下,我选择了一对自定义变量来输入,但您可能需要一个自定义JavaScript变量,该变量采用标准{{Page Path}}变量(默认情况下会输入该变量)并返回一切都在第一个问号之前。

替换“页面”字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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