Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass through GTM environment params if present #5067

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/gatsby-plugin-google-tagmanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ plugins: [
resolve: `gatsby-plugin-google-tagmanager`,
options: {
id: "YOUR_GOOGLE_TAGMANAGER_ID",

// Include GTM in development.
// Defaults to false meaning GTM will only be loaded in production.
includeInDevelopment: false,

// Specify optional GTM environment details.
gtmAuth: "YOUR_GOOGLE_TAGMANAGER_ENVIROMENT_AUTH_STRING",
gtmPreview: "YOUR_GOOGLE_TAGMANAGER_ENVIROMENT_PREVIEW_NAME",
},
},
];
Expand Down
11 changes: 9 additions & 2 deletions packages/gatsby-plugin-google-tagmanager/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import { stripIndent } from "common-tags"
import { oneLine, stripIndent } from "common-tags"

exports.onRenderBody = (
{ setHeadComponents, setPreBodyComponents },
Expand All @@ -9,6 +9,11 @@ exports.onRenderBody = (
process.env.NODE_ENV === `production` ||
pluginOptions.includeInDevelopment
) {

const environmentParamStr = (pluginOptions.gtmAuth && pluginOptions.gtmPreview) ? oneLine`
&gtm_auth=${pluginOptions.gtmAuth}&gtm_preview=${pluginOptions.gtmPreview}&gtm_cookies_win=x
` : ``

setHeadComponents([
<script
key="plugin-google-tagmanager"
Expand All @@ -17,7 +22,7 @@ exports.onRenderBody = (
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
'https://www.googletagmanager.com/gtm.js?id='+i+dl+'${environmentParamStr}';f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer', '${pluginOptions.id}');`,
}}
/>,
Expand All @@ -31,6 +36,8 @@ exports.onRenderBody = (
<iframe
src="https://www.googletagmanager.com/ns.html?id=${
pluginOptions.id
}${
environmentParamStr
}"
height="0"
width="0"
Expand Down