Is there a way to stop Google Analytics counting development work as hits?

后端 未结 22 1663
终归单人心
终归单人心 2020-12-02 04:51

I have added the JavaScript that I need to the bottom of my pages so that I can make use of Google Analytics. Only problem is that I am sure that it is counting all my devel

相关标签:
22条回答
  • 2020-12-02 05:46

    Just as an additional option for this, I have a development server with lots of different sites and developers. This meant that I wasn't particularly happy with the 3 main options

    • hosts file- problematic with lots of developers and open to human error
    • if/else development block on every site etc
    • configuration on GA website - some clients have their own GA accounts; would have to be completed on every site with the potential to be forgotten/overlooked

    Rather than implementing the various options in the other answers here I approached the problem in the following way. In the global httpd.conf (rather than a site specific one) I used the apache module mod_substitute to simulate the effect the hosts file fix in another answer has, but for every development site, and every developer automatically.

    Enable the module

    CentOS: Open /etc/conf/httpd.conf and add the following line

    LoadModule substitute_module modules/mod_substitute.so

    Ubuntu/Debian: Run the following command

    sudo a2enmod substitute

    Once you've got the module enabled add the following lines to your httpd global config file

    CentOS: /etc/conf/httpd.conf

    Ubuntu/Debian: /etc/apache2/httpd.conf

    # Break Google Analytics
    AddOutputFilterByType SUBSTITUTE text/html 
    Substitute "s|.google-analytics.com|.127.0.0.1|n"
    

    Then restart apache

    CentOS: service httpd restart

    Ubuntu/Debian: /etc/init.d/apache2 restart

    What this does is replace all text matching .google-analytics.com with .127.0.0.1 when apache serves the page so your page renders with analytics code similar to the below example

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', '']);
    _gaq.push(['_trackPageview']);
    
    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.127.0.0.1/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
    
    0 讨论(0)
  • 2020-12-02 05:49

    I use Ad Blocker for Firefox, it can specifically block the Google analytics tracking script. Since firefox is my primary development browser it works great until i need to test my work in other browsers.

    0 讨论(0)
  • 2020-12-02 05:51

    If You are behind NAT or You can't for other reason give Your IP to Google Analytics, then the simplest method is to set the google analytics domain to localhost (127.0.0.1), from now when You open Your browser, all request to Google Analytics will be directed to Your working station, without knowledge of Google Analytics.

    0 讨论(0)
  • 2020-12-02 05:51

    get the request host variable.

    So wrap an if statement around the analytics javascript like this (Ruby-esque pseudocode):

    <body>
    <shtuff>dfsfsdf</shtuff>
    if not (request.host == 'localhost')
      #analytics code here
    elsif (request.host == the server's ip/domain)
      #analytics code here
    else
      #do nothing
    end
    </body>
    
    0 讨论(0)
提交回复
热议问题