fix: ensure window.datalayer access in client only
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import "../../core.css";
|
||||
import "../../scandic.css";
|
||||
import "../../core.css"
|
||||
import "../../scandic.css"
|
||||
|
||||
import Footer from "@/components/Current/Footer";
|
||||
import LangPopup from "@/components/Current/LangPopup";
|
||||
import Script from "next/script";
|
||||
import SkipToMainContent from "@/components/SkipToMainContent";
|
||||
import Footer from "@/components/Current/Footer"
|
||||
import LangPopup from "@/components/Current/LangPopup"
|
||||
import Script from "next/script"
|
||||
import SkipToMainContent from "@/components/SkipToMainContent"
|
||||
|
||||
import type { Metadata } from "next";
|
||||
import type { LangParams, LayoutArgs } from "@/types/params";
|
||||
import type { Metadata } from "next"
|
||||
import type { LangParams, LayoutArgs } from "@/types/params"
|
||||
import { env } from "@/env/server.mjs"
|
||||
export const metadata: Metadata = {
|
||||
description: "New web",
|
||||
@@ -15,6 +15,7 @@ export const metadata: Metadata = {
|
||||
}
|
||||
|
||||
const adobeSrcs = {
|
||||
// TODO: decide on naming and environments
|
||||
development: "",
|
||||
test: "https://assets.adobedtm.com/c1bd08b1e4e7/d64a7c1f5f17/launch-84c70d82a50c-staging.min.js",
|
||||
production:
|
||||
@@ -72,7 +73,14 @@ export default function RootLayout({
|
||||
`}
|
||||
</Script> */}
|
||||
{/* TODO: Ensure order (datalayer created before script is executed!) */}
|
||||
{/* Should this be moved to an app/layout instead? */}
|
||||
<Script id={"before"}>{`
|
||||
console.log('before script load')
|
||||
`}</Script>
|
||||
<Script src={adobeSrcs[env.NODE_ENV]} />
|
||||
<Script id="after">{`
|
||||
console.log('after script load')
|
||||
`}</Script>
|
||||
{/* <Script
|
||||
data-cookieconsent="ignore"
|
||||
src="/Static/dist/js/main-ng.js?336b801d6b38eff10884"
|
||||
@@ -86,5 +94,5 @@ export default function RootLayout({
|
||||
<Footer lang={params.lang} />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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