fix: improve typings

This commit is contained in:
Arvid Norlin
2024-02-15 16:22:44 +01:00
parent 4beb8da0b9
commit 09b28edb48
3 changed files with 17 additions and 10 deletions

View File

@@ -1,22 +1,19 @@
"use client" "use client"
import { import {
SiteSectionObject,
TrackingData, TrackingData,
TrackingProps, TrackingProps,
WindowWithDataLayer,
} from "@/types/components/tracking" } from "@/types/components/tracking"
import { usePathname, useSearchParams } from "next/navigation" import { usePathname, useSearchParams } from "next/navigation"
import { useEffect } from "react" import { useEffect } from "react"
declare const window: WindowWithDataLayer
function createPageObject(trackingData: TrackingData) { function createPageObject(trackingData: TrackingData) {
const [lang, ...segments] = trackingData.pathName const [lang, ...segments] = trackingData.pathName
.split("/") .split("/")
.filter((v: string) => v) .filter((v: string) => v)
function getSiteSections(segments: string[]) { function getSiteSections(segments: string[]): SiteSectionObject {
const sitesections: { [key: string]: string } = {}
/* /*
Adobe expects the properties sitesection1 - sitessection6, hence the for-loop below Adobe expects the properties sitesection1 - sitessection6, hence the for-loop below
The segments ['explore-scandic', 'wifi'] should result in: The segments ['explore-scandic', 'wifi'] should result in:
@@ -29,8 +26,10 @@ function createPageObject(trackingData: TrackingData) {
sitesection6: "explore-scandic|wifi||||", sitesection6: "explore-scandic|wifi||||",
} }
*/ */
const sitesections = {} as SiteSectionObject
for (let i = 0; i < 6; i++) { 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("|") sitesections[key] = segments.slice(0, i + 1).join("|")
if (i > 0 && !segments[i]) { if (i > 0 && !segments[i]) {
sitesections[key] = sitesections[key].concat( sitesections[key] = sitesections[key].concat(

View File

@@ -1,7 +1,3 @@
export type WindowWithDataLayer = Window & {
datalayer: { [key: string]: any }
}
export type TrackingProps = { export type TrackingProps = {
pageData: { pageData: {
pageId: string pageId: string
@@ -21,3 +17,12 @@ export type TrackingData = {
publishedDate: string publishedDate: string
createdDate: string createdDate: string
} }
export type SiteSectionObject = {
sitesection1: string
sitesection2: string
sitesection3: string
sitesection4: string
sitesection5: string
sitesection6: string
}

3
types/window.d.ts vendored Normal file
View File

@@ -0,0 +1,3 @@
interface Window {
datalayer: { [key: string]: any }
}