From 09b28edb486bfb0b84e9176027e9bf9e1d104503 Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Thu, 15 Feb 2024 16:22:44 +0100 Subject: [PATCH] fix: improve typings --- app/[lang]/Tracking.tsx | 11 +++++------ types/components/tracking.ts | 13 +++++++++---- types/window.d.ts | 3 +++ 3 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 types/window.d.ts diff --git a/app/[lang]/Tracking.tsx b/app/[lang]/Tracking.tsx index 9062e63e1..6eab35ed7 100644 --- a/app/[lang]/Tracking.tsx +++ b/app/[lang]/Tracking.tsx @@ -1,22 +1,19 @@ "use client" import { + SiteSectionObject, TrackingData, TrackingProps, - WindowWithDataLayer, } from "@/types/components/tracking" import { usePathname, useSearchParams } from "next/navigation" import { useEffect } from "react" -declare const window: WindowWithDataLayer - function createPageObject(trackingData: TrackingData) { const [lang, ...segments] = trackingData.pathName .split("/") .filter((v: string) => v) - function getSiteSections(segments: string[]) { - const sitesections: { [key: string]: string } = {} + function getSiteSections(segments: string[]): SiteSectionObject { /* Adobe expects the properties sitesection1 - sitessection6, hence the for-loop below The segments ['explore-scandic', 'wifi'] should result in: @@ -29,8 +26,10 @@ function createPageObject(trackingData: TrackingData) { sitesection6: "explore-scandic|wifi||||", } */ + const sitesections = {} as SiteSectionObject for (let i = 0; i < 6; i++) { - const key = "sitesection" + (i + 1) + const key = ("sitesection" + (i + 1)) as keyof SiteSectionObject + sitesections[key] = segments.slice(0, i + 1).join("|") if (i > 0 && !segments[i]) { sitesections[key] = sitesections[key].concat( diff --git a/types/components/tracking.ts b/types/components/tracking.ts index e8436e38d..e717a55f3 100644 --- a/types/components/tracking.ts +++ b/types/components/tracking.ts @@ -1,7 +1,3 @@ -export type WindowWithDataLayer = Window & { - datalayer: { [key: string]: any } -} - export type TrackingProps = { pageData: { pageId: string @@ -21,3 +17,12 @@ export type TrackingData = { publishedDate: string createdDate: string } + +export type SiteSectionObject = { + sitesection1: string + sitesection2: string + sitesection3: string + sitesection4: string + sitesection5: string + sitesection6: string +} diff --git a/types/window.d.ts b/types/window.d.ts new file mode 100644 index 000000000..8e2462b1f --- /dev/null +++ b/types/window.d.ts @@ -0,0 +1,3 @@ +interface Window { + datalayer: { [key: string]: any } +}