11'use client'
22
3+ import { ReadonlyURLSearchParams , usePathname , useSearchParams } from 'next/navigation'
34import React , { ReactNode , useCallback , useContext , useEffect , useRef , useState } from 'react'
45import { track } from 'utils/matomo'
56
67const TrackingContext = React . createContext < {
78 trackOnce : ( action : string ) => void
89} | null > ( null )
910
11+ const trackingTime : Record < string , number | ( ( pathname : string , params : ReadonlyURLSearchParams ) => number ) > = {
12+ Numérique : 15000 ,
13+ Habillement : 15000 ,
14+ Mobilier : 15000 ,
15+ Boisson : 15000 ,
16+ Électroménager : 15000 ,
17+ Repas : 15000 ,
18+ Transport : ( pathname : string , params : ReadonlyURLSearchParams ) => {
19+ if ( pathname . includes ( 'itineraire' ) ) {
20+ return 15000
21+ }
22+ const defaultMode = params . get ( 'defaultMode' )
23+ if ( defaultMode === 'comparison' ) {
24+ return 15000
25+ }
26+
27+ return 30000
28+ } ,
29+ }
30+
1031export function TrackingProvider ( { children, tracking } : { children : ReactNode ; tracking : string } ) {
1132 // inspired from https://usehooks-ts.com/react-hook/use-intersection-observer
1233 const ref = useRef < HTMLDivElement | null > ( null )
1334 const [ entry , setEntry ] = useState < IntersectionObserverEntry > ( )
1435
1536 const hasTracked = useRef ( false )
1637 const timeoutRef = useRef < NodeJS . Timeout | null > ( null )
38+ const params = useSearchParams ( )
39+ const pathname = usePathname ( )
1740
1841 const trackOnce = useCallback (
1942 ( action : string ) => {
@@ -31,7 +54,7 @@ export function TrackingProvider({ children, tracking }: { children: ReactNode;
3154 )
3255
3356 useEffect ( ( ) => {
34- const node = ref . current // DOM Ref
57+ const node = ref . current
3558 const hasIOSupport = ! ! window . IntersectionObserver
3659 if ( ! hasIOSupport || ! node ) {
3760 return
@@ -45,18 +68,19 @@ export function TrackingProvider({ children, tracking }: { children: ReactNode;
4568 useEffect ( ( ) => {
4669 if ( entry ) {
4770 if ( entry . isIntersecting ) {
48- timeoutRef . current = setTimeout (
49- ( ) => {
50- trackOnce ( 'Temps' )
51- } ,
52- tracking . toLowerCase ( ) . replace ( / é / g, 'e' ) . includes ( 'etiquette' ) ? 15000 : 45000
53- )
71+ let timing = trackingTime [ tracking ] || 15000
72+ if ( typeof timing !== 'number' ) {
73+ timing = timing ( pathname , params )
74+ }
75+ timeoutRef . current = setTimeout ( ( ) => {
76+ trackOnce ( 'Temps' )
77+ } , timing )
5478 } else if ( timeoutRef . current ) {
5579 clearTimeout ( timeoutRef . current )
5680 timeoutRef . current = null
5781 }
5882 }
59- } , [ tracking , entry ] )
83+ } , [ tracking , entry , params , pathname ] )
6084
6185 return (
6286 < TrackingContext . Provider
0 commit comments