Skip to content

Commit b3ed9be

Browse files
tinder-kylebosswardpeet
authored andcommitted
feat(gatsby-plugin-google-analytics): enable more options. (#15280)
* Added more plugin options for gatsby-plugin-google-analytics. This included breaking out `knownOptions` into three factions: - createOnly - general All options are from general, but I did not add all of the general options because a few do not work in a set-it-and-forget-it kinda way. Added some tests.
1 parent ac987b3 commit b3ed9be

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232
experimentId: "YOUR_GOOGLE_EXPERIMENT_ID",
3333
// Set Variation ID. 0 for original 1,2,3....
3434
variationId: "YOUR_GOOGLE_OPTIMIZE_VARIATION_ID",
35-
// Any additional create only fields (optional)
35+
// Any additional optional fields
3636
sampleRate: 5,
3737
siteSpeedSampleRate: 10,
3838
cookieDomain: "example.com",
@@ -42,7 +42,7 @@ module.exports = {
4242
}
4343
```
4444

45-
See below for the complete list of [Create Only Fields](#create-only-fields).
45+
See below for the complete list of [optional fields](#optional-fields).
4646

4747
## `<OutboundLink>` component
4848

@@ -120,7 +120,7 @@ If you need to set up SERVER_SIDE Google Optimize experiment, you can add the ex
120120

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

123-
## Create Only Fields
123+
## Optional Fields
124124

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

@@ -138,4 +138,12 @@ This plugin supports all optional Create Only Fields documented in [Google Analy
138138
- `legacyHistoryImport`: boolean
139139
- `allowLinker`: boolean
140140

141+
This plugin also supports several optional General fields documented in [Google Analytics](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#general):
142+
143+
- `allowAdFeatures`: boolean
144+
- `dataSource`: string
145+
- `queueTime`: number
146+
- `forceSSL`: boolean
147+
- `transport`: string
148+
141149
These fields can be specified in the plugin's `options` as shown in the [How to use](#how-to-use) section.

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

+29
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,35 @@ describe(`gatsby-plugin-google-analytics`, () => {
136136
expect(result).toMatch(/cookieName/)
137137
expect(result).toMatch(/sampleRate/)
138138
})
139+
140+
it(`sets additional general fields`, () => {
141+
const { setPostBodyComponents } = setup({
142+
transport: `beacon`,
143+
allowAdFeatures: true,
144+
queueTime: 5,
145+
})
146+
147+
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
148+
expect(result).toContain(`ga('set', 'transport', 'beacon')`)
149+
expect(result).toContain(`ga('set', 'allowAdFeatures', 'true')`)
150+
expect(result).toContain(`ga('set', 'queueTime', '5')`)
151+
})
152+
153+
it(`does not set fields that have an invalid value`, () => {
154+
const { setPostBodyComponents } = setup({
155+
allowAdFeatures: `swag`,
156+
})
157+
158+
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
159+
expect(result).not.toContain(`allowAdFeatures`)
160+
})
161+
162+
it(`does not set fields that were not set`, () => {
163+
const { setPostBodyComponents } = setup({})
164+
165+
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
166+
expect(result).not.toContain(`allowAdFeatures`)
167+
})
139168
})
140169
})
141170
})

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

+31-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import React from "react"
22

33
const knownOptions = {
4-
clientId: `string`,
5-
sampleRate: `number`,
6-
siteSpeedSampleRate: `number`,
7-
alwaysSendReferrer: `boolean`,
8-
allowAnchor: `boolean`,
9-
cookieName: `string`,
10-
cookieExpires: `number`,
11-
storeGac: `boolean`,
12-
legacyCookieDomain: `string`,
13-
legacyHistoryImport: `boolean`,
14-
allowLinker: `boolean`,
4+
createOnly: {
5+
clientId: `string`,
6+
sampleRate: `number`,
7+
siteSpeedSampleRate: `number`,
8+
alwaysSendReferrer: `boolean`,
9+
allowAnchor: `boolean`,
10+
cookieName: `string`,
11+
cookieExpires: `number`,
12+
storeGac: `boolean`,
13+
legacyCookieDomain: `string`,
14+
legacyHistoryImport: `boolean`,
15+
allowLinker: `boolean`,
16+
},
17+
general: {
18+
allowAdFeatures: `boolean`,
19+
dataSource: `string`,
20+
queueTime: `number`,
21+
forceSSL: `boolean`,
22+
transport: `string`,
23+
},
1524
}
1625

1726
export const onRenderBody = (
@@ -41,8 +50,8 @@ export const onRenderBody = (
4150
}
4251

4352
const gaCreateOptions = {}
44-
for (const option in knownOptions) {
45-
if (typeof pluginOptions[option] === knownOptions[option]) {
53+
for (const option in knownOptions.createOnly) {
54+
if (typeof pluginOptions[option] === knownOptions.createOnly[option]) {
4655
gaCreateOptions[option] = pluginOptions[option]
4756
}
4857
}
@@ -109,7 +118,15 @@ export const onRenderBody = (
109118
typeof pluginOptions.variationId !== `undefined`
110119
? `ga('set', 'expVar', '${pluginOptions.variationId}');`
111120
: ``
112-
}}
121+
}
122+
${Object.keys(knownOptions.general).reduce((gaSetCommands, option) => {
123+
if (typeof pluginOptions[option] === knownOptions.general[option]) {
124+
gaSetCommands += `ga('set', '${option}', '${
125+
pluginOptions[option]
126+
}');\n`
127+
}
128+
return gaSetCommands
129+
}, ``)}
113130
`,
114131
}}
115132
/>,

0 commit comments

Comments
 (0)