Skip to content

Commit a9191da

Browse files
committed
feat: manage engament time depending on simulator
1 parent 82f448f commit a9191da

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

app/(iframes)/iframes/livraison/etiquette-animee/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import LivraisonEtiquette from 'components/outils/livraison/LivraisonEtiquette'
55
const page = () => {
66
return (
77
<TranslationProvider>
8-
<TrackingProvider tracking='Livraison etiquette'>
8+
<TrackingProvider tracking='Livraison étiquette'>
99
<LivraisonEtiquette id='animated' animated />
1010
</TrackingProvider>
1111
</TranslationProvider>

src/providers/TrackingProvider.tsx

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
11
'use client'
22

3+
import { ReadonlyURLSearchParams, usePathname, useSearchParams } from 'next/navigation'
34
import React, { ReactNode, useCallback, useContext, useEffect, useRef, useState } from 'react'
45
import { track } from 'utils/matomo'
56

67
const 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+
1031
export 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

Comments
 (0)