Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 389
rauberdaniel Avatar asked Mar 08 '14 19:03

rauberdaniel


People also ask

Can Google Analytics be turned off?

To control who uses Analytics in your organization Sign in using your administrator account (does not end in @gmail.com). Google Analytics. Click Service status. To turn on or off a service for everyone in your organization, click On for everyone or Off for everyone, and then click Save.

Should I turn off Google Analytics?

Websites can still track what pages you visit on their site though by using server logs. Secondly, preventing Google from having information on websites you visit is a large reason to start opting out of Google Analytics.

Why does Google Analytics keep tracking me?

Google uses personal data from Google Analytics, Global Site Tag, and from their many other trackers and products, so they can target you with advertising and content they think you'll want to see.


1 Answers

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

like image 151
lukaszfiszer Avatar answered Nov 15 '22 00:11

lukaszfiszer