Skip to content

Commit 45ec012

Browse files
rotexhawkpieh
authored andcommitted
feat(gatsby-plugin-google-analytics): add support for google optimize experiment and variation id (#10903)
1 parent ac2a54a commit 45ec012

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

packages/gatsby-plugin-google-analytics/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ module.exports = {
2626
exclude: ["/preview/**", "/do-not-track/me/too/"],
2727
// Enables Google Optimize using your container Id
2828
optimizeId: "YOUR_GOOGLE_OPTIMIZE_TRACKING_ID",
29+
// Enables Google Optimize Experiment ID
30+
experimentId: "YOUR_GOOGLE_EXPERIMENT_ID",
31+
// Set Variation ID. 0 for original 1,2,3....
32+
variationId: "YOUR_GOOGLE_OPTIMIZE_VARIATION_ID"
2933
// Any additional create only fields (optional)
3034
sampleRate: 5,
3135
siteSpeedSampleRate: 10,
@@ -92,6 +96,14 @@ If you need to exclude any path from the tracking system, you can add it (one or
9296

9397
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.
9498

99+
## The "experimentId" option
100+
101+
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)
102+
103+
## The "variationId" option
104+
105+
Besides the experiment ID you also need the variation ID for SERVER_SIDE experiments in Google Optimize. Set 0 for original version.
106+
95107
## Create Only Fields
96108

97109
This plugin supports all optional Create Only Fields documented in [Google Analytics](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#create):

packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ exports.onRenderBody = (
8787
typeof pluginOptions.optimizeId !== `undefined`
8888
? `ga('require', '${pluginOptions.optimizeId}');`
8989
: ``
90+
}
91+
${
92+
typeof pluginOptions.experimentId !== `undefined`
93+
? `ga('set', 'expId', '${pluginOptions.experimentId}');`
94+
: ``
95+
}
96+
${
97+
typeof pluginOptions.variationId !== `undefined`
98+
? `ga('set', 'expVar', '${pluginOptions.variationId}');`
99+
: ``
90100
}}
91101
`,
92102
}}

0 commit comments

Comments
 (0)