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

后端 未结 22 1768
终归单人心
终归单人心 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);
    })();
    

提交回复
热议问题