Basically, we have a javascript integration on client's webpages. We don't have much (or any) control of the deployed environment, hence the only way to test our integration is truly working is using browser automation on the client's webpage. We don't want to interfere with our tracking and analytics, and more importantly, the client's analytics (which is very important to verifying their ROI). What is the most convenient and effective way to block Google Analytics in the context of selenium browser automation?

I have found a few approaches:

  1. Google Analytics Opt Out Browser Add On

  2. Editing the hosts file to redirect Google Analytics to localhost (I'm not too happy about this solution since in the past it has broken some of our integration files)

  3. Other browser specific extensions, Block yourself from GA extension for Chrome and Google Disconnect for Firefox.

6 Answers 6


Browsermob-proxy could be used to blacklist the GA hosts, so the code isn't even loaded.


It integrates well with Selenium.

Here is a general purpose talk describing using BMP to blacklist cruft that can slow down automation (Facebook & Twitter widgets are specifically mentioned, but I'm sure GA can also be blacklisted):


And here is a great guide for easily using browsermob-proxy with Selenium in C#. And the regex for blacklisting every request except the ga.js file (this way you can test GA are working and pages won't break that assume the file exists).

  • 1
    Yes. A proxy is is absolutely the way to go about it. Especially when I need to do this with other domains. I'll edit and paste the code that get's it working in C# when I get it working.
    – craastad
    Sep 25, 2013 at 14:57

It's not very well documented, but you can also implement request blocking by passing the host-resolver-rules option to chrome and mapping the domain to localhost or an invalid IP. Something like this should work for you:

        ChromeOptions options = new ChromeOptions()
        options.addArguments("--host-resolver-rules=MAP www.google-analytics.com")
        def driverInstance = new ChromeDriver(options)
  • Thank you so much for this! One thing to note, multiple host mappings can be specified if separated by a comma ,
    – Jezor
    Nov 20, 2018 at 16:54
  • Perfect, this made my day! Works with Chrome headless. It is possible to use wildcards. Apr 10, 2019 at 18:38
  • @David Do we have something similar for IOS Safari or Mac Safari?
    – idrisjafer
    Sep 16, 2021 at 7:27

You should use the functionality of google analytics to exclude you IP addresses. This is covered in the online help here.

  • 1
    This is unfortunately not an option here. We don't have access to our client's GA and can't reliably (or functionally) communicate to them to filter them way. Plus these will be used in automated tests running on many IP addresses locally and on cloud services.
    – craastad
    Sep 24, 2013 at 15:13

I found a pretty simple way to do it for Chrome and Firefox using the Google Analytics Opt out Add ons, which are maintained for basically all browser. Refering to this SO question about browser plugin testing with selenium, I was able to open a GA/free selenium session with the following code for C#

var options = new ChromeOptions();
options.Proxy = proxy;
var driver = new ChromeDriver(options);


var profile = new FirefoxProfile();
var driver = new FirefoxDriver(profile);

after copying the extension/addon to working directory. Google Chrome extension .crx files can be downloaded using the help of this SO post.

Unfortunately with this approach for IE, we are at the mercy of Selenium implementing add on support for IE Driver, which is probably a hopeless case.... If anyone can think of a solution for IE, that would be amazing.

  • Haven't tried it, but I bet you could use the fiddler API to block any traffic to the google analytics servers during your tests. Or maybe even just add an entry to your hosts file to have any traffic sent to google analytics servers to another IP address instead.
    – Sam Woods
    Sep 24, 2013 at 18:21
  • I know about the entry to the host file trick (as mentioned above). It just doesn't seem like a desirable solution (but i guess i don't have any justification why).
    – craastad
    Sep 25, 2013 at 7:05
  • The Fiddler API sounds like a great idea. I did look into the Fiddler API into how to actually test the analytics calls weren't being sent, but compared to just looking at Fiddler's output, it wasn't worth the effort atm.
    – craastad
    Sep 25, 2013 at 7:57

You could simply just add the opt-out cookie to the browser. This answer may help.


Based on this link, I found this solution to work for Selenium Java:

ChromeOptions chromeOptions = new ChromeOptions();

// block all domains, but not *.company.com
chromeOptions.addArguments("--host-resolver-rules=MAP * localhost, Exclude *.company.com");

final ChromeDriver driver = new ChromeDriver(chromeOptions);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.