fix: ensure window.datalayer access in client only
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { usePathname, useSearchParams } from "next/navigation"
|
||||
import Script from "next/script"
|
||||
|
||||
type WindowWithDataLayer = Window & {
|
||||
datalayer: { [key: string]: any }
|
||||
@@ -50,15 +49,17 @@ function createPageObject(pathName: string, queryString?: string) {
|
||||
}
|
||||
|
||||
export default function Tracking() {
|
||||
console.log("create datalayer 🚀")
|
||||
|
||||
if (!window.datalayer) {
|
||||
window.datalayer = {}
|
||||
}
|
||||
|
||||
const pathName = usePathname()
|
||||
const queryString = useSearchParams().toString()
|
||||
|
||||
if (typeof window === "undefined") {
|
||||
return null
|
||||
}
|
||||
if (!window.datalayer) {
|
||||
console.log("creating datalayer 🧑🔧")
|
||||
window.datalayer = {}
|
||||
}
|
||||
|
||||
const pageObject = createPageObject(pathName, queryString)
|
||||
|
||||
if (window.datalayer) {
|
||||
@@ -72,12 +73,12 @@ export default function Tracking() {
|
||||
// }
|
||||
|
||||
// datalayer.user = user_obj;
|
||||
console.log("🤖 datalayer: ", window.datalayer)
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<Script id="page-tracking">{`
|
||||
console.log('hello')
|
||||
`}</Script>
|
||||
</>
|
||||
)
|
||||
return null
|
||||
// <>
|
||||
// <Script id="page-tracking">{`
|
||||
// console.log('hello')
|
||||
// `}</Script>
|
||||
// </>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user