Skip to content

Commit

Permalink
fix(gatsby-plugin-google-tagmanager): allow functions for defaultData…
Browse files Browse the repository at this point in the history
…Layer option (#27886)

* Added check for function type for defaultDataLayer prop

* Fixing lint issues

* More lint fixes

* Add tests

Co-authored-by: Max Stoiber <contact@mxstbr.com>
  • Loading branch information
ARKEOLOGIST and mxstbr authored Nov 9, 2020
1 parent 5bc1134 commit 631f3c4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-google-tagmanager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"babel-preset-gatsby-package": "^0.6.0-next.0",
"cross-env": "^7.0.2"
"cross-env": "^7.0.2",
"gatsby-plugin-utils": "^0.3.0-next.0"
},
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-google-tagmanager#readme",
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { pluginOptionsSchema } from "../gatsby-node"
import { testPluginOptionsSchema } from "gatsby-plugin-utils"

describe(`pluginOptionsSchema`, () => {
it(`should validate valid options`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
id: `YOUR_GOOGLE_TAGMANAGER_ID`,
includeInDevelopment: false,
defaultDataLayer: { platform: `gatsby` },
gtmAuth: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_AUTH_STRING`,
gtmPreview: `YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_PREVIEW_NAME`,
dataLayerName: `YOUR_DATA_LAYER_NAME`,
routeChangeEventName: `YOUR_ROUTE_CHANGE_EVENT_NAME`,
})

expect(isValid).toEqual(true)
})

it(`should support defaultDataLayer as a function`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
defaultDataLayer: () => {
return {
originalLocation:
document.location.protocol +
`//` +
document.location.hostname +
document.location.pathname +
document.location.search,
}
},
})

expect(isValid).toEqual(true)
})
})
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-google-tagmanager/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ exports.pluginOptionsSchema = ({ Joi }) =>
.description(
`Include Google Tag Manager when running in development mode.`
),
defaultDataLayer: Joi.object()
defaultDataLayer: Joi.alternatives()
.try(Joi.object(), Joi.function())
.default(null)
.description(
`Data layer to be set before Google Tag Manager is loaded. Should be an object or a function.`
Expand Down

0 comments on commit 631f3c4

Please sign in to comment.