Skip to content

Commit

Permalink
Record interscroller progress with logs and metrics (#1497)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeii authored Aug 6, 2024
1 parent 0ba81d2 commit 28c1b56
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-cobras-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/commercial': minor
---

Log interscroller progress with logs and metrics
54 changes: 54 additions & 0 deletions src/core/lib/video-interscroller-progress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { EventTimer } from 'core/event-timer';
import { bypassMetricsSampling } from 'experiments/utils';

const endpoint = window.guardian.config.page.isDev
? '//logs.code.dev-guardianapis.com/log'
: '//logs.guardianapis.com/log';

let creativeId: number | undefined;
let progress: number = 0;

const sendProgress = () => {
if (!creativeId || !progress) {
return;
}
void fetch(endpoint, {
method: 'POST',
body: JSON.stringify({
label: 'commercial.interscroller.videoProgress',
properties: [
{ name: 'id', value: creativeId },
{ name: 'progress', value: progress },
],
}),
keepalive: true,
cache: 'no-store',
mode: 'no-cors',
});
};

const sendProgressOnUnloadViaLogs = () => {
window.addEventListener('visibilitychange', sendProgress, { once: true });
// Safari does not reliably fire the `visibilitychange` on page unload.
window.addEventListener('pagehide', sendProgress, { once: true });
};

const initVideoProgressReporting = (gamCreativeId: number) => {
creativeId = gamCreativeId;

EventTimer.get().setProperty('videoInterscrollerCreativeId', creativeId);

bypassMetricsSampling();

sendProgressOnUnloadViaLogs();
};

const updateVideoProgress = (updatedProgress: number) => {
progress = updatedProgress;
EventTimer.get().setProperty(
'videoInterscrollerPercentageProgress',
updatedProgress,
);
};

export { initVideoProgressReporting, updateVideoProgress };
25 changes: 13 additions & 12 deletions src/core/messenger/background.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { isObject } from '@guardian/libs';
import { EventTimer } from 'core/event-timer';
import {
initVideoProgressReporting,
updateVideoProgress,
} from 'core/lib/video-interscroller-progress';
import type { RegisterListener } from 'core/messenger';
import { bypassCommercialMetricsSampling } from 'core/send-commercial-metrics';
import fastdom from 'utils/fastdom-promise';
import {
renderAdvertLabel,
Expand Down Expand Up @@ -239,7 +241,10 @@ const setupBackground = async (
const creativeTemplateId =
slot.getResponseInformation()?.creativeTemplateId;
if (creativeTemplateId === 11885667) {
return slot.getResponseInformation()?.creativeId;
return (
slot.getResponseInformation()?.creativeId ??
undefined
);
}
}
return undefined;
Expand All @@ -263,21 +268,17 @@ const setupBackground = async (

observer.observe(backgroundParent);

EventTimer.get().setProperty(
'videoInterscrollerCreativeId',
getCreativeId(),
);
const creativeId = getCreativeId();

void bypassCommercialMetricsSampling();
if (creativeId) {
initVideoProgressReporting(creativeId);
}

video.ontimeupdate = function () {
const percent = Math.round(
100 * (video.currentTime / video.duration),
);
EventTimer.get().setProperty(
'videoInterscrollerPercentageProgress',
percent,
);
updateVideoProgress(percent);
};
}
} else {
Expand Down

0 comments on commit 28c1b56

Please sign in to comment.