In our current client project, we are using Google Analytics to track custom events of what users are doing on our website.
To make sure, this is working in the way we expect, we want to write functional tests to check if the events are send to Google Analytics when we expect them.
In this article, I will describe how we created a solution were the events sent are made visible on the website in a ‘debug mode’. They are captured and tested against in our Cucumber Test suite using Capybara and Poltergeist.
Testing Google Analytics Custom Events
Custom Events in Google Anayltics are a great way to enhance your usage statistics by tracking events specific to your website. However, as they are specific to your web page, they require you to write custom logic and call ga.js when done: gaq.push(‘trackEvent’, ‘Contract’, ‘scrollToEnd’, ‘landLine’).
This means you will want to test this behaviour. So, let’s assume for this article you want to satisfy a Cucumber behaviour like this (./features/google-analytics.feature):
[code] Feature: Google Analytics Tracking
Scenario: Reading land line contract Given I am on the “/contracts/land-line” page And I scroll to the bottom GA should get an event {“Contract”, “scrollToEnd”, “landLine”}
1 2 3 4 5 6 7 8 9 10 11 12 |
|
javascript
var Example = Example || {}; Example.Analytics = Example.Analytics || {}; var gaq = gaq || [];
Example.Analytics.push = function () { gaq.push.apply(gaq, arguments); }
1 2 3 4 |
|
html
1 2 3 4 |
|
javascript
Example.Analytics.is_debug = window.location.href.match(/[\?&]google-analytics-debug([&#]|$)/);
$(function () { if (Example.Analytics.is_debug) { $(“#google-analytics-debug”).show(); } });
1 2 3 4 |
|
javascript
Example.Analytics.push = function () { gaq.push.apply(gaq, arguments);
if (Example.Analytics.is_debug) { var args = arguments[0]; $(function () { var li = $(“
”).text(JSON.stringify(args)); $(“#google-analytics-debug ol”).append(li); }); } }1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Next add the following lines to ./support/env.rb
[code language=“ruby”] require “capybara” require “capybara/cucumber” require ‘capybara/poltergeist’
require “json” Capybara.app_host= ‘localhost:4567’ Capybara.default_selector= :css Capybara.default_driver = :poltergeist
include Capybara::DSL
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Finally implement the steps for the cucumber feature above:
[code language=“ruby”] Given /^I am on the “([^”]*)“ page$/ do |path| visit "http://localhost:4567/#{path}?google-analytics-debug” end
When /^I scroll to the bottom$/ do page.execute_script “window.scrollBy(0,100000)” end
Then /^GA should get an event {“([^”])“, ”([^“])”, “([^”]*)“}$/ do |category, action, value| ga_pushes.should include([”_trackEvent", category, action, value]) end
```
When you run the Cucumber feature you should see a passing test result.
Discussion
The solution I propose in this article to test Google Analytics Custom Events consists of two parts:
Making Google Analytics Events visible with JavaScript
Reading them from Cucumber tests
The underlying assumption of this is, that the ga.js library actually works - which I think you can. The other assumption is, that the first line of your Example::Analytics.push function does work as well. You have to make sure, this is true by code review and by manually testing using the ga_debug.js library or Chrome extension.
A nice benefit of this mechanism is, that you can see what Google Analytics Events are send even when you are doing development or manual testing.
On the down side, because of the get parameter, you have slightly different code in production than development and testing. You have to make your own judgement if you want to make this trade-off.
Another thing we discussed is, that people will be able to actually turn on the debugging in production as well. We don’t see a security issue here, because this will only tell people what they could already know, e.g. when using the Google Analytics Debugger Chrome extension.
All code presented in this article can be found at https://gist.github.com/3600363.