@@ -30,6 +30,8 @@ import type {
3030import { SelfHostedVideoPlayer } from './SelfHostedVideoPlayer' ;
3131import { ophanTrackerWeb } from './YoutubeAtom/eventEmitters' ;
3232
33+ const VISIBILITY_THRESHOLD = 0.5 ;
34+
3335const videoContainerStyles = (
3436 isCinemagraph : boolean ,
3537 aspectRatio : number ,
@@ -98,6 +100,26 @@ const logAndReportError = (src: string, error: Error) => {
98100 log ( 'dotcom' , message ) ;
99101} ;
100102
103+ /**
104+ * Initiates attention tracking for ophan
105+ */
106+ const trackAttention = async (
107+ videoElement : HTMLVideoElement ,
108+ atomId : string ,
109+ ) => {
110+ try {
111+ const ophan = await getOphan ( 'Web' ) ;
112+ ophan . trackComponentAttention (
113+ `gu-video-loop-${ atomId } ` ,
114+ videoElement ,
115+ VISIBILITY_THRESHOLD ,
116+ true ,
117+ ) ;
118+ } catch ( error ) {
119+ log ( 'dotcom' , 'Failed to track video attention:' , error ) ;
120+ }
121+ } ;
122+
101123const dispatchOphanAttentionEvent = (
102124 eventType : 'videoPlaying' | 'videoPause' ,
103125) => {
@@ -198,8 +220,6 @@ export const SelfHostedVideo = ({
198220 const [ width , setWidth ] = useState ( expectedWidth ) ;
199221 const [ height , setHeight ] = useState ( expectedHeight ) ;
200222
201- const VISIBILITY_THRESHOLD = 0.5 ;
202-
203223 /**
204224 * All controls on the video are hidden: the video looks like a GIF.
205225 * This includes but may not be limited to: audio icon, play/pause icon, subtitles, progress bar.
@@ -355,6 +375,13 @@ export const SelfHostedVideo = ({
355375 setHasPageBecomeActive ( true ) ;
356376 } ;
357377
378+ /**
379+ * Initialise Ophan attention tracking
380+ */
381+ if ( vidRef . current ) {
382+ void trackAttention ( vidRef . current , atomId ) ;
383+ }
384+
358385 document . addEventListener (
359386 customSelfHostedVideoPlayAudioEventName ,
360387 handleCustomPlayAudioEvent ,
@@ -388,31 +415,7 @@ export const SelfHostedVideo = ({
388415 handlePageBecomesVisible ( ) ;
389416 } ) ;
390417 } ;
391- } , [ uniqueId ] ) ;
392-
393- /**
394- * Initiates attention tracking for ophan
395- */
396- useEffect ( ( ) => {
397- const video = vidRef . current ;
398- if ( ! video ) return ;
399-
400- const trackAttention = async ( ) => {
401- try {
402- const ophan = await getOphan ( 'Web' ) ;
403- ophan . trackComponentAttention (
404- `gu-video-loop-${ atomId } ` ,
405- video ,
406- VISIBILITY_THRESHOLD ,
407- true ,
408- ) ;
409- } catch ( error ) {
410- log ( 'dotcom' , 'Failed to track video attention:' , error ) ;
411- }
412- } ;
413-
414- void trackAttention ( ) ;
415- } , [ atomId ] ) ;
418+ } , [ uniqueId , atomId ] ) ;
416419
417420 /**
418421 * Keeps track of whether the video has been in view or not.
0 commit comments