Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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

@"http://www\.google-analytics\.com/(?!ga\.js).*"
share|improve this answer
    
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 '13 at 14:57
add comment

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;
options.AddExtension(Path.GetFullPath("chrome-ga-optout-extension.crx"));
var driver = new ChromeDriver(options);

and

var profile = new FirefoxProfile();
profile.AddExtension(Path.GetFullPath("firefox-ga-optou-addon.xpi"));
profile.SetProxyPreferences(proxy);
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.

share|improve this answer
    
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 '13 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 '13 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 '13 at 7:57
add comment

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

share|improve this answer
    
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 '13 at 15:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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