Skip to content

Commit ed72e68

Browse files
tackaberrywardpeet
andauthored
feat(gatsby-plugin-google-tagmanager): add option for selfHostedOrigin (#32733)
In the gatsby-plugin-google-tagmanager, this change supports variable selfHostedOrigin, defaulting to googletagmanager.com. With the advent of Server Side Tagging, we have the ability to serve gtm.js from "self-hosted" tagging server. See: developers.google.com/tag-manager/serverside And: developers.google.com/tag-manager/serverside/send-data#update_the_gtmjs_source_domain Co-authored-by: Ward Peeters <[email protected]>
1 parent a6d4b1e commit ed72e68

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ plugins: [
3939
routeChangeEventName: "YOUR_ROUTE_CHANGE_EVENT_NAME",
4040
// Defaults to false
4141
enableWebVitalsTracking: true,
42+
// Defaults to https://www.googletagmanager.com
43+
selfHostedOrigin: "YOUR_SELF_HOSTED_ORIGIN",
4244
},
4345
},
4446
]

packages/gatsby-plugin-google-tagmanager/src/__tests__/gatsby-node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe(`pluginOptionsSchema`, () => {
1212
dataLayerName: `YOUR_DATA_LAYER_NAME`,
1313
routeChangeEventName: `YOUR_ROUTE_CHANGE_EVENT_NAME`,
1414
enableWebVitalsTracking: true,
15+
selfHostedOrigin: `YOUR_SELF_HOSTED_ORIGIN`,
1516
})
1617

1718
expect(isValid).toEqual(true)

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,53 @@ describe(`gatsby-plugin-google-tagmanager`, () => {
180180
expect(mocks.setHeadComponents.mock.calls[0].length).toBe(1)
181181
expect(headConfig.key).toBe(`plugin-google-tagmanager`)
182182
})
183+
184+
it(`should set selfHostedOrigin as googletagmanager.com by default`, () => {
185+
const mocks = {
186+
setHeadComponents: jest.fn(),
187+
setPreBodyComponents: jest.fn(),
188+
}
189+
const pluginOptions = {
190+
id: `123`,
191+
includeInDevelopment: true,
192+
}
193+
194+
onRenderBody(mocks, pluginOptions)
195+
const [headConfig] = mocks.setHeadComponents.mock.calls[0][0]
196+
const [preBodyConfig] = mocks.setPreBodyComponents.mock.calls[0][0]
197+
198+
// eslint-disable-next-line no-useless-escape
199+
expect(headConfig.props.dangerouslySetInnerHTML.__html).toContain(
200+
`https://www.googletagmanager.com/gtm.js`
201+
)
202+
expect(preBodyConfig.props.dangerouslySetInnerHTML.__html).toContain(
203+
`https://www.googletagmanager.com/ns.html`
204+
)
205+
})
206+
207+
it(`should set selfHostedOrigin`, () => {
208+
const selfHostedOrigin = `YOUR_SELF_HOSTED_ORIGIN`
209+
const mocks = {
210+
setHeadComponents: jest.fn(),
211+
setPreBodyComponents: jest.fn(),
212+
}
213+
const pluginOptions = {
214+
id: `123`,
215+
includeInDevelopment: true,
216+
selfHostedOrigin: selfHostedOrigin,
217+
}
218+
219+
onRenderBody(mocks, pluginOptions)
220+
const [headConfig] = mocks.setHeadComponents.mock.calls[0][0]
221+
const [preBodyConfig] = mocks.setPreBodyComponents.mock.calls[0][0]
222+
223+
// eslint-disable-next-line no-useless-escape
224+
expect(headConfig.props.dangerouslySetInnerHTML.__html).toContain(
225+
`${selfHostedOrigin}/gtm.js`
226+
)
227+
expect(preBodyConfig.props.dangerouslySetInnerHTML.__html).toContain(
228+
`${selfHostedOrigin}/ns.html`
229+
)
230+
})
183231
})
184232
})

packages/gatsby-plugin-google-tagmanager/src/gatsby-node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ exports.pluginOptionsSchema = ({ Joi }) =>
4141
`Name of the event that is triggered on every Gatsby route change.`
4242
),
4343
enableWebVitalsTracking: Joi.boolean().default(false),
44+
selfHostedOrigin: Joi.string()
45+
.default(`https://www.googletagmanager.com`)
46+
.description(`The origin where GTM is hosted.`),
4447
})

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import React from "react"
22
import { oneLine, stripIndent } from "common-tags"
33

4-
const generateGTM = ({ id, environmentParamStr, dataLayerName }) => stripIndent`
4+
const generateGTM = ({
5+
id,
6+
environmentParamStr,
7+
dataLayerName,
8+
selfHostedOrigin,
9+
}) => stripIndent`
510
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
611
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
712
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
8-
'https://www.googletagmanager.com/gtm.js?id='+i+dl+'${environmentParamStr}';f.parentNode.insertBefore(j,f);
13+
'${selfHostedOrigin}/gtm.js?id='+i+dl+'${environmentParamStr}';f.parentNode.insertBefore(j,f);
914
})(window,document,'script','${dataLayerName}', '${id}');`
1015

11-
const generateGTMIframe = ({ id, environmentParamStr }) =>
12-
oneLine`<iframe src="https://www.googletagmanager.com/ns.html?id=${id}${environmentParamStr}" height="0" width="0" style="display: none; visibility: hidden" aria-hidden="true"></iframe>`
16+
const generateGTMIframe = ({ id, environmentParamStr, selfHostedOrigin }) =>
17+
oneLine`<iframe src="${selfHostedOrigin}/ns.html?id=${id}${environmentParamStr}" height="0" width="0" style="display: none; visibility: hidden" aria-hidden="true"></iframe>`
1318

1419
const generateDefaultDataLayer = (dataLayer, reporter, dataLayerName) => {
1520
let result = `window.${dataLayerName} = window.${dataLayerName} || [];`
@@ -41,6 +46,7 @@ exports.onRenderBody = (
4146
defaultDataLayer,
4247
dataLayerName = `dataLayer`,
4348
enableWebVitalsTracking = false,
49+
selfHostedOrigin = `https://www.googletagmanager.com`,
4450
}
4551
) => {
4652
if (process.env.NODE_ENV === `production` || includeInDevelopment) {
@@ -60,6 +66,8 @@ exports.onRenderBody = (
6066
)
6167
}
6268

69+
selfHostedOrigin = selfHostedOrigin.replace(/\/$/, ``)
70+
6371
const inlineScripts = []
6472
if (enableWebVitalsTracking) {
6573
// web-vitals/polyfill (necessary for non chromium browsers)
@@ -83,7 +91,12 @@ exports.onRenderBody = (
8391
dangerouslySetInnerHTML={{
8492
__html: oneLine`
8593
${defaultDataLayerCode}
86-
${generateGTM({ id, environmentParamStr, dataLayerName })}`,
94+
${generateGTM({
95+
id,
96+
environmentParamStr,
97+
dataLayerName,
98+
selfHostedOrigin,
99+
})}`,
87100
}}
88101
/>
89102
)
@@ -94,7 +107,11 @@ exports.onRenderBody = (
94107
<noscript
95108
key="plugin-google-tagmanager"
96109
dangerouslySetInnerHTML={{
97-
__html: generateGTMIframe({ id, environmentParamStr }),
110+
__html: generateGTMIframe({
111+
id,
112+
environmentParamStr,
113+
selfHostedOrigin,
114+
}),
98115
}}
99116
/>,
100117
])

0 commit comments

Comments
 (0)