Spaces:
Runtime error
Runtime error
add gtm
Browse files- .DS_Store +0 -0
- app/layout.tsx +5 -2
- components/gtm/index.tsx +43 -0
- utils/gtm.ts +22 -0
.DS_Store
CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
|
|
app/layout.tsx
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
import { cache } from "react";
|
2 |
import type { Metadata } from "next";
|
3 |
import { Inter } from "next/font/google";
|
4 |
import { QueryClient } from "@tanstack/react-query";
|
5 |
|
6 |
import "@/assets/globals.css";
|
7 |
import Providers from "@/components/react-query/providers";
|
8 |
-
import
|
9 |
|
10 |
const inter = Inter({ subsets: ["latin"] });
|
11 |
|
@@ -47,6 +47,9 @@ export default function RootLayout({
|
|
47 |
{/* <Script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js" /> */}
|
48 |
<body className={inter.className}>
|
49 |
<Providers>{children}</Providers>
|
|
|
|
|
|
|
50 |
</body>
|
51 |
</html>
|
52 |
);
|
|
|
1 |
+
import { Suspense, cache } from "react";
|
2 |
import type { Metadata } from "next";
|
3 |
import { Inter } from "next/font/google";
|
4 |
import { QueryClient } from "@tanstack/react-query";
|
5 |
|
6 |
import "@/assets/globals.css";
|
7 |
import Providers from "@/components/react-query/providers";
|
8 |
+
import Analytics from "@/components/gtm";
|
9 |
|
10 |
const inter = Inter({ subsets: ["latin"] });
|
11 |
|
|
|
47 |
{/* <Script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js" /> */}
|
48 |
<body className={inter.className}>
|
49 |
<Providers>{children}</Providers>
|
50 |
+
<Suspense>
|
51 |
+
<Analytics />
|
52 |
+
</Suspense>
|
53 |
</body>
|
54 |
</html>
|
55 |
);
|
components/gtm/index.tsx
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"use client";
|
2 |
+
import { useEffect } from "react";
|
3 |
+
import { usePathname, useSearchParams } from "next/navigation";
|
4 |
+
import Script from "next/script";
|
5 |
+
|
6 |
+
import { pageview } from "@/utils/gtm";
|
7 |
+
|
8 |
+
export default function Analytics() {
|
9 |
+
const pathname = usePathname();
|
10 |
+
const searchParams = useSearchParams();
|
11 |
+
|
12 |
+
useEffect(() => {
|
13 |
+
if (pathname) {
|
14 |
+
pageview(pathname);
|
15 |
+
}
|
16 |
+
}, [pathname, searchParams]);
|
17 |
+
|
18 |
+
return (
|
19 |
+
<>
|
20 |
+
<noscript>
|
21 |
+
<iframe
|
22 |
+
src="https://www.googletagmanager.com/ns.html?id=P6WGPCC4"
|
23 |
+
height="0"
|
24 |
+
width="0"
|
25 |
+
style={{ display: "none", visibility: "hidden" }}
|
26 |
+
/>
|
27 |
+
</noscript>
|
28 |
+
<Script
|
29 |
+
id="gtm-script"
|
30 |
+
strategy="afterInteractive"
|
31 |
+
dangerouslySetInnerHTML={{
|
32 |
+
__html: `
|
33 |
+
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
|
34 |
+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
|
35 |
+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
|
36 |
+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
|
37 |
+
})(window,document,'script','dataLayer', 'GTM-WKJGXLNK');
|
38 |
+
`,
|
39 |
+
}}
|
40 |
+
/>
|
41 |
+
</>
|
42 |
+
);
|
43 |
+
}
|
utils/gtm.ts
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// lib/gtm.ts
|
2 |
+
type WindowWithDataLayer = Window & {
|
3 |
+
dataLayer: Record<string, any>[]
|
4 |
+
}
|
5 |
+
|
6 |
+
declare const window: WindowWithDataLayer
|
7 |
+
|
8 |
+
export const GTM_ID = "GTM-WKJGXLNK"
|
9 |
+
|
10 |
+
export const pageview = (url: string) => {
|
11 |
+
if (typeof window.dataLayer !== "undefined") {
|
12 |
+
window.dataLayer.push({
|
13 |
+
event: "pageview",
|
14 |
+
page: url,
|
15 |
+
})
|
16 |
+
} else {
|
17 |
+
console.log({
|
18 |
+
event: "pageview",
|
19 |
+
page: url,
|
20 |
+
})
|
21 |
+
}
|
22 |
+
}
|