How do you block Google Analytics from Selenium automated visits?



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



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

    https://github.com/lightbody/browsermob-proxy

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

    http://element34.ca/blog/proxy-executor

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

    @derk 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2