0

What I'm trying now is to have access to a dataLayer object from usercentric with this code:

const getDataLayerValue = (keyToFind: string, objects: DataLayerType[]): boolean | undefined | string => {
  for (const obj of Object.values(objects)) {
    if (obj[keyToFind] !== undefined) {
      return obj[keyToFind]
    }

    for (const key in obj) {
      if (typeof[key] === 'object') {
        const nestedValue = getDataLayerValue(keyToFind, [
          obj[key] as unknow as DataLayerType,
        ])
          nestedValue !== undefined && nestedValue;
      }
    }
  }
  return undefined
}

And using the function

useEffect(() => {
  const result = typeof window !== 'undefined' && (window as any).dataLayer;
  console.log(result) // receive data every time when I refresh the page
  
  const brazeCookie = getDataLayerValue('Braze', result)
  console.log(brazeCookie)
}, [])

The problem is, on refresh page, the second console it returns undefined and the first console return every time data. Why I can not access dataLayer when I refresh the page?

I've tried to put the data in localStorage, to have access, but after a refresh page and clean all the cookies, in the localStorage, the value is undefined.

How can I have access to dataLayer every time? Is this a problem from usercentric?

Paleo
  • 3
  • 3

0 Answers0