Beefing Up the Integration of Optimizely and Google Analytics

By on in with 7 Comments

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 */
experimentId = <Numeric ID from the URL of the experiment editor>;
if (typeof(optimizely) != "undefined" &amp;&amp;
optimizely.variationMap.hasOwnProperty(experimentId)) {
window._gaq = window._gaq || [];
_gaq.push(['_setAccount', '<Google Analytics web property ID>']);
_gaq.push(['_trackEvent', 'Optimizely',[experimentId].name, optimizely.variationNamesMap[experimentId], 1, true]);
/* _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.



Similar Posts:


  1. Hi,

    i stumbled upon this entry because we have different numbers in Analytics and in Optimizely – after weeks of tweaking just today i thought of the timezones and how they could be a problem.

    When you say “Be sure you have both Optimizely and Google Analytics set up to use the same timezone!” – how did you do this in Optimizely? I cannot find an option to set the timezone anywhere and also the Optimizely documentation produces no results when looking for “time zone”…


  2. Hi Gerald,

    I hadn’t run into a timezone difference myself, so this got me to poking around…with the same result. I couldn’t find it in Optimizely. That led me to ping Optimizely support, who responded: “We take the timezone from the user’s machine, so the only way to really change the timezone on the results page is to change the system settings.”

    That’s interesting. So, it also means that, if you’re working with people in different time zones who are accessing the platform as well, you will likely see slightly different results by day (or dramatically different results if you’re on different continents!).

  3. Hi Tim. Is that code still current? I tried placing it in the Global JS and the Optimizely UI threw an error when trying to evaluate it.

    Error: Syntax Error; unexpected token (col 1, line 1)

    Is this normal? Thanks!

  4. Merritt,

    I actually haven’t had a client using Optimizely and GA for a while, so I, unfortunately, don’t know if this is entirely current. It’s not doing anything all that crazy, so I’d be surprised if it was a rev on Optimizely’s side that is causing the issue.

    Also, based on the fact that it’s reporting a Syntax Error seems odd. You’re not including the “< >” in the experimentID, are you?

    It might be worth something sending to Optimizely support — the specific code you’re using so they can see it specific to your account.

    (If you can report back on how it works out, I’d appreciate. If this is no longer valid, I’ll take down the post)

  5. A slight tweak seems to have resolved the error.

    Replace: &&
    With: &&

    Working like a champ for me, though I haven’t yet tested the visitor counts between GA and Optimizely.

    Thanks for posting this!

  6. Merrit,

    What did you replace and for what exactly? I bumped into the same problem and would love to get it fixed 🙂

    Thanks in advance for you & thanks for your post Tim!

  7. Just figured it out:

    Replace: “&&”
    With: “&&”

    (without quotation marks).

    Thanks again!

Leave your Comment

« »