有没有办法在PhantomJS中禁用Google

有没有办法在PhantomJS中禁用Google

本文介绍了有没有办法在PhantomJS中禁用Google Analytics(分析)跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PhantomJS跟踪一些网站,但我不想将Google Analytics(分析)滥发给人。有没有办法禁用Google Analytics跟踪脚本(ga.js / analytics.js)向Google发送数据?就像使用通常的GAOptOut浏览器插件一样。



我查看了Chrome插件并尝试了代码,但似乎并未执行当告诉PhantomJS这样做(onLoadStart)。

解决方案使用 page.onResourceRequested
  page.onResourceRequested = function(requestData,request){
如果((/google-analytics\.com/.com/gi).test(requestData['url'])){
console.log('Request to GA。Aborting:'+ requestData ['url']) ;
request.abort();
}
};

相关完整示例:


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']);
        request.abort();
    }
};

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

这篇关于有没有办法在PhantomJS中禁用Google Analytics(分析)跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:54