Skip to content

Commit 38b319b

Browse files
authored
Loop video tracking (#14172)
* Add `videoType` parameter to `ophanTrackerWeb` so it can be used for varied video event tracking * Add first time in view and first play tracking
1 parent deb2bfc commit 38b319b

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

dotcom-rendering/src/components/LoopVideo.importable.tsx

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { css } from '@emotion/react';
22
import { log, storage } from '@guardian/libs';
33
import { SvgAudio, SvgAudioMute } from '@guardian/source/react-components';
44
import { useCallback, useEffect, useRef, useState } from 'react';
5-
import { submitClickComponentEvent } from '../client/ophan/ophan';
5+
import {
6+
submitClickComponentEvent,
7+
submitComponentEvent,
8+
} from '../client/ophan/ophan';
69
import { getZIndex } from '../lib/getZIndex';
710
import { useIsInView } from '../lib/useIsInView';
811
import { useShouldAdapt } from '../lib/useShouldAdapt';
@@ -14,6 +17,7 @@ import {
1417
import { useConfig } from './ConfigContext';
1518
import type { PLAYER_STATES, PlayerStates } from './LoopVideoPlayer';
1619
import { LoopVideoPlayer } from './LoopVideoPlayer';
20+
import { ophanTrackerWeb } from './YoutubeAtom/eventEmitters';
1721

1822
const videoContainerStyles = css`
1923
z-index: ${getZIndex('loop-video-container')};
@@ -212,9 +216,23 @@ export const LoopVideo = ({
212216

213217
useEffect(() => {
214218
if (isInView && !hasBeenInView) {
219+
/**
220+
* Track the first time the video comes into view.
221+
*/
222+
void submitComponentEvent(
223+
{
224+
component: {
225+
componentType: 'LOOP_VIDEO',
226+
id: `gu-video-loop-${atomId}`,
227+
},
228+
action: 'VIEW',
229+
},
230+
'Web',
231+
);
232+
215233
setHasBeenInView(true);
216234
}
217-
}, [isInView, hasBeenInView]);
235+
}, [isInView, hasBeenInView, atomId]);
218236

219237
/**
220238
* Autoplay the video when it comes into view.
@@ -230,9 +248,25 @@ export const LoopVideo = ({
230248
(playerState === 'NOT_STARTED' ||
231249
playerState === 'PAUSED_BY_INTERSECTION_OBSERVER')
232250
) {
251+
/**
252+
* check if the video has not been in view before tracking the play.
253+
* This is so we only track the first play.
254+
*/
255+
if (!hasBeenInView) {
256+
ophanTrackerWeb(atomId, 'loop')('play');
257+
}
258+
233259
void playVideo();
234260
}
235-
}, [isAutoplayAllowed, isInView, isPlayable, playerState, playVideo]);
261+
}, [
262+
isAutoplayAllowed,
263+
isInView,
264+
isPlayable,
265+
playerState,
266+
playVideo,
267+
hasBeenInView,
268+
atomId,
269+
]);
236270

237271
/**
238272
* Stops playback when the video is scrolled out of view, resumes playbacks

dotcom-rendering/src/components/YoutubeAtom/eventEmitters.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ const getAppsMediaEvent = (
2626
}
2727
};
2828

29-
const ophanTrackerWeb = (id: string) => {
29+
type VideoType = 'youtube' | 'loop';
30+
31+
const ophanTrackerWeb = (id: string, videoType: VideoType) => {
3032
return (trackingEvent: VideoEventKey): void => {
3133
void getOphan('Web').then((ophan) => {
3234
const event = {
3335
video: {
34-
id: `gu-video-youtube-${id}`,
36+
id: `gu-video-${videoType}-${id}`,
3537
eventType: `video:content:${trackingEvent}`,
3638
} satisfies VideoEvent,
3739
} satisfies EventPayload;
3840
log('dotcom', {
39-
from: 'YoutubeAtom event emitter web',
41+
from: `${videoType}Atom event emitter web`,
4042
id,
4143
event,
4244
});

dotcom-rendering/src/components/YoutubeBlockComponent.importable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export const YoutubeBlockComponent = ({
172172
duration={duration}
173173
eventEmitters={
174174
renderingTarget === 'Web'
175-
? [ophanTrackerWeb(id)]
175+
? [ophanTrackerWeb(id, 'youtube')]
176176
: [ophanTrackerApps(id)]
177177
}
178178
format={format}

0 commit comments

Comments
 (0)