Skip to content

Commit

Permalink
feat(gatsby-plugin-google-analytics): enable more options. (#15280)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
tinder-kyleboss authored and wardpeet committed Jul 30, 2019
1 parent ac987b3 commit b3ed9be
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
14 changes: 11 additions & 3 deletions packages/gatsby-plugin-google-analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
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)
// Any additional optional fields
sampleRate: 5,
siteSpeedSampleRate: 10,
cookieDomain: "example.com",
Expand All @@ -42,7 +42,7 @@ module.exports = {
}
```

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

## `<OutboundLink>` component

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

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
## Optional 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 All @@ -138,4 +138,12 @@ This plugin supports all optional Create Only Fields documented in [Google Analy
- `legacyHistoryImport`: boolean
- `allowLinker`: boolean

This plugin also supports several optional General fields documented in [Google Analytics](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#general):

- `allowAdFeatures`: boolean
- `dataSource`: string
- `queueTime`: number
- `forceSSL`: boolean
- `transport`: string

These fields can be specified in the plugin's `options` as shown in the [How to use](#how-to-use) section.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,35 @@ describe(`gatsby-plugin-google-analytics`, () => {
expect(result).toMatch(/cookieName/)
expect(result).toMatch(/sampleRate/)
})

it(`sets additional general fields`, () => {
const { setPostBodyComponents } = setup({
transport: `beacon`,
allowAdFeatures: true,
queueTime: 5,
})

const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
expect(result).toContain(`ga('set', 'transport', 'beacon')`)
expect(result).toContain(`ga('set', 'allowAdFeatures', 'true')`)
expect(result).toContain(`ga('set', 'queueTime', '5')`)
})

it(`does not set fields that have an invalid value`, () => {
const { setPostBodyComponents } = setup({
allowAdFeatures: `swag`,
})

const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
expect(result).not.toContain(`allowAdFeatures`)
})

it(`does not set fields that were not set`, () => {
const { setPostBodyComponents } = setup({})

const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
expect(result).not.toContain(`allowAdFeatures`)
})
})
})
})
Expand Down
45 changes: 31 additions & 14 deletions packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import React from "react"

const knownOptions = {
clientId: `string`,
sampleRate: `number`,
siteSpeedSampleRate: `number`,
alwaysSendReferrer: `boolean`,
allowAnchor: `boolean`,
cookieName: `string`,
cookieExpires: `number`,
storeGac: `boolean`,
legacyCookieDomain: `string`,
legacyHistoryImport: `boolean`,
allowLinker: `boolean`,
createOnly: {
clientId: `string`,
sampleRate: `number`,
siteSpeedSampleRate: `number`,
alwaysSendReferrer: `boolean`,
allowAnchor: `boolean`,
cookieName: `string`,
cookieExpires: `number`,
storeGac: `boolean`,
legacyCookieDomain: `string`,
legacyHistoryImport: `boolean`,
allowLinker: `boolean`,
},
general: {
allowAdFeatures: `boolean`,
dataSource: `string`,
queueTime: `number`,
forceSSL: `boolean`,
transport: `string`,
},
}

export const onRenderBody = (
Expand Down Expand Up @@ -41,8 +50,8 @@ export const onRenderBody = (
}

const gaCreateOptions = {}
for (const option in knownOptions) {
if (typeof pluginOptions[option] === knownOptions[option]) {
for (const option in knownOptions.createOnly) {
if (typeof pluginOptions[option] === knownOptions.createOnly[option]) {
gaCreateOptions[option] = pluginOptions[option]
}
}
Expand Down Expand Up @@ -109,7 +118,15 @@ export const onRenderBody = (
typeof pluginOptions.variationId !== `undefined`
? `ga('set', 'expVar', '${pluginOptions.variationId}');`
: ``
}}
}
${Object.keys(knownOptions.general).reduce((gaSetCommands, option) => {
if (typeof pluginOptions[option] === knownOptions.general[option]) {
gaSetCommands += `ga('set', '${option}', '${
pluginOptions[option]
}');\n`
}
return gaSetCommands
}, ``)}
`,
}}
/>,
Expand Down

0 comments on commit b3ed9be

Please sign in to comment.