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

百般思念 提交于 2019-12-05 04:33:31

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!