From 63d3e82187c2dfa21e54d3a37060b57e5646e5fe Mon Sep 17 00:00:00 2001 From: Hana Worku Date: Fri, 22 Mar 2024 15:03:12 -0500 Subject: [PATCH] Add useCallback around callUTagView --- services/app-web/src/hooks/useTealium.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/services/app-web/src/hooks/useTealium.ts b/services/app-web/src/hooks/useTealium.ts index f0ba8c0b7d..ded6094419 100644 --- a/services/app-web/src/hooks/useTealium.ts +++ b/services/app-web/src/hooks/useTealium.ts @@ -1,4 +1,4 @@ -import { useEffect } from 'react' +import { useCallback, useEffect } from 'react' import { usePage } from '../contexts/PageContext' import { useCurrentRoute } from './useCurrentRoute' import { createScript } from './useScript' @@ -36,7 +36,7 @@ const useTealium = (): { user: loggedInUser, }) - const callUTagView = () => { + const callUTagView = useCallback(() => { // eslint-disable-next-line @typescript-eslint/no-empty-function const utag = window.utag || { link: () => {}, view: () => {} } const tagData: TealiumViewDataObject = { @@ -50,7 +50,7 @@ const useTealium = (): { logged_in: `${Boolean(loggedInUser) ?? false}`, } utag.view(tagData) - } + },[currentRoute,tealiumPageName, pathname, loggedInUser]) // Add Tealium setup // this effect should only fire on initial app load @@ -125,22 +125,23 @@ const useTealium = (): { return new Promise(resolve => setTimeout(resolve, 1000)); } - // All of this is a guardrail - protect against trying to call utag before its loaded + if (!window.utag) { - waitForUtag().then( () =>{ + // All of this is a guardrail - protect against trying to call utag before its loaded on initial load + waitForUtag().finally( () =>{ if (!window.utag) { recordJSException('Analytics did not load in time') return } else { callUTagView() } - } - ).catch(() => { return }) + }) } else { + // We are in a SPA page route change after initial load callUTagView() } - }, [currentRoute, loggedInUser, pathname, tealiumPageName]) + }, [currentRoute, loggedInUser, pathname, tealiumPageName, callUTagView]) // Add user event const logTealiumEvent = (linkData: {