Skip to content

Commit dc33c97

Browse files
committed
Change gtag to gtm and fix gtm
1 parent dd8c0a1 commit dc33c97

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

components/meta/gtag.tsx

Lines changed: 0 additions & 14 deletions
This file was deleted.

components/meta/gtm.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useEffect } from 'react';
2+
3+
export const useGTM = (w, d, s, l, i) => {
4+
const GTM_ID = process.env.NEXT_PUBLIC_GTM_ID ?? 'GTM-MXDVTH6';
5+
6+
useEffect(() => {
7+
(function (w, d, s, l, i) {
8+
w[l] = w[l] || [];
9+
w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
10+
var f = d.getElementsByTagName(s)[0],
11+
j = d.createElement(s),
12+
dl = l != 'dataLayer' ? '&l=' + l : '';
13+
j.async = true;
14+
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
15+
f.parentNode.insertBefore(j, f);
16+
})(window, document, 'script', 'dataLayer', GTM_ID);
17+
}, []);
18+
};

components/meta/meta.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Head from 'next/head';
2-
// import GTag from './gtag';
2+
import { useGTM } from './gtm';
33
import { TMPropsMeta } from './metaType';
44

55
const Meta = (props: TMPropsMeta) => {
@@ -11,6 +11,7 @@ const Meta = (props: TMPropsMeta) => {
1111
imageWidth = '1200',
1212
imageHeight = '627',
1313
} = props;
14+
// useGTM();
1415

1516
return (
1617
<Head>

next.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
env: {
55
NEXT_PUBLIC_URL: process.env.NEXT_PUBLIC_URL,
66
//NEXT_PUBLIC_CMS_URL: process.env.NEXT_PUBLIC_CMS_URL,
7-
//NEXT_PUBLIC_GTAG_ID: process.env.NEXT_PUBLIC_GTAG_ID,
7+
NEXT_PUBLIC_GTM_ID: process.env.NEXT_PUBLIC_GTM_ID,
88
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
99
},
1010
eslint: { ignoreDuringBuilds: true },

pages/_document.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import Document, { Head, Html, Main, NextScript } from 'next/document';
2+
const GTM_ID = process.env.NEXT_PUBLIC_GTM_ID ?? 'GTM-MXDVTH6';
23

34
class MyDocument extends Document {
45
render() {
56
return (
67
<Html lang="en" className="no-js">
78
<Head />
89
<body>
10+
<noscript
11+
dangerouslySetInnerHTML={{
12+
__html: `<iframe src="https://www.googletagmanager.com/ns.html?id=${GTM_ID}" height="0" width="0" style="display:none;visibility:hidden"></iframe>`,
13+
}}
14+
/>
915
<Main />
1016
<NextScript />
1117
</body>

0 commit comments

Comments
 (0)