-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-plugin-google-gtag): Add plugin options (#37953)
* add plugin options * change object syntax * update source code * update README * Restore packages/gatsby-plugin-google-gtag/src/gatsby-browser.js * Restore packages/gatsby-plugin-google-gtag/src/gatsby-ssr.js
- Loading branch information
Showing
4 changed files
with
162 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/gatsby-plugin-google-gtag/src/__tests__/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { testPluginOptionsSchema } from "gatsby-plugin-utils" | ||
import { pluginOptionsSchema } from "../gatsby-node" | ||
|
||
describe(`pluginOptionsSchema`, () => { | ||
it(`should invalidate incorrect options`, async () => { | ||
const options = { | ||
trackingIds: undefined, // Is required | ||
gtagConfig: `test`, | ||
pluginConfig: { | ||
head: `test`, | ||
respectDNT: `test`, | ||
exclude: `test`, | ||
origin: 1, | ||
delayOnRouteUpdate: `test`, | ||
}, | ||
} | ||
const expectedErrors = [ | ||
`"trackingIds" is required`, | ||
`"gtagConfig" must be of type object`, | ||
`"pluginConfig.head" must be a boolean`, | ||
`"pluginConfig.respectDNT" must be a boolean`, | ||
`"pluginConfig.exclude" must be an array`, | ||
`"pluginConfig.origin" must be a string`, | ||
`"pluginConfig.delayOnRouteUpdate" must be a number`, | ||
] | ||
|
||
const { isValid, errors } = await testPluginOptionsSchema( | ||
pluginOptionsSchema, | ||
options | ||
) | ||
|
||
expect(isValid).toBe(false) | ||
expect(errors).toEqual(expectedErrors) | ||
}) | ||
|
||
it(`should validate correct options`, async () => { | ||
const options = { | ||
trackingIds: [`test`], | ||
gtagConfig: { | ||
anonymize_ip: true, | ||
}, | ||
pluginConfig: { | ||
head: true, | ||
respectDNT: true, | ||
exclude: [`test`], | ||
origin: `test`, | ||
delayOnRouteUpdate: 1, | ||
}, | ||
} | ||
const { isValid, errors } = await testPluginOptionsSchema( | ||
pluginOptionsSchema, | ||
options | ||
) | ||
|
||
expect(isValid).toBe(true) | ||
expect(errors).toEqual([]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// @ts-check | ||
|
||
/** | ||
* @type {import('gatsby').GatsbyNode["pluginOptionsSchema"]} | ||
*/ | ||
exports.pluginOptionsSchema = ({ Joi }) => | ||
Joi.object({ | ||
trackingIds: Joi.array() | ||
.items(Joi.string()) | ||
.description( | ||
`The tracking IDs; the tracking code won't be generated without them.` | ||
) | ||
.required(), | ||
gtagConfig: Joi.object() | ||
.keys({ | ||
optimize_id: Joi.string().description( | ||
`Enable if you need to use Google Optimize.` | ||
), | ||
anonymize_ip: Joi.boolean() | ||
.description(`Enable if you need to use the "anonymizeIP" function.`) | ||
.default(false), | ||
}) | ||
.unknown(true) | ||
.description( | ||
`This object gets passed directly to the gtag config command.` | ||
) | ||
.default({}), | ||
pluginConfig: Joi.object() | ||
.keys({ | ||
head: Joi.boolean() | ||
.description( | ||
`Puts tracking script in the <head> instead of the <body>` | ||
) | ||
.default(false), | ||
respectDNT: Joi.boolean() | ||
.description( | ||
`If you enable this optional option, Google Global Site Tag will not be loaded at all for visitors that have "Do Not Track" enabled.` | ||
) | ||
.default(false), | ||
exclude: Joi.array() | ||
.items(Joi.string()) | ||
.description( | ||
`If you need to exclude any path from the tracking system, you can add it (one or more) to this optional array as glob expressions.` | ||
) | ||
.default([]), | ||
origin: Joi.string() | ||
.description(`Your optional self hosted origin for the script.`) | ||
.default(`https://www.googletagmanager.com`), | ||
delayOnRouteUpdate: Joi.number() | ||
.description(`Delay processing pageview events on route update`) | ||
.default(0), | ||
}) | ||
.description(`Configure the plugin's behavior.`), | ||
}) |