-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SSP-1960] added logic for orgering GTM events (#2921)
- Loading branch information
Showing
11 changed files
with
82 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useRouter } from 'next/router'; | ||
import React, { useState, createContext, useEffect } from 'react'; | ||
|
||
export type GtmContextType = { | ||
didPageViewRun: boolean; | ||
setDidPageViewRun: (newState: boolean) => void; | ||
}; | ||
|
||
const defaultState: GtmContextType = { | ||
didPageViewRun: false, | ||
setDidPageViewRun: () => undefined, | ||
}; | ||
|
||
export const GtmContext = createContext(defaultState); | ||
|
||
export const GtmProvider: FC = ({ children }) => { | ||
const [didPageViewRun, setDidPageViewRun] = useState(defaultState.didPageViewRun); | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
const onRouteChangeStart = () => { | ||
setDidPageViewRun(false); | ||
}; | ||
|
||
router.events.on('routeChangeStart', onRouteChangeStart); | ||
|
||
return () => { | ||
router.events.off('routeChangeStart', onRouteChangeStart); | ||
}; | ||
}, [router.events]); | ||
|
||
return <GtmContext.Provider value={{ didPageViewRun, setDidPageViewRun }}>{children}</GtmContext.Provider>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { GtmContext, GtmContextType } from './GtmProvider'; | ||
import { useContext } from 'react'; | ||
|
||
export const useGtmContext = (): GtmContextType => { | ||
const context = useContext(GtmContext); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (!context) { | ||
throw new Error('useGtmContext must be used within a GtmProvider'); | ||
} | ||
|
||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
storefront/gtm/hooks/useGtmContactInformationPageViewEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { useGtmContext } from 'gtm/context/useGtmContext'; | ||
import { getGtmContactInformationPageViewEvent } from 'gtm/helpers/eventFactories'; | ||
import { gtmSafePushEvent } from 'gtm/helpers/gtm'; | ||
import { GtmPageViewEventType } from 'gtm/types/events'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export const useGtmContactInformationPageViewEvent = (gtmPageViewEvent: GtmPageViewEventType): void => { | ||
const wasViewedRef = useRef(false); | ||
const { didPageViewRun } = useGtmContext(); | ||
|
||
useEffect(() => { | ||
if (gtmPageViewEvent._isLoaded && gtmPageViewEvent.cart && !wasViewedRef.current) { | ||
if (didPageViewRun && gtmPageViewEvent._isLoaded && gtmPageViewEvent.cart && !wasViewedRef.current) { | ||
wasViewedRef.current = true; | ||
gtmSafePushEvent(getGtmContactInformationPageViewEvent(gtmPageViewEvent.cart)); | ||
} | ||
}, [gtmPageViewEvent._isLoaded, gtmPageViewEvent.cart]); | ||
}, [gtmPageViewEvent._isLoaded, gtmPageViewEvent.cart, didPageViewRun]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters