Skip to content

Commit d224536

Browse files
committed
feat: integrate react-gtm-module
1 parent c3da0a1 commit d224536

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"prop-types": "^15.7.2",
5858
"react": "^17.0.1",
5959
"react-dom": "^17.0.1",
60+
"react-gtm-module": "^2.0.11",
6061
"sass": "^1.26.11",
6162
"shorthash": "^0.0.2",
6263
"simple-react-lightbox": "3.6.6",

pages/_app.js

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Router from 'next/router'
33
import SimpleReactLightbox from 'simple-react-lightbox'
44
import { ThemeProvider } from 'next-themes'
55
import { appWithTranslation } from 'next-i18next'
6+
import TagManager from 'react-gtm-module'
67
import nextI18NextConfig from '../next-i18next.config'
78

89
import Header from 'components/Header'
@@ -22,6 +23,14 @@ Router.events.on(
2223
(url) => process.env.NODE_ENV === 'production' && pageview(url)
2324
)
2425

26+
const tagManagerArgs = {
27+
gtmId: process.env.NEXT_PUBLIC_GTM_ID,
28+
}
29+
30+
if (tagManagerArgs.gtmId) {
31+
TagManager.initialize(tagManagerArgs)
32+
}
33+
2534
function MyApp({ Component, pageProps, router }) {
2635
return (
2736
<>

pages/_document.js

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { NEXT_PUBLIC_GA_TRACKING_ID } from 'lib/constants'
44
class MyDocument extends Document {
55
render() {
66
const isProduction = process.env.NODE_ENV === 'production'
7+
const isGTMEnabled = process.env.NEXT_PUBLIC_GTM_ID && isProduction
78

89
return (
910
<Html lang="zh-Hant-TW" data-dark-theme="dark">
@@ -48,8 +49,26 @@ class MyDocument extends Document {
4849
/>
4950
</t>
5051
)}
52+
{isGTMEnabled && (
53+
<script
54+
dangerouslySetInnerHTML={{
55+
__html: `(function (w, d, s, l, i) {
56+
w[l] = w[l] || []; w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
57+
var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
58+
j.async = true; j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
59+
})(window, document, 'script', 'dataLayer', '${process.env.NEXT_PUBLIC_GTM_ID}');`,
60+
}}
61+
></script>
62+
)}
5163
</Head>
5264
<body>
65+
{isGTMEnabled && (
66+
<noscript
67+
dangerouslySetInnerHTML={{
68+
__html: `<iframe src="https://www.googletagmanager.com/ns.html?id=${process.env.NEXT_PUBLIC_GTM_ID}" height="0" width="0" style="display:none;visibility:hidden"></iframe>`,
69+
}}
70+
></noscript>
71+
)}
5372
<Main />
5473
<NextScript />
5574
</body>

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3570,6 +3570,11 @@ react-dom@^17.0.1:
35703570
object-assign "^4.1.1"
35713571
scheduler "^0.20.2"
35723572

3573+
react-gtm-module@^2.0.11:
3574+
version "2.0.11"
3575+
resolved "https://registry.yarnpkg.com/react-gtm-module/-/react-gtm-module-2.0.11.tgz#14484dac8257acd93614e347c32da9c5ac524206"
3576+
integrity sha512-8gyj4TTxeP7eEyc2QKawEuQoAZdjKvMY4pgWfycGmqGByhs17fR+zEBs0JUDq4US/l+vbTl+6zvUIx27iDo/Vw==
3577+
35733578
react-i18next@^11.16.2:
35743579
version "11.17.2"
35753580
resolved "https://registry.npmjs.org/react-i18next/-/react-i18next-11.17.2.tgz"

0 commit comments

Comments
 (0)