Skip to content

Commit

Permalink
feat(gatsby-plugin-google-analytics): add support for google optimize…
Browse files Browse the repository at this point in the history
… experiment and variation id (#10903)
  • Loading branch information
rotexhawk authored and pieh committed Jan 8, 2019
1 parent ac2a54a commit 45ec012
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/gatsby-plugin-google-analytics/README.md
Expand Up @@ -26,6 +26,10 @@ module.exports = {
exclude: ["/preview/**", "/do-not-track/me/too/"],
// Enables Google Optimize using your container Id
optimizeId: "YOUR_GOOGLE_OPTIMIZE_TRACKING_ID",
// Enables Google Optimize Experiment ID
experimentId: "YOUR_GOOGLE_EXPERIMENT_ID",
// Set Variation ID. 0 for original 1,2,3....
variationId: "YOUR_GOOGLE_OPTIMIZE_VARIATION_ID"
// Any additional create only fields (optional)
sampleRate: 5,
siteSpeedSampleRate: 10,
Expand Down Expand Up @@ -92,6 +96,14 @@ If you need to exclude any path from the tracking system, you can add it (one or

If you need to use Google Optimize for A/B testing, you can add this optional Optimize container id to allow Google Optimize to load the correct test parameters for your site.

## The "experimentId" option

If you need to setup SERVER_SIDE Google Optimize experiment, you can add the experiment ID. The experiment ID is shown on the right-hand panel on the experiment details page. [Server-side Experiments](https://developers.google.com/optimize/devguides/experiments)

## The "variationId" option

Besides the experiment ID you also need the variation ID for SERVER_SIDE experiments in Google Optimize. Set 0 for original version.

## Create Only Fields

This plugin supports all optional Create Only Fields documented in [Google Analytics](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#create):
Expand Down
10 changes: 10 additions & 0 deletions packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js
Expand Up @@ -87,6 +87,16 @@ exports.onRenderBody = (
typeof pluginOptions.optimizeId !== `undefined`
? `ga('require', '${pluginOptions.optimizeId}');`
: ``
}
${
typeof pluginOptions.experimentId !== `undefined`
? `ga('set', 'expId', '${pluginOptions.experimentId}');`
: ``
}
${
typeof pluginOptions.variationId !== `undefined`
? `ga('set', 'expVar', '${pluginOptions.variationId}');`
: ``
}}
`,
}}
Expand Down

0 comments on commit 45ec012

Please sign in to comment.