Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix events not being pushed after page route #2316 #2319

Merged
merged 8 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions web/lib/hooks/useConsent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { ConsentType } from './useConsentState'

const getConsentState = (consentType: ConsentType): boolean => {
// Prevents SSR issues
if (typeof window !== 'undefined') {
switch (consentType) {
case 'statistics':
return window.Cookiebot.consent.statistics
case 'marketing':
return window.Cookiebot.consent.marketing
default:
return false
}
}
return false
}

/**
* Returns true if the consent is given for the given consentType.
* @param consentType Can be either marketing or statistics
* @returns
*/
export default function useConsent(consentType: ConsentType): boolean | undefined {
const [consent, setConsent] = useState<boolean>(getConsentState(consentType))
const router = useRouter()
useEffect(() => {
setConsent(getConsentState(consentType))
}, [consentType, router.asPath])
return consent
}
12 changes: 3 additions & 9 deletions web/lib/hooks/useVideojsAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import useConsentState from './useConsentState'
import useConsent from './useConsent'
import { pushToDataLayer } from '../../lib/gtm'
import { useEffect, useCallback, useState } from 'react'
import Player from 'video.js/dist/types/player'
Expand All @@ -20,13 +20,7 @@ type EventData = {

// Video Analytics Hook
const useVideojsAnalytics = (player: Player | null, src: string, title?: string, autoPlay?: boolean): void => {
const [allowAnalytics, setAllowAnalytics] = useState(false)

useConsentState(
'statistics',
() => setAllowAnalytics(true),
() => setAllowAnalytics(false),
)
const allowAnalytics = useConsent('statistics') || false

const pushEventToDataLayer = useCallback(
(eventType: EventType, player: Player) => {
Expand Down Expand Up @@ -121,7 +115,7 @@ const useVideoProgressEvent = (
if (!player) return
const intervalId = setInterval(() => {
const duration = player.duration()
if ((!allowAnalytics || !duration) && !player) return
if (!(allowAnalytics || duration || player)) return
const currentTime = player.currentTime()
if (currentTime && duration) {
const progress = (currentTime / duration) * 100
Expand Down
Loading