Skip to content

Commit 81a3181

Browse files
davidbelowRubén Belowwardpeet
authored
feat(tagmanager): add configurable event names (#21362) (#24076)
Co-authored-by: Rubén Below <[email protected]> Co-authored-by: Ward Peeters <[email protected]>
1 parent 78271c3 commit 81a3181

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,26 @@ plugins: [
1717
id: "YOUR_GOOGLE_TAGMANAGER_ID",
1818

1919
// Include GTM in development.
20+
//
2021
// Defaults to false meaning GTM will only be loaded in production.
2122
includeInDevelopment: false,
2223

2324
// datalayer to be set before GTM is loaded
2425
// should be an object or a function that is executed in the browser
26+
//
2527
// Defaults to null
2628
defaultDataLayer: { platform: "gatsby" },
2729

2830
// Specify optional GTM environment details.
2931
gtmAuth: "YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_AUTH_STRING",
3032
gtmPreview: "YOUR_GOOGLE_TAGMANAGER_ENVIRONMENT_PREVIEW_NAME",
3133
dataLayerName: "YOUR_DATA_LAYER_NAME",
34+
35+
// Name of the event that is triggered
36+
// on every Gatsby route change.
37+
//
38+
// Defaults to gatsby-route-change
39+
routeChangeEventName: "YOUR_ROUTE_CHANGE_EVENT_NAME",
3240
},
3341
},
3442
]
@@ -44,6 +52,7 @@ plugins: [
4452
options: {
4553
// datalayer to be set before GTM is loaded
4654
// should be a stringified object or object
55+
//
4756
// Defaults to null
4857
defaultDataLayer: function () {
4958
return {
@@ -61,13 +70,13 @@ If you want to link analytics use with anything inside the container (for exampl
6170

6271
#### Tracking routes
6372

64-
This plugin will fire a new event called `gatsby-route-change` whenever a route is changed in your Gatsby application. To record this in Google Tag Manager, we will need to add a trigger to the desired tag to listen for the event:
73+
This plugin will fire a new event called `gatsby-route-change` (or as in the `gatsby-config.js` configured `routeChangeEventName`) whenever a route is changed in your Gatsby application. To record this in Google Tag Manager, we will need to add a trigger to the desired tag to listen for the event:
6574

6675
1. Visit the [Google Tag Manager console](https://tagmanager.google.com/) and click on the workspace for your site.
6776
2. Navigate to the desired tag using the 'Tags' tab of the menu on the right hand side.
6877
3. Under "Triggering", click the pencil icon, then the "+" button to add a new trigger.
6978
4. In the "Choose a trigger" window, click on the "+" button again.
70-
5. Choose the trigger type by clicking the pencil button and clicking "Custom event". For event name, enter `gatsby-route-change`.
79+
5. Choose the trigger type by clicking the pencil button and clicking "Custom event". For event name, enter `gatsby-route-change` (or as in the `gatsby-config.js` configured `routeChangeEventName`).
7180

7281
This tag will now catch every route change in Gatsby, and you can add Google tag services as you wish to it.
7382

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

+22
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ describe(`onRouteUpdate`, () => {
7070
expect(window.dataLayer).toHaveLength(1)
7171
})
7272

73+
it(`registers a custom route change event name if given in routeChangeEventName`, () => {
74+
const { onRouteUpdate } = getAPI(() => {
75+
process.env.NODE_ENV = `production`
76+
})
77+
const customEventName = `custom-route-change-event-name`
78+
79+
onRouteUpdate(
80+
{},
81+
{
82+
routeChangeEventName: customEventName,
83+
}
84+
)
85+
86+
jest.runAllTimers()
87+
88+
expect(window.dataLayer).toEqual([
89+
{
90+
event: customEventName,
91+
},
92+
])
93+
})
94+
7395
it(`registers new data layer variable if dataLayerName is specified`, () => {
7496
const { onRouteUpdate } = getAPI(() => {
7597
process.env.NODE_ENV = `production`

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ exports.onRouteUpdate = (_, pluginOptions) => {
88
let data = pluginOptions.dataLayerName
99
? window[pluginOptions.dataLayerName]
1010
: window.dataLayer
11+
let eventName = pluginOptions.routeChangeEventName
12+
? pluginOptions.routeChangeEventName
13+
: `gatsby-route-change`
1114

12-
data.push({ event: `gatsby-route-change` })
15+
data.push({ event: eventName })
1316
}, 50)
1417
}
1518
}

0 commit comments

Comments
 (0)