Integrating Optimizely with Google Universal Analytics

Last updated:

This article will help you:

  • Integrate Google Universal Analytics to read data from Optimizely
  • Add Google Universal Analytics integration code to your page
  • Track Google Universal Analytics metrics as experiment goals
  • Create custom dimensions and reports in Universal Analytics to track and report on Optimizely data

If you use Google Universal Analytics (UA) to monitor your site's traffic, you can view the results of your Optimizely experiments alongside the rest of the data that UA collects. In this article, we'll walk through how to implement Google Universal Analytics.

Why integrate Optimizely and Google Universal Analytics?

  • You'll have a direct view of how Optimizely experiments affect the metrics you track in Google Analytics.
  • You'll be able to see Google Analytics data for each variation in your experiment.
  • You'll be able to filter your Google Analytics reporting by visitors who were successfully included in an Optimizely experiment and exclude those who weren't.
  • If you're running redirect experiments and plan to view your data in Universal Analytics, you'll need to turn on this integration for your traffic source information to report correctly. Otherwise, Optimizely won't be able to set a necessary variable, and your traffic will report as "Direct."
 

Tip:

Are you looking for a different Google Analytics Integration? If so, check out our article on implementing Google Analytics Classic or Google Analytics for iOS. Or see our Google Tag Manager article.

For more on the subject, check out these great writeups by our Solutions Partners SwellPath and Vertical Nerve; and Geoff Kenyon.

Implementing Optimizely and Google Analytics in a tag manager? Read our tag manager article to find out more about how this will affect your GA tracking code.

Are you troubleshooting your Universal Analytics integration, or see discrepancies in your reports? See our GA troubleshooting article for more information.

 

Prerequisites

Begin by going to the Integrations tab on the Home page. Select Universal Analytics and toggle the button to ON in the details sidebar to the right.

Google Analytics

To enable Google Analytics for an individual experiment, go to that experiment in the Editor, then click Options > Integrations.

  • In order to integrate Optimizely with Universal Analytics, you must have an available Custom Dimension (note: this refers to a Custom Dimension in Google Analytics, not the Optimizely feature) to populate with Optimizely experiment data.
  • The Universal Analytics tracking code must be placed at the bottom of the <head> section of your pages. Google Analytics integration will not function properly unless the Optimizely snippet is above the GA snippet.
  • You will use the number (index) of the Custom Dimension to configure your Optimizely experiment to send data to. See below to learn how to access your list of Custom Dimensions in Universal Analytics and create one for Optimizely if needed.
  • Use one Custom Dimension per concurrently running experiment so that their experiment names/variations don't overwrite each other. 
  • Make sure that if you have the setDomainName function call on your page it is not using a leading period or www.
  • Make sure that your Optimizely experiment is running for this integration to work.
 

Note:

As long as you're testing on the same domain and Optimizely is implemented on the experiment page, you can track session-wide goals in Google Analytics.

 

Enable Universal Analytics Integration

Optimizely uses Universal Analytics' "Custom Dimensions" to tag your visitors with the experiments and variations to which they've been added. Configuring Optimizely to begin sending this information to Universal Analytics requires three steps:

  1. Add the following JavaScript code to your site wherever the Universal Analytics code exists after the ga('create'...) function fires and before the Universal Analytics ga('send','pageview') function fires and the tracking call is made (see details in the next section):

     // Optimizely Universal Analytics Integration
     window.optimizely = window.optimizely || [];
     window.optimizely.push("activateUniversalAnalytics");
    
  2. In the Optimizely Editor, in your experiment, go to Options > Integrations then click the Universal Analytics checkbox to enable the integration.



     

    Important:

    By enabling this integration, you actually replace the need to include the UA setDimension code on your page. If you include the ga('set', 'dimension1', dimensionValue); code on your site, you may actually end up overwriting the Optimizely/UA integration.

  3. Select a Custom Dimension that you'd like Optimizely to use. Note that this is a UA Custom Dimension, not the Optimizely feature. This Custom Dimension should not already be in use by another part of your site, or by another currently-running Optimizely experiment.

  4. Specify a Custom Tracker if you are using a custom event tracker other than the default. This will change Optimizely's integration call to use the custom tracker rather than the default. For example, if your website is using the call:

    ga('tracker2.send', 'pageview');

    then you would enter tracker2 into the Specify a custom tracker field, and Optimizely would integrate with tracker2 instead of the default tracker.
 

Note:

The Optimizely API call in step #1 will set custom dimension values for all active Optimizely experiments that are configured to use the Universal Analytics integration and is safe to make even if no experiments are currently configured to use it.

Essentially, you can add this code sitewide, once, and never have to change it again.

The only exception is if you have experiments using manual or conditional activation. If you're using these activation modes, you may need to re-activate UA (step 1) and send a second Google Analytics pageview call after the experiment is activated.

Place the API Call

We recommend adding this code to your site as the last call that is made before the pageview, especially if you are using the cookieDomain function.

The latest tracking code can be obtained from Google Analytics by navigating to the Admin tab, then clicking Tracking Info > Tracking Code

A sample setup might look like this in the body of your HTML:

<script>
//Universal Analytics Setup
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXX-Y', 'auto');

// Optimizely Universal Analytics Integration Code
window.optimizely = window.optimizely || [];
window.optimizely.push("activateUniversalAnalytics");

// End Optimizely Code
ga('send', 'pageview');
</script>
 

Create a Custom Dimension

In order to integrate Optimizely with Universal Analytics and view reports based on the integration data, you must configure a Universal Analytics custom dimension first. This dimension, and the name you give it, will be used to create reports that leverage your Optimizely integration data for filtering.

  1. While logged in as an Admin, click the Admin tab at the top and you should be brought to a page like this:



  2. Click the Custom Definitions link under the middle Property column and then select Custom Dimensions. You should be brought to a tab like this: 



  3. If the Custom Dimension you want to integrate your Optimizely experiment with is listed on this tab (the 'dimension number' is shown in the Index column), then you can just take note of the name for that Dimension and don't need to add one.

  4. If the Custom Dimension is not listed on this tab, you'll need to create a new Custom Dimension for use with Optimizely. To do this, click the New Custom Dimension button and give the New Dimension a name and a Scope. For more information on the recommended scope, see the "Note" box below:


     

    Note:

    While our recommended integration is scoped at the session level, you can also configure your integration to be scoped at the user level.

    Ultimately, you'll need to choose the scoping that's better for the types of conversions you want to track. For instance, session-level scoping may be better if the experiment is running sitewide or if a visitor will always go back through the experiment pages before converting. But user-level scoping may be better if it's possible for a user to leave the site, then come back and convert without seeing the experiment again.

    Our default integration uses session scope to minimize the possibility of overwriting your experiment data sent to Google Analytics, but if you're tracking complex conversions that span multiple sessions, you may prefer to scope at the user level. For more information, see Google's documentation on scoping.

    If you do this, just be cautious of reusing Custom Dimensions in Google Analytics, and for consistency between Google Analytics and Optimizely's Results page, set the Google Analytics reporting range to the duration that your experiment ran.

  5. Click Create and your new Custom Dimension will be available for integrating with Optimizely experiments.

Create a Custom Report

Now, you'll need to see reporting on your experiment within Universal Analytics.

  1. Log into your Universal Analytics account and click the Customization tab at the top. You should see a Custom Reports list that looks something like this: 



  2. Set up a Custom Report for each experiment you have integrated Universal Analytics with. To do this, click New Custom Report. Then give the report a title and add the metric groups you wish to view in the report.

  3. To filter this report for only your Optimizely experiment, choose the Custom Dimension you set up previously as one of the Dimension Drilldowns. See the below image for an example.

  4. Add this dimension in the Filters section and use a Regex match on the experiment ID for the experiment you want to filter. This will ensure that only Universal Analytics data for that particular experiment is shown in your report:



     

    Important:

    Make sure to leave Filters empty when creating a custom report for an iOS experiment.

     

  5. Click Save. You'll then be able to access your report and update the date filter if you wish:

Check your integration

GA’s Custom Dimensions contain visitor-specific information that can be sent along with events and pageviews. It can take up to 24 hours to populate that data into GA; however, the Custom Dimension is set immediately once you integrate GA and Optimizely, so you can check that the Custom Dimension is set correctly using Google Analytics’ debugger tool.

Using the debugger tool, look for the variable cd[x] where ‘x’ is the slot corresponding to the Optimizely experiment. The variable will be set with the Optimizely experiment followed by the variation you were bucketed into.

An example would look like this:

dimension7 (&cd7) Optimizely_Onboarding (1645002506): Variant#1
 

Tip:

Have a question about integrating Optimizely with your Analytics Platform? Head over to the Optimizely Community to post a discussion and see what others are talking about.

Comments