diff --git a/package.json b/package.json index 310b0f4..273e377 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "prop-types": "^15.7.2", "react": "^17.0.1", "react-dom": "^17.0.1", + "react-gtm-module": "^2.0.11", "sass": "^1.26.11", "shorthash": "^0.0.2", "simple-react-lightbox": "3.6.6", diff --git a/pages/_app.js b/pages/_app.js index 3dea636..652a91c 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -3,6 +3,7 @@ import Router from 'next/router' import SimpleReactLightbox from 'simple-react-lightbox' import { ThemeProvider } from 'next-themes' import { appWithTranslation } from 'next-i18next' +import TagManager from 'react-gtm-module' import nextI18NextConfig from '../next-i18next.config' import Header from 'components/Header' @@ -22,6 +23,14 @@ Router.events.on( (url) => process.env.NODE_ENV === 'production' && pageview(url) ) +const tagManagerArgs = { + gtmId: process.env.NEXT_PUBLIC_GTM_ID, +} + +if (tagManagerArgs.gtmId) { + TagManager.initialize(tagManagerArgs) +} + function MyApp({ Component, pageProps, router }) { return ( <> diff --git a/pages/_document.js b/pages/_document.js index c1c7b63..cc8ef8c 100644 --- a/pages/_document.js +++ b/pages/_document.js @@ -4,6 +4,7 @@ import { NEXT_PUBLIC_GA_TRACKING_ID } from 'lib/constants' class MyDocument extends Document { render() { const isProduction = process.env.NODE_ENV === 'production' + const isGTMEnabled = process.env.NEXT_PUBLIC_GTM_ID && isProduction return ( @@ -48,8 +49,26 @@ class MyDocument extends Document { /> )} + {isGTMEnabled && ( + + )}
+ {isGTMEnabled && ( + + )}