Is there a way to turn off Google Analytics tracking in PhantomJS?

I want to track some sites using PhantomJS, but I do not want to spam Google Analytics people. Is there a way to disable the tracking of the Google Analytics script (ga.js / analytics.js) from sending data to Google? How is this possible with regular GAOptOut browser plugins.

I looked in Chrome Plugin and tried the code, but it does not seem to execute when PhantomJS reports this (onLoadStart).

+7
javascript phantomjs google-analytics
source share
2 answers

Use the page.onResourceRequested method to abort all requests in 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(); } }; 

A related, complete example: https://github.com/ariya/phantomjs/blob/master/examples/loadurlwithoutcss.js

+15
source share

I just did some testing with viewing Googles Analytics in real time. The Filter Bots option seems to filter out PhantomJS.

I use PhantomJS as part of BackStopJS - but I believe that they also use the PhantomJS stock.

Before hacking workarounds, as I was going to, check if you are all filtered out.

Another solution that I came across, if this does not work for you, was to block Google Analytics on your test machine with:

 127.0.0.1 www.google-analytics.com 127.0.0.1 google-analytics.com 
0
source share

All Articles