11'use client'
2+ import { sendGTMEvent } from '@next/third-parties/google'
3+ import { useCallback , useEffect , useState } from 'react'
4+ import type Player from 'video.js/dist/types/player'
25import useConsent from '../../lib/hooks/useConsent'
3- import { pushToDataLayer } from '../../lib/gtm'
4- import { useEffect , useCallback , useState } from 'react'
5- import Player from 'video.js/dist/types/player'
66
77const GTM_PLAY_EVENT = 'video_play'
88const GTM_PAUSE_EVENT = 'video_pause'
99const GTM_COMPLETION_EVENT = 'video_completion'
1010const GTM_PROGRESS_MILESTONES = [ 25 , 50 , 75 , 90 ] // Percentages
1111
12- type EventType = typeof GTM_PLAY_EVENT | typeof GTM_PAUSE_EVENT | typeof GTM_COMPLETION_EVENT | string
12+ type EventType =
13+ | typeof GTM_PLAY_EVENT
14+ | typeof GTM_PAUSE_EVENT
15+ | typeof GTM_COMPLETION_EVENT
16+ | string
1317
1418type EventData = {
1519 eventType : EventType
@@ -20,7 +24,12 @@ type EventData = {
2024}
2125
2226// Video Analytics Hook
23- const useVideojsAnalytics = ( player : Player | null , src : string , title ?: string , autoPlay ?: boolean ) : void => {
27+ const useVideojsAnalytics = (
28+ player : Player | null ,
29+ src : string ,
30+ title ?: string ,
31+ autoPlay ?: boolean ,
32+ ) : void => {
2433 const allowAnalytics = useConsent ( [ 'statistics' ] ) || false
2534
2635 const pushEventToDataLayer = useCallback (
@@ -33,7 +42,7 @@ const useVideojsAnalytics = (player: Player | null, src: string, title?: string,
3342 currentTime : player . currentTime ( ) || 0 ,
3443 src,
3544 }
36- pushToDataLayer ( 'video_event' , eventData )
45+ sendGTMEvent ( { event : 'video_event' , ... eventData } )
3746 }
3847 } ,
3948 [ title , src , autoPlay ] ,
@@ -120,10 +129,10 @@ const useVideoProgressEvent = (
120129 const currentTime = player . currentTime ( )
121130 if ( currentTime && duration ) {
122131 const progress = ( currentTime / duration ) * 100
123- GTM_PROGRESS_MILESTONES . forEach ( ( milestone ) => {
132+ GTM_PROGRESS_MILESTONES . forEach ( milestone => {
124133 if ( progress >= milestone && ! trackedMilestones . includes ( milestone ) ) {
125134 pushEvent ( `video_progress_${ milestone } ` , player )
126- setTrackedMilestones ( ( prev ) => [ ...prev , milestone ] )
135+ setTrackedMilestones ( prev => [ ...prev , milestone ] )
127136 }
128137 } )
129138 }
0 commit comments