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

≯℡__Kan透↙ 提交于 2019-12-07 01:15:33

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/22274112/is-there-a-way-to-disable-google-analytics-tracking-in-phantomjs

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