Beefing Up the Integration of Optimizely and Google Analytics
I’ve developed a pretty serious crush on Optimizely as an A/B and multivariate testing platform — it’s hard to beat the ease-of-deployment and ease-of-use. Just like any platform — web analytics, tag management, voice of the customer, testing, or otherwise — that is implemented with “just one line of Javascript,” there are some limitations as to what can be achieved without any additional tweaks to the code on your site. But, still, the emergence of “leveraging the DOM” has been a boon to the world of analytics and optimization. And, while, at Clearhead, we are platform-agnostic when it comes to testing technology, this post is about Optimizely, which we’ve found certainly shines in certain situations.
Coming from a web analytics background, I tend to want almost any technology that relates to my site to be hooked in to the site’s web analytics. It doesn’t matter what your testing platform is or what your web analytics platform is, you’ll almost certainly want to link them up (Exhibit 1: Bryan Hawkins just wrote a great post detailing how to push Adobe Test&Target data into Google Analytics.)
What’s the Point of Integrating?
First, let’s define “integration” in the context of this post:
Integration means passing information into your web analytics tool that tells the tool if a visitor was exposed to a certain test, and, if they were, to which variation of the test they were exposed.
By enabling this integration, you enable segmentation of your traffic based on the different test variations, and you can do side-by-side comparisons of visitor behavior and any metric based on which variation of the test the visitor experienced. This is similar, in some ways, why you put campaign tracking parameters on links from banner ads to your sites: you get powerful data to augment the impression, clickthrough, and conversion (if tracking pixels are implemented) metrics provided by the media server.
The depth of out-of-the-box integration is one of the selling points for using testing and web analytics solutions from the same company (Adobe, Google, or Webtrends), but it is always possible — and not all that difficult — to enable a link between any testing technology and any web analytics platform.
This Post Just Covers Optimizely –> Google Analytics
Recently, we had a client that was using Optimizely and Google Analytics…and we quickly ran into some limitations of the out-of-the-box integration, which is well-documented by Optimizely. What the integration does is populate a visit-scope Google Analytics custom variable: the variable key is the name of the experiment, and the variable value is the variation that was served to the visitor. (My favorite explanation of Google Analytics custom variables, including what “visit-scope” means and the implications therein, is the first one Justin Cutroni wrote on the subject).
One shortcoming to Optimizely’s documentation is that, when it comes to using the integration, it only details how to build an advanced segment based on the custom variable. Custom variables also have their own reporting, including many standard metrics, available under Audience » Custom » Custom Variables, and custom variables can be used in custom reports. So, the post stops short of providing sufficiently deep documentation on how to get to custom variable data within Google Analytics.
But…the bigger issue are two limitations of the integration itself.
First, there is the fact that the free version of Google Analytics only has five custom variables available. If you’re not already using custom variables, then that means you can run five Optimizely experiments concurrently…so you’re probably fine. But, if you are using custom variables (to track registered users, logged in users, returning customers, page type, or any of the slew of other valuable uses), well, you should be! (Justin actually has written multiple posts with specific suggestions on that front.) With Optimizely’s standard Google Analytics integration, you can only integrate as many concurrent experiments as you have available custom variables.
The other limitation is related to the “just one line of Javascript!” implementation of Optimizely. Yes, it is one line of Javascript, but, depending on your site and the conversion goal for your experiment, you may have to implement additional Javascript on your site to pass data back to Optimizely (for instance, if you want to track revenue by test variation, you have to implement Javascript on your order confirmation page to tell Optimizely how much revenue was in the order). In many cases, that information is data that is already being captured by Google Analytics!
In the case of this client, we were going to have to implement additional Javascript on the site in order to track orders, and we had only one available custom variable and three concurrent experiments. In short, we were effectively up a creek with a spork for a paddle!* Luckily, we also had a couple of passable analytics (me) and javascript (Ryan Garner) woodworkers and a plank of (digital) wood.
A More Flexible Integration Approach
The approach we took allows an unlimited number of experiments to be run concurrently (of course, the more experiments there are running at the same time, the more risk there is that experiments will overlap, which has implications for test duration and, in some cases, results interpretation for individual tests). We did this using:
- Optimizely’s Global Javascript feature — the ability to implement a piece of Javascript across all pages in the experiment, including the original page and every variation; Optimizely has the capability documented, and even references in the documentation that its primary use is for integration with “analytics services.”
- Google Analytics Non-Interaction Events — a way to pass test information into Google Analytics without affecting any web analytics metrics (more on non-interaction events can be found in the Google Analytics documentation for event tracking)
All we needed to do — and we admit we based this largely on the example in Optimizely’s documentation, was go to Options » Global Javascript in each Optimizely experiment and paste in the following code:
[Update: The original code snippet included in this post worked…but could occasionally cause the dreaded “flicker” when the page loaded. The snippet has been updated as of 30-Nov-2012 to both use some Optimizely special markup to force the code to evaluate immediately as soon as it loads, and it now checks for _gaq before using it so as to not cause any Javascript errors.]
/* _optimizely_evaluate=force */
setTimeout(function(){
experimentId =
if (typeof(optimizely) != "undefined" &&
optimizely.variationMap.hasOwnProperty(experimentId)) {
window._gaq = window._gaq || [];
_gaq.push(['_setAccount', '
_gaq.push(['_trackEvent', 'Optimizely', optimizely.data.experiments[experimentId].name, optimizely.variationNamesMap[experimentId], 1, true]);
}},1000);
/* _optimizely_evaluate=safe */
With this code, a non-interaction event gets sent to Google Analytics each time the experiment is displayed. That event has a Category of “Optimizely,” an Action that is the name of the experiment, and a Label that is the variation of the experiment that was displayed.
With the event values sent to Google Analytics, you can explore visitor behavior for each version of the test to which the visitor was exposed:
- By building custom segments for each variation (use the Action and Label values to isolate visitors who were exposed to each variation of the experiment)
- By drilling down on Content » Events » Top Events » Optimizely and analyzing site usage and/or Ecommerce metrics
- By creating a custom report that breaks out the experiment variations as warranted with specific metrics of interest
Of course, before you go too nuts with your in-Google Analytics analysis, be sure to do a quick cross-reference with the Optimizely Results page for the experiment to make sure Google Analytics and Optimizely are within the same ballpark when it comes to how many times each variation of the test has been served (use the “Unique Events” or “Visits” metric in Google Analytics). They will never match, as they are capturing the test counts in very different ways, but they should be within spitting distance of each other. Be sure you have both Optimizely and Google Analytics set up to use the same timezone!
That’s all there is to it! Happy test analyzing!
*If you’re not familiar with the “up a creek” reference, see this link.