7
votes

I want to track some sites using PhantomJS, but I don’t want to spam peoples Google Analytics. Is there a way to disable the Google Analytics tracking script (ga.js / analytics.js) from sending data to Google? Like it is possible with the usual GAOptOut browser plugins.

I had a look in the Chrome Plugin and tried the code from that, but it doesn’t seem to be executed when telling PhantomJS to do so (onLoadStart).

2

2 Answers

15
votes

Use page.onResourceRequested method to abort all requests to google analytics.

page.onResourceRequested = function(requestData, request) {
    if ((/google-analytics\.com/gi).test(requestData['url'])){
        console.log('Request to GA. Aborting: ' + requestData['url']);
        request.abort();
    }
};

Related, full example: https://github.com/ariya/phantomjs/blob/master/examples/loadurlwithoutcss.js

0
votes

I've just done some testing with Googles Analytics Real Time view. The "Filter Bots" option appears to filter out PhantomJS.

I'm using PhantomJS as part of BackStopJS - but I believe they're using a stock PhantomJS too.

Before hacking on work-arounds like I was about to - check to see whether you're filtered anyway.

Another solution I came across, if that doesn't work for you, was to block google-analytics on your testing machine with:

127.0.0.1    www.google-analytics.com
127.0.0.1    google-analytics.com