From b40ef7d5c83f4d60abae3411588858d491a6e1b2 Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Fri, 9 Feb 2024 15:36:04 +0100 Subject: [PATCH 01/19] WIP: add Adobe page tracking --- .../(live)/current-content-page/page.tsx | 4 +- app/[lang]/(live)/layout.tsx | 21 ++++- app/[lang]/(live)/not-found.tsx | 13 ++- app/[lang]/Tracking.tsx | 83 +++++++++++++++++++ 4 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 app/[lang]/Tracking.tsx diff --git a/app/[lang]/(live)/current-content-page/page.tsx b/app/[lang]/(live)/current-content-page/page.tsx index 2585f6982..cc056893b 100644 --- a/app/[lang]/(live)/current-content-page/page.tsx +++ b/app/[lang]/(live)/current-content-page/page.tsx @@ -1,7 +1,7 @@ import { notFound } from "next/navigation" -import { request } from "@/lib/request"; -import { GetCurrentBlockPage } from "@/lib/graphql/Query/CurrentBlockPage.graphql"; +import { request } from "@/lib/request" +import { GetCurrentBlockPage } from "@/lib/graphql/Query/CurrentBlockPage.graphql" import ContentPage from "@/components/Current/ContentPage" diff --git a/app/[lang]/(live)/layout.tsx b/app/[lang]/(live)/layout.tsx index 797078b88..57bc88523 100644 --- a/app/[lang]/(live)/layout.tsx +++ b/app/[lang]/(live)/layout.tsx @@ -8,11 +8,18 @@ import SkipToMainContent from "@/components/SkipToMainContent"; 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", title: "Scandic Hotels New Web", -}; +} + +const adobeSrcs = { + development: "", + test: "https://assets.adobedtm.com/c1bd08b1e4e7/d64a7c1f5f17/launch-84c70d82a50c-staging.min.js", + production: + "https://assets.adobedtm.com/c1bd08b1e4e7/d64a7c1f5f17/launch-e56085bbe998.min.js", +} export default function RootLayout({ children, @@ -56,6 +63,16 @@ export default function RootLayout({ data-cookieconsent="ignore" src="/Static/dist/js/ng/main.js?1705409330990" /> + {/* */} + {/* TODO: Ensure order (datalayer created before script is executed!) */} + + + ) +} From 666e62c85103cf0c673c2c44f415f263eb5c8c7e Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Mon, 12 Feb 2024 13:16:05 +0100 Subject: [PATCH 02/19] fix: ensure window.datalayer access in client only --- app/[lang]/(live)/layout.tsx | 26 +++++++++++++++++--------- app/[lang]/Tracking.tsx | 29 +++++++++++++++-------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/app/[lang]/(live)/layout.tsx b/app/[lang]/(live)/layout.tsx index 57bc88523..e2c59e1a8 100644 --- a/app/[lang]/(live)/layout.tsx +++ b/app/[lang]/(live)/layout.tsx @@ -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({ `} */} {/* TODO: Ensure order (datalayer created before script is executed!) */} + {/* Should this be moved to an app/layout instead? */} + {/* - - ) + return null + // <> + // + // } From 5ccb6166c307ab46f203f6991d6da4314cce3897 Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Mon, 12 Feb 2024 13:50:58 +0100 Subject: [PATCH 03/19] WIP: tracking --- app/[lang]/(live)/layout.tsx | 13 ++----- app/[lang]/(live)/not-found.tsx | 13 ++----- app/[lang]/Tracking.tsx | 66 ++++++++++++++++----------------- 3 files changed, 39 insertions(+), 53 deletions(-) diff --git a/app/[lang]/(live)/layout.tsx b/app/[lang]/(live)/layout.tsx index e2c59e1a8..697955bd3 100644 --- a/app/[lang]/(live)/layout.tsx +++ b/app/[lang]/(live)/layout.tsx @@ -64,15 +64,6 @@ export default function RootLayout({ data-cookieconsent="ignore" src="/Static/dist/js/ng/main.js?1705409330990" /> - {/* */} - {/* TODO: Ensure order (datalayer created before script is executed!) */} {/* Should this be moved to an app/layout instead? */} ) diff --git a/app/[lang]/(live)/not-found.tsx b/app/[lang]/(live)/not-found.tsx index 56fdd344c..1a6cc1ce6 100644 --- a/app/[lang]/(live)/not-found.tsx +++ b/app/[lang]/(live)/not-found.tsx @@ -1,13 +1,8 @@ -import Tracking from "./Tracking" - export default function NotFound() { return ( - <> -
-

Not Found

-

Could not find requested resource

-
- - +
+

Not Found

+

Could not find requested resource

+
) } diff --git a/app/[lang]/Tracking.tsx b/app/[lang]/Tracking.tsx index 80cb0cdbb..8714ed9c7 100644 --- a/app/[lang]/Tracking.tsx +++ b/app/[lang]/Tracking.tsx @@ -1,6 +1,8 @@ "use client" +import "client-only" -import { usePathname, useSearchParams } from "next/navigation" +import { useParams, usePathname, useSearchParams } from "next/navigation" +import { useEffect } from "react" type WindowWithDataLayer = Window & { datalayer: { [key: string]: any } @@ -11,11 +13,11 @@ declare const window: WindowWithDataLayer function createPageObject(pathName: string, queryString?: string) { const [lang, ...segments] = pathName.split("/").filter((v: string) => v) - // implement more solid approach for lang ⬆️ + // Better to get lang from useParams() instead?? ⬆️ function getSiteSections(segments: string[]) { const sitesections: { [key: string]: string } = {} - + // Adobe expects the properties sitesection1 - sitessection6, hence the for-loop below for (let i = 0; i < 6; i++) { const key = "value" + (i + 1) sitesections[key] = segments.slice(0, i + 1).join("|") @@ -28,21 +30,22 @@ function createPageObject(pathName: string, queryString?: string) { return sitesections } const sitesections = getSiteSections(segments) + const { host: domain, href: pageurl } = window.location const page_obj = { - pagename: "", - pagetype: "", - pageurl: "urlObject.href", + pagename: "", // CMS + pagetype: "", // CMS + pageurl, // is window.location.href viable? ...sitesections, - createDate: "< the date when page is created>", - publishDate: "< the date when page is published>", - domain: "", // is window.location.host viable? + createDate: "< the date when page is created>", // CMS + publishDate: "< the date when page is published>", // CMS + domain, // is window.location.host viable? errorcode: null, // handle querystring: queryString || "", - pageid: "", + pageid: "", // CMS sessionid: "", // base on what? domainlanguage: lang, - hotelbrand: "", // what is this based on? + hotelbrand: "scandic", // "", what is this based on? siteversion: "new-web", // good enough? } return page_obj @@ -52,33 +55,26 @@ export default function Tracking() { const pathName = usePathname() const queryString = useSearchParams().toString() - if (typeof window === "undefined") { - return null - } - if (!window.datalayer) { - console.log("creating datalayer πŸ§‘β€πŸ”§") - window.datalayer = {} - } + useEffect(() => { + if (!window.datalayer) { + console.log("creating datalayer πŸ§‘β€πŸ”§") + window.datalayer = {} + } else { + const pageObject = createPageObject(pathName, queryString) - const pageObject = createPageObject(pathName, queryString) + window.datalayer.page = pageObject - if (window.datalayer) { - window.datalayer.page = pageObject + // NOTE: Is this irrelevant fΓΆr drop 1? + // var user_obj = { + // loginstatus: "", + // memberid: "", + // memberlevel: "", + // } - // NOTE: Is this irrelevant fΓΆr drop 1? - // var user_obj = { - // loginstatus: "", - // memberid: "", - // memberlevel: "", - // } + // datalayer.user = user_obj; + console.log("πŸ€– datalayer: ", window.datalayer) + } + }, [pathName, queryString]) - // datalayer.user = user_obj; - console.log("πŸ€– datalayer: ", window.datalayer) - } return null - // <> - // - // } From 5bd162da10eaf4ce95afcb9f8fbcf9029c18b2c3 Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Tue, 13 Feb 2024 15:18:44 +0100 Subject: [PATCH 04/19] feat: align Tracking component props with CMS data --- app/[lang]/Tracking.tsx | 31 +++++++++++++++++-------------- types/components/tracking.ts | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 types/components/tracking.ts diff --git a/app/[lang]/Tracking.tsx b/app/[lang]/Tracking.tsx index 8714ed9c7..7984032f5 100644 --- a/app/[lang]/Tracking.tsx +++ b/app/[lang]/Tracking.tsx @@ -1,7 +1,8 @@ "use client" -import "client-only" +import "client-only" // NEEDED? -import { useParams, usePathname, useSearchParams } from "next/navigation" +import { TrackingData, TrackingProps } from "@/types/components/tracking" +import { usePathname, useSearchParams } from "next/navigation" import { useEffect } from "react" type WindowWithDataLayer = Window & { @@ -10,8 +11,10 @@ type WindowWithDataLayer = Window & { declare const window: WindowWithDataLayer -function createPageObject(pathName: string, queryString?: string) { - const [lang, ...segments] = pathName.split("/").filter((v: string) => v) +function createPageObject(trackingData: TrackingData) { + const [lang, ...segments] = trackingData.pathName + .split("/") + .filter((v: string) => v) // Better to get lang from useParams() instead?? ⬆️ @@ -33,16 +36,16 @@ function createPageObject(pathName: string, queryString?: string) { const { host: domain, href: pageurl } = window.location const page_obj = { - pagename: "", // CMS - pagetype: "", // CMS + pagename: trackingData.pageName, + pagetype: trackingData.pageType, pageurl, // is window.location.href viable? ...sitesections, - createDate: "< the date when page is created>", // CMS - publishDate: "< the date when page is published>", // CMS + createDate: trackingData.createdDate, + publishDate: trackingData.publishedDate, domain, // is window.location.host viable? errorcode: null, // handle - querystring: queryString || "", - pageid: "", // CMS + querystring: trackingData.queryString || "", + pageid: trackingData.pageId, sessionid: "", // base on what? domainlanguage: lang, hotelbrand: "scandic", // "", what is this based on? @@ -51,16 +54,16 @@ function createPageObject(pathName: string, queryString?: string) { return page_obj } -export default function Tracking() { +export default function Tracking({ pageData }: TrackingProps) { const pathName = usePathname() const queryString = useSearchParams().toString() - useEffect(() => { if (!window.datalayer) { console.log("creating datalayer πŸ§‘β€πŸ”§") window.datalayer = {} } else { - const pageObject = createPageObject(pathName, queryString) + const trackingData = { ...pageData, pathName, queryString } + const pageObject = createPageObject(trackingData) window.datalayer.page = pageObject @@ -74,7 +77,7 @@ export default function Tracking() { // datalayer.user = user_obj; console.log("πŸ€– datalayer: ", window.datalayer) } - }, [pathName, queryString]) + }, [pathName, queryString, pageData]) return null } diff --git a/types/components/tracking.ts b/types/components/tracking.ts new file mode 100644 index 000000000..00afb55e3 --- /dev/null +++ b/types/components/tracking.ts @@ -0,0 +1,19 @@ +export type TrackingProps = { + pageData: { + pageId: string + createdDate: string + publishedDate: string + pageName: string + pageType: string + } +} + +export type TrackingData = { + pathName: string + queryString: string + pageId: string + pageName: string + pageType: string + publishedDate: string + createdDate: string +} From 5b3b47b568177bea620db66da2aeb636ad515efa Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Tue, 13 Feb 2024 15:53:26 +0100 Subject: [PATCH 05/19] WIP: map CMS data to Tracking --- .../(live)/current-content-page/page.tsx | 22 ++++++++++++++----- lib/graphql/Query/CurrentBlockPage.graphql | 2 ++ types/requests/currentBlockPage.ts | 6 +++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/[lang]/(live)/current-content-page/page.tsx b/app/[lang]/(live)/current-content-page/page.tsx index cc056893b..c3ed2654a 100644 --- a/app/[lang]/(live)/current-content-page/page.tsx +++ b/app/[lang]/(live)/current-content-page/page.tsx @@ -7,6 +7,7 @@ import ContentPage from "@/components/Current/ContentPage" import type { PageArgs, LangParams, UriParams } from "@/types/params" import type { GetCurrentBlockPageData } from "@/types/requests/currentBlockPage" +import Tracking from "../../Tracking" export default async function CurrentContentPage({ params, @@ -31,13 +32,24 @@ export default async function CurrentContentPage({ console.log("SearchParams URI: ", searchParams.uri) throw new Error("Not found") } + const pageData = response.data.all_current_blocks_page.items[0] + const trackingData = { + pageName: pageData.title, + pageType: pageData.system.content_type_uid, + publishedDate: pageData.system.updated_at, + createdDate: pageData.system.created_at, + pageId: pageData.system.uid, + } return ( - + <> + + + ) } catch (err) { return notFound() diff --git a/lib/graphql/Query/CurrentBlockPage.graphql b/lib/graphql/Query/CurrentBlockPage.graphql index dfc087b35..06f6968d4 100644 --- a/lib/graphql/Query/CurrentBlockPage.graphql +++ b/lib/graphql/Query/CurrentBlockPage.graphql @@ -31,6 +31,8 @@ query GetCurrentBlockPage($locale: String!, $url: String!) { system { uid content_type_uid + created_at + updated_at } } total diff --git a/types/requests/currentBlockPage.ts b/types/requests/currentBlockPage.ts index 251d29753..a4007f708 100644 --- a/types/requests/currentBlockPage.ts +++ b/types/requests/currentBlockPage.ts @@ -48,6 +48,12 @@ export type BlockPage = { preamble: Preamble title: string url: string + system: { + uid: string + content_type_uid: string // should this be locked down to the specific enum? + created_at: string + updated_at: string + } } export type GetCurrentBlockPageData = { From 0556b509bc38a985b48931c5da39de058626b3bc Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Wed, 14 Feb 2024 11:15:52 +0100 Subject: [PATCH 06/19] fix: replace content_type_id with __typename --- app/[lang]/(live)/current-content-page/page.tsx | 2 +- lib/graphql/Query/CurrentBlockPage.graphql | 2 +- types/requests/currentBlockPage.ts | 8 ++++++-- types/requests/utils/typename.ts | 4 ++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/[lang]/(live)/current-content-page/page.tsx b/app/[lang]/(live)/current-content-page/page.tsx index c3ed2654a..de9faf0e1 100644 --- a/app/[lang]/(live)/current-content-page/page.tsx +++ b/app/[lang]/(live)/current-content-page/page.tsx @@ -35,7 +35,7 @@ export default async function CurrentContentPage({ const pageData = response.data.all_current_blocks_page.items[0] const trackingData = { pageName: pageData.title, - pageType: pageData.system.content_type_uid, + pageType: pageData.__typename, publishedDate: pageData.system.updated_at, createdDate: pageData.system.created_at, pageId: pageData.system.uid, diff --git a/lib/graphql/Query/CurrentBlockPage.graphql b/lib/graphql/Query/CurrentBlockPage.graphql index 06f6968d4..8eebebb7a 100644 --- a/lib/graphql/Query/CurrentBlockPage.graphql +++ b/lib/graphql/Query/CurrentBlockPage.graphql @@ -10,6 +10,7 @@ query GetCurrentBlockPage($locale: String!, $url: String!) { all_current_blocks_page(limit: 1, locale: $locale, where: { url: $url }) { items { + __typename aside { __typename ...ContactAside @@ -30,7 +31,6 @@ query GetCurrentBlockPage($locale: String!, $url: String!) { url system { uid - content_type_uid created_at updated_at } diff --git a/types/requests/currentBlockPage.ts b/types/requests/currentBlockPage.ts index a4007f708..52a096880 100644 --- a/types/requests/currentBlockPage.ts +++ b/types/requests/currentBlockPage.ts @@ -8,7 +8,11 @@ import type { PuffBlock } from "./blocks/puff" import type { Preamble } from "./preamble" import type { Text } from "./blocks/text" import type { AllRequestResponse } from "./utils/all" -import type { AsideTypenameEnum, Typename } from "./utils/typename" +import type { + AsideTypenameEnum, + Typename, + PagesTypenameEnum, +} from "./utils/typename" import type { EmbedEnum } from "./utils/embeds" import type { Edges } from "./utils/edges" @@ -41,6 +45,7 @@ export type Breadcrumbs = { } export type BlockPage = { + __typename: PagesTypenameEnum.CurrentBlocksPage aside: Asides[] blocks: Blocks[] breadcrumbs: Breadcrumbs @@ -50,7 +55,6 @@ export type BlockPage = { url: string system: { uid: string - content_type_uid: string // should this be locked down to the specific enum? created_at: string updated_at: string } diff --git a/types/requests/utils/typename.ts b/types/requests/utils/typename.ts index ea9d03e94..2e9aaf74f 100644 --- a/types/requests/utils/typename.ts +++ b/types/requests/utils/typename.ts @@ -12,6 +12,10 @@ export enum BlocksTypenameEnum { CurrentBlocksPageBlocksText = "CurrentBlocksPageBlocksText", } +export enum PagesTypenameEnum { + CurrentBlocksPage = "CurrentBlocksPage", +} + export type BlocksTypename = keyof typeof BlocksTypenameEnum export type Typename = T & { From c5e20e969a2af690b7c014ddf23651f4a64c7138 Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Wed, 14 Feb 2024 11:42:31 +0100 Subject: [PATCH 07/19] chore: remove redundant import --- app/[lang]/Tracking.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/app/[lang]/Tracking.tsx b/app/[lang]/Tracking.tsx index 7984032f5..741332b55 100644 --- a/app/[lang]/Tracking.tsx +++ b/app/[lang]/Tracking.tsx @@ -1,5 +1,4 @@ "use client" -import "client-only" // NEEDED? import { TrackingData, TrackingProps } from "@/types/components/tracking" import { usePathname, useSearchParams } from "next/navigation" From 0973103c0ee4eb6ed5fa7e54831444673fa7b744 Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Wed, 14 Feb 2024 11:44:22 +0100 Subject: [PATCH 08/19] chore: move type --- app/[lang]/Tracking.tsx | 10 +++++----- types/components/tracking.ts | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/[lang]/Tracking.tsx b/app/[lang]/Tracking.tsx index 741332b55..e4498b511 100644 --- a/app/[lang]/Tracking.tsx +++ b/app/[lang]/Tracking.tsx @@ -1,13 +1,13 @@ "use client" -import { TrackingData, TrackingProps } from "@/types/components/tracking" +import { + TrackingData, + TrackingProps, + WindowWithDataLayer, +} from "@/types/components/tracking" import { usePathname, useSearchParams } from "next/navigation" import { useEffect } from "react" -type WindowWithDataLayer = Window & { - datalayer: { [key: string]: any } -} - declare const window: WindowWithDataLayer function createPageObject(trackingData: TrackingData) { diff --git a/types/components/tracking.ts b/types/components/tracking.ts index 00afb55e3..e8436e38d 100644 --- a/types/components/tracking.ts +++ b/types/components/tracking.ts @@ -1,3 +1,7 @@ +export type WindowWithDataLayer = Window & { + datalayer: { [key: string]: any } +} + export type TrackingProps = { pageData: { pageId: string From 778f456f4efd3670569906bc3f2d170d7fba101c Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Wed, 14 Feb 2024 11:47:51 +0100 Subject: [PATCH 09/19] chore: remove console statements --- app/[lang]/(live)/layout.tsx | 6 ------ app/[lang]/Tracking.tsx | 1 - 2 files changed, 7 deletions(-) diff --git a/app/[lang]/(live)/layout.tsx b/app/[lang]/(live)/layout.tsx index 697955bd3..fdfa24b8a 100644 --- a/app/[lang]/(live)/layout.tsx +++ b/app/[lang]/(live)/layout.tsx @@ -65,13 +65,7 @@ export default function RootLayout({ src="/Static/dist/js/ng/main.js?1705409330990" /> {/* Should this be moved to an app/layout instead? */} - {/* diff --git a/app/[lang]/Tracking.tsx b/app/[lang]/Tracking.tsx index 4fb60af9e..3cfcc7d6e 100644 --- a/app/[lang]/Tracking.tsx +++ b/app/[lang]/Tracking.tsx @@ -58,7 +58,6 @@ export default function Tracking({ pageData }: TrackingProps) { const queryString = useSearchParams().toString() useEffect(() => { if (!window.datalayer) { - console.log("creating datalayer πŸ§‘β€πŸ”§") window.datalayer = {} } else { const trackingData = { ...pageData, pathName, queryString } From b938295ea2bea26e94d5361465fa97ce97678919 Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Wed, 14 Feb 2024 14:17:47 +0100 Subject: [PATCH 12/19] chore: remove comment --- app/[lang]/(live)/layout.tsx | 1 - app/[lang]/Tracking.tsx | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/[lang]/(live)/layout.tsx b/app/[lang]/(live)/layout.tsx index b60730084..af65e4b02 100644 --- a/app/[lang]/(live)/layout.tsx +++ b/app/[lang]/(live)/layout.tsx @@ -57,7 +57,6 @@ export default function RootLayout({ data-cookieconsent="ignore" src="/Static/dist/js/ng/main.js?1705409330990" /> - {/* Should this be moved to an app/layout instead? */} {/* +