Skip to content

Commit

Permalink
enable defaultDatalayer as function
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Jul 9, 2019
1 parent 34e4f6c commit 83691a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe(`gatsby-plugin-google-tagmanager`, () => {
setPreBodyComponents: jest.fn(),
}
const pluginOptions = {
id: `123`,
includeInDevelopment: true,
}

Expand All @@ -51,7 +52,10 @@ describe(`gatsby-plugin-google-tagmanager`, () => {
}
const pluginOptions = {
includeInDevelopment: true,
defaultDataLayer: { pageCategory: `home` },
defaultDataLayer: {
type: `object`,
value: { pageCategory: `home` },
},
}

onRenderBody(mocks, pluginOptions)
Expand All @@ -69,12 +73,17 @@ describe(`gatsby-plugin-google-tagmanager`, () => {
}
const pluginOptions = {
includeInDevelopment: true,
defaultDataLayer: function() {
return { pageCategory: window.pageType }
defaultDataLayer: {
type: `function`,
value: function() {
return { pageCategory: window.pageType }
}.toString(),
},
}

const datalayerFuncAsString = oneLine`${pluginOptions.defaultDataLayer.toString()}`
const datalayerFuncAsString = oneLine`${
pluginOptions.defaultDataLayer.value
}`

onRenderBody(mocks, pluginOptions)
const [headConfig] = mocks.setHeadComponents.mock.calls[0][0]
Expand All @@ -96,15 +105,21 @@ describe(`gatsby-plugin-google-tagmanager`, () => {
}
let pluginOptions = {
includeInDevelopment: true,
defaultDataLayer: 5,
defaultDataLayer: {
type: `number`,
value: 5,
},
}

expect(() => onRenderBody(mocks, pluginOptions)).toThrow()

class Test {}
pluginOptions = {
includeInDevelopment: true,
defaultDataLayer: new Test(),
defaultDataLayer: {
type: `object`,
value: new Test(),
},
}

expect(() => onRenderBody(mocks, pluginOptions)).toThrow()
Expand Down
13 changes: 13 additions & 0 deletions packages/gatsby-plugin-google-tagmanager/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @type {import('gatsby').GatsbyNode["onPreInit"]} */
exports.onPreInit = (args, options) => {
if (options.defaultDataLayer) {
options.defaultDataLayer = {
type: typeof options.defaultDataLayer,
value: options.defaultDataLayer,
}

if (options.defaultDataLayer.type === `function`) {
options.defaultDataLayer.value = options.defaultDataLayer.value.toString()
}
}
}
8 changes: 4 additions & 4 deletions packages/gatsby-plugin-google-tagmanager/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ const generateGTMIframe = ({ id, environmentParamStr }) =>
const generateDefaultDataLayer = (dataLayer, reporter) => {
let result = `window.dataLayer = window.dataLayer || [];`

if (typeof dataLayer === `function`) {
result += `window.dataLayer.push((${dataLayer})());`
if (dataLayer.type === `function`) {
result += `window.dataLayer.push((${dataLayer.value})());`
} else {
if (typeof dataLayer !== `object` || dataLayer.constructor !== Object) {
if (dataLayer.type !== `object` || dataLayer.value.constructor !== Object) {
reporter.panic(
`Oops the plugin option "defaultDataLayer" should be a plain object. "${dataLayer}" is not valid.`
)
}

result += `window.dataLayer.push(${JSON.stringify(dataLayer)});`
result += `window.dataLayer.push(${JSON.stringify(dataLayer.value)});`
}

return stripIndent`${result}`
Expand Down

0 comments on commit 83691a8

Please sign in to comment.