0

I need to filter internal QA people out of our analytics reporting.

We currently have code in our site that shows/hides an information div if the visitor is of the 'student' role:

$(document).ready(function(){
if($.inArray('student',ENV['current_user_roles']) === 1 && $.inArray('student',ENV['current_user_roles']) === 1 ){
  if ($.inArray('teacher',ENV['current_user_roles']) == -1 ){
  paramArray = window.location.href.split('/');
  if (paramArray.indexOf('assignments') == -1 && paramArray.indexOf('settings') == -1 && paramArray.indexOf('grades') == -1 && paramArray.indexOf('quizzes') == -1  && paramArray.indexOf('users') == -1){ 
    var l = $('#right-side-wrapper a.edit_link.button.button-sidebar-wide'); 
    if(l===null || l.length===0){
      $('body').removeClass('with-right-side');
    }
  }
}
} 
});

I am not well-versed in JavaScript, but it seems like there should be a simple way to re-use this code, but wrap the google analytics tracking code inside, and only load it if the user is of the role 'student:'

$(document).ready(function(){
if($.inArray('student',ENV['current_user_roles']) === 1 && $.inArray('student',ENV['current_user_roles']) === 1 ){
  if ($.inArray('teacher',ENV['current_user_roles']) == -1 ){
    var _gaq=[["_setAccount","UA-xxxxxxxx-1"],["_trackPageview"]];
    (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1;
    g.src=("https:"==location.protocol?"//ssl":"//www")+".google-analytics.com/ga.js";
    s.parentNode.insertBefore(g,s)}(document,"script"));
}
} 
});

I tried the above, based on what I saw around the internet [ https://gist.github.com/benbalter/902140 ], but this implementation did not successfully filter out non-students.

Any advice?

4

2 回答 2

1

您的代码的一个问题_gaq是它将成为一个局部变量,并且与_gaqga.js 加载的对象不同。因为_setAccount&_trackPageview不在 global 中_gaq,所以不应该跟踪任何内容。

页面中的任何位置是否还有另一组分析代码?

建议:

  • 将加载 Google Analytics 的代码放在页眉中,但省略_setAccount&_trackPageview部分。
  • 在页面加载时,有条件地推送_setAccount&_trackPageview命令。

在页眉中,类似于:

<script type="text/javascript">
var _gaq = _gaq || [];
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

在页面加载时

$(document).ready(function(){
  if($.inArray('student',ENV['current_user_roles']) === 1){
    _gaq.push("_setAccount","UA-xxxxxxxx-1");
    _gaq.push("_trackPageview");
  } 
});
于 2013-10-02T19:35:55.190 回答
0

也许您可以尝试使用 Cookie。在您的 QA 工作站上简单地设置一个 cookie,如果设置了 cookie,则不会调用 Google 代码。

于 2013-10-02T18:25:00.450 回答