4
4
SiteInfo ,
5
5
useSitecoreContext ,
6
6
} from '@sitecore-jss/sitecore-jss-nextjs' ;
7
- import { useEffect } from 'react' ;
7
+ import { useCallback , useEffect } from 'react' ;
8
8
import config from 'temp/config' ;
9
9
import { init } from '@sitecore/engage' ;
10
10
import { siteResolver } from 'lib/site-resolver' ;
@@ -27,30 +27,26 @@ const CdpPageView = (): JSX.Element => {
27
27
/**
28
28
* Creates a page view event using the Sitecore Engage SDK.
29
29
*/
30
- const createPageView = async (
31
- page : string ,
32
- language : string ,
33
- site : SiteInfo ,
34
- pageVariantId : string
35
- ) => {
36
- const pointOfSale = resolvePointOfSale ( site , language ) ;
37
- const engage = await init ( {
38
- clientKey : process . env . NEXT_PUBLIC_CDP_CLIENT_KEY || '' ,
39
- targetURL : process . env . NEXT_PUBLIC_CDP_TARGET_URL || '' ,
40
- // Replace with the top level cookie domain of the website that is being integrated e.g ".example.com" and not "www.example.com"
41
- cookieDomain : window . location . hostname . replace ( / ^ w w w \. / , '' ) ,
42
- // Cookie may be created in personalize middleware (server), but if not we should create it here
43
- forceServerCookieMode : false ,
44
- } ) ;
45
- engage . pageView ( {
46
- channel : 'WEB' ,
47
- currency : 'USD' ,
48
- pointOfSale,
49
- page,
50
- pageVariantId,
51
- language,
52
- } ) ;
53
- } ;
30
+ const createPageView = useCallback (
31
+ async ( page : string , language : string , site : SiteInfo , pageVariantId : string ) => {
32
+ const pointOfSale = resolvePointOfSale ( site , language ) ;
33
+ const engage = await init ( {
34
+ clientKey : process . env . NEXT_PUBLIC_CDP_CLIENT_KEY || '' ,
35
+ targetURL : process . env . NEXT_PUBLIC_CDP_TARGET_URL || '' ,
36
+ cookieDomain : window . location . hostname . replace ( / ^ w w w \. / , '' ) ,
37
+ forceServerCookieMode : false ,
38
+ } ) ;
39
+ engage . pageView ( {
40
+ channel : 'WEB' ,
41
+ currency : 'USD' ,
42
+ pointOfSale,
43
+ page,
44
+ pageVariantId,
45
+ language,
46
+ } ) ;
47
+ } ,
48
+ [ ] // Empty dependency array, so it will only be created once.
49
+ ) ;
54
50
55
51
/**
56
52
* Determines if the page view events should be turned off.
@@ -87,7 +83,7 @@ const CdpPageView = (): JSX.Element => {
87
83
scope
88
84
) ;
89
85
createPageView ( route . name , language , siteInfo , pageVariantId ) ;
90
- } , [ pageState , route , variantId , site ] ) ;
86
+ } , [ pageState , route , variantId , site , createPageView ] ) ;
91
87
92
88
return < > </ > ;
93
89
} ;
0 commit comments