11'use client'
2-
2+ import { ReadonlyURLSearchParams , usePathname , useSearchParams } from 'next/navigation'
33import React , { ReactNode , useCallback , useContext , useEffect , useRef , useState } from 'react'
44import { track } from 'utils/matomo'
55
66const TrackingContext = React . createContext < {
77 trackOnce : ( action : string ) => void
88} | null > ( null )
99
10+ const trackingTime : Record < string , number | ( ( pathname : string , params : ReadonlyURLSearchParams ) => number ) > = {
11+ // Simulateur
12+ Livraison : 30000 ,
13+ 'Fruits et légumes' : 30000 ,
14+ Alimentation : 30000 ,
15+ Chauffage : 30000 ,
16+ 'Usage numérique' : 30000 ,
17+ Télétravail : 30000 ,
18+ OsezChanger : 30000 ,
19+ Quiz : 30000 ,
20+ Comparateur : 30000 ,
21+ // Graphique dynamique
22+ Numérique : 30000 ,
23+ Habillement : 30000 ,
24+ Mobilier : 30000 ,
25+ Boisson : 30000 ,
26+ Électroménager : 30000 ,
27+ Repas : 30000 ,
28+ // Transport
29+ Transport : ( pathname : string , params : ReadonlyURLSearchParams ) => {
30+ if ( pathname . includes ( 'itineraire' ) ) {
31+ return 15000
32+ }
33+ const defaultMode = params . get ( 'defaultMode' )
34+ if ( defaultMode === 'comparison' ) {
35+ return 15000
36+ }
37+
38+ return 30000
39+ } ,
40+ }
41+
1042export function TrackingProvider ( { children, tracking } : { children : ReactNode ; tracking : string } ) {
1143 // inspired from https://usehooks-ts.com/react-hook/use-intersection-observer
1244 const ref = useRef < HTMLDivElement | null > ( null )
1345 const [ entry , setEntry ] = useState < IntersectionObserverEntry > ( )
1446
1547 const hasTracked = useRef ( false )
1648 const timeoutRef = useRef < NodeJS . Timeout | null > ( null )
49+ const params = useSearchParams ( )
50+ const pathname = usePathname ( )
1751
1852 const trackOnce = useCallback (
1953 ( action : string ) => {
@@ -31,7 +65,7 @@ export function TrackingProvider({ children, tracking }: { children: ReactNode;
3165 )
3266
3367 useEffect ( ( ) => {
34- const node = ref . current // DOM Ref
68+ const node = ref . current
3569 const hasIOSupport = ! ! window . IntersectionObserver
3670 if ( ! hasIOSupport || ! node ) {
3771 return
@@ -45,18 +79,20 @@ export function TrackingProvider({ children, tracking }: { children: ReactNode;
4579 useEffect ( ( ) => {
4680 if ( entry ) {
4781 if ( entry . isIntersecting ) {
48- timeoutRef . current = setTimeout (
49- ( ) => {
50- trackOnce ( 'Temps' )
51- } ,
52- tracking . toLowerCase ( ) . replace ( / é / g, 'e' ) . includes ( 'etiquette' ) ? 15000 : 45000
53- )
82+ // Par défaut 15s sur les étiquettes et les infographies
83+ let timing = trackingTime [ tracking ] || 15000
84+ if ( typeof timing !== 'number' ) {
85+ timing = timing ( pathname , params )
86+ }
87+ timeoutRef . current = setTimeout ( ( ) => {
88+ trackOnce ( 'Temps' )
89+ } , timing )
5490 } else if ( timeoutRef . current ) {
5591 clearTimeout ( timeoutRef . current )
5692 timeoutRef . current = null
5793 }
5894 }
59- } , [ tracking , entry ] )
95+ } , [ tracking , entry , params , pathname ] )
6096
6197 return (
6298 < TrackingContext . Provider
0 commit comments