rauberdaniel rauberdaniel - 6 months ago 56
Javascript Question

Is there a way to disable Google Analytics tracking in PhantomJS?

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).


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']);

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