- Optimizely X Web Experimentation
- Optimizely X Web Personalization
- Optimizely X Web Recommendations
- Optimizely Classic
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 Google 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 Google UA collects.
Here are a few reasons to integrate:
- Get a direct view of how Optimizely experiments affect the metrics you track in Google Analytics
- See Google Analytics data for each variation in your experiment (or experience in a campaign)
- Filter your Google Analytics reporting by visitors who were successfully included in an Optimizely experiment and exclude those who weren't
Integrate Google UA with Optimizely X Web
We've updated the Google UA integration for Optimizely X Web. Now it's even easier to integrate Optimizely with Google UA!
Here's what you need to know:
In Optimizely X Web, you can choose any order for the Optimizely and Google UA snippets.
Optimizely X Web no longer sends Optimizely data with the 'pageview'. It sends a separate 'non-interaction event', independent of the 'pageview'.
When a decision event is made, Optimizely X Web sends that event to Google UA as soon as possible, polling for availability of the Google UA object.
If the Google UA object isn't ready when a decision is made, Optimizely X Web checks back every 200 ms to see if Optimizely can send the UA string. This helps with delayed activation experiments (and if they fall under delayed activation, it helps with geo-targeted experiments too).
You no longer need to add the API call
activateUniversalAnalytics. But it's fine to leave it in your code, and it's a good idea to leave it if you're still using Optimizely Classic.
Ready to get started? First, you'll need to enable Google Analytics.
Navigate to the project's Home page and select the Integrations tab.
Select Google Analytics and toggle it On.
See how to enable integrations in Optimizely X Web.
To enable Google Analytics for an individual experiment in Optimizely Classic, navigate to the experiment in the Visual Editor, then click Options > Integrations.
Here are a few other details to keep in mind:
- To integrate Optimizely with Google UA, you'll need an available custom dimension in Google Analytics to populate with Optimizely experiment data. (The custom dimension in Google Analytics is not the same as the Optimizely feature.)
- In Optimizely Classic, place the Google UA tracking code at the bottom of the <head> section of your pages. The integration won't work properly unless the Optimizely snippet is above the Google UA snippet.
- Use the number (or index) of the custom dimension to configure and send data to your Optimizely experiment. (To access your Google UA custom dimensions and create one for Optimizely, create a custom dimension.)
- Use one custom dimension per concurrently running experiment or campaign to keep experiment names and variations from overwriting each other.
- If you have the setDomainName function call on your page, make sure that it doesn't use a leading period or "www".
- Your Optimizely experiment or campaign must be running for Google UA integration to work.
- 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 UA.
The reason for having one custom dimension per concurrently running experiment is data attribution.
For example, suppose Experiment A and Experiment B both use Custom Dimension 1. If a visitor lands on Experiment A within page 1 and lands on Experiment B on page 2 within the same session, the visitor's actions are attributed only to Experiment B from that point on.
In the same scenario, if Experiment A and Experiment B
have their own unique custom dimensions, Experiment A will continue to collect data for the visitor even after they're bucketed into Experiment B.
Enable Google UA integration
Optimizely uses Google UA's custom dimensions to tag your visitors with the experiments and variations to which they've been added. Both Optimizely Classic and Optimizely X Web require the below steps. Optimizely Classic requires an extra step, to integrate: place the API call code.
Here are the three steps in configuring Optimizely to start sending this information to Google UA:
In the Campaign Overview, click Options > Integrations and select the Universal Analytics checkbox.
Select the custom dimension that you want Optimizely to use. Remember, this is a Google UA custom dimension, not the Optimizely feature. Make sure that this custom dimension isn't already in use for another part of your site or a concurrently running Optimizely experiment.
setDimension code on your page. If you include the
ga('set', 'dimension1', dimensionValue); code on your site, you might overwrite the Optimizely-Google UA integration.
- If you're using a custom event tracker other than the default, choose one in the Specify a custom tracker field. This changes Optimizely's integration call to use the custom event tracker instead of the default. For example, if your website uses this call:
...you'll enter tracker2 in the Specify a custom tracker field, and Optimizely will integrate with tracker2 instead of the default tracker.
Place the API call code (Optimizely Classic)
If you're using Optimizely Classic, this step will help you place the API call code on your site. Skip this step if you're using the Optimizely X Web platform.
ga('create'...) function fires and before the Google UA
ga('send','pageview') function fires and the tracking call is made:
// Optimizely Universal Analytics Integration
window.optimizely = window.optimizely || ;
It's important that this code is the last call that is made before the pageview, especially if you're using the setCookieDomain function.
To get the latest tracking code from Google Analytics, navigate to the Admin tab, then click Tracking Info > Tracking Code.
As an example, your API call code might look like this in the body of your HTML:
//Universal Analytics Setup
ga('create', 'UA-XXXX-Y', 'auto');
// Optimizely Universal Analytics Integration Code
window.optimizely = window.optimizely || ;
// End Optimizely Code
The Optimizely API call sets custom dimension values for all active Optimizely experiments configured to use the Google UA integration. It's fine to make this API call even if no experiments are currently configured to use it. This means that you only have to add this code site-wide once, and you don't have to change it!
Create a Google UA custom dimension
To integrate Optimizely with Google UA and view reports based on the integration data, you need a Google UA custom dimension. This custom dimension and the name you give it are used to create reports that leverage your Optimizely integration data for filtering.
Here's how to create a Google UA custom dimension:
- Log into your Google UA account as an Admin and click the Admin tab.
- Click Custom Definitions and select the Custom Dimensions sub-item.
If you see the custom dimension you want to integrate your Optimizely experiment with, just note it's name and index number--no need to add a new one.
If you want to integrate with a custom dimension that isn't listed, you do need to create a new one. Click New Custom Dimension, and give the new dimension a name and a scope.
- Click Create.
That's all! Your custom dimension is now available for integrating with Optimizely experiments.
Although we recommend integration scope at the session level, you can configure your integration scope at the user level. Optimizely's default integration uses session-level scope to minimize the possibility of overwriting your experiment data sent to Google UA. But if you're tracking complex conversions that span multiple sessions, you might prefer a user-level scope. Choose the scope that's better for the types of conversions you want to track.
For example, you might want a session-level scope if the experiment is running site-wide or if a visitor will always go back through the experiment pages before converting. But you'd want a user-level scope if it's possible for a user to leave the site, then come back and convert without seeing the experiment again.
If you do this, be cautious of reusing custom dimensions in Google UA. For consistency between Google UA and Optimizely's Results page, set the Google UA reporting range to the same duration as your experiment.
Check out Google's documentation on scoping for details.
Create a custom report
You'll want to see reports on your experiments within Google UA, so you'll need a custom report for each experiment. Here's how to create custom reports:
- Log into your Google UA account and click the Customization tab.
- Set up a custom report for each experiment with which you have integrated Google UA: click New Custom Report, give the report a title, and add the metric groups you want to view in the report.
- To filter the report for only your Optimizely experiment, choose the custom dimension you set up as one of the Dimension Drilldowns.
- Add the custom dimension in the Filters section.
- In Optimizely X Web, enter the regular expression ("Regex") match for the campaign you want to filter for. In Optimizely Classic, use the experiment ID instead.
This will ensure that your report shows only the Google UA data for that particular experiment.
Leave the fields in the Filters section empty when you're creating a custom report for an iOS experiment.
- Click Save.
Now you can access your report and update the date filter as needed!
Confirm your integration
Google UA'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 Google UA, but the custom dimension is set immediately when you integrate Google UA and Optimizely. This means you can confirm that the custom dimension is set correctly with the Google Analytics’ debugger tool.
Using the debugger tool, look for the variable
x is the slot that corresponds to the Optimizely experiment. The variable is set with the Optimizely experiment, followed by the variation you were bucketed into. Here's an example:
dimension7 (&cd7) Optimizely_Onboarding (1645002506): Variant#1
Have a question about integrating Optimizely with your Google UA platform? Head over to the Optimizely Community to post a discussion and see what others are talking about.
Preserve referral sources during redirects (Optimizely Classic)
In Optimizely Classic, when a redirect takes place in Optimizely with Google UA integration enabled, we grab the document.referrer value and call the
ga('set','referrer'); function to maintain the original referrer. This works well on landing pages, but if there is a redirect on any page deeper into your site, it strips the visitor's original session referrer and makes the referrer the immediately preceding page. This inflates your direct and referral traffic source data in Google UA and AdWords reports.
To address this issue, implement this GitHub code to run on every page load above the Optimizely snippet. On the initial landing page, this code will grab the session's initial referrer and set it to a cookie. The cookie persists throughout the visitor's navigation on your site. When the visitor is redirected on a non-landing page as part of an Optimizely experiment, this code will determine if the existing session's referrer should be preserved or updated to document.referrer. Then, the code sends the appropriate referrer to Google UA. As a result, the original traffic source is persistent.
Please make sure to read the implementation instructions at the top of the code.