title: Load Google Analytics Conditionally
body: <p>I need to filter internal QA people out of our analytics reporting. </p>
  
  <p>We currently have code in our site that shows/hides an information div if the visitor is of the 'student' role:</p>
  
  <pre><code>$(document).ready(function(){
  if($.inArray('student',ENV['current_user_roles']) === 1 &amp;&amp; $.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 &amp;&amp; paramArray.indexOf('settings') == -1 &amp;&amp; paramArray.indexOf('grades') == -1 &amp;&amp; paramArray.indexOf('quizzes') == -1  &amp;&amp; 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');
      }
    }
  }
  } 
  });
  </code></pre>
  
  <p>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:'</p>
  
  <pre><code>$(document).ready(function(){
  if($.inArray('student',ENV['current_user_roles']) === 1 &amp;&amp; $.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"));
  }
  } 
  });
  </code></pre>
  
  <p>I tried the above, based on what I saw around the internet [ <a href="https://gist.github.com/benbalter/902140" rel="nofollow">https://gist.github.com/benbalter/902140</a> ], but this implementation did not successfully filter out non-students.</p>
  
  <p>Any advice?</p>