-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Record interscroller progress with logs and metrics (#1497)
- Loading branch information
Showing
3 changed files
with
72 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@guardian/commercial': minor | ||
--- | ||
|
||
Log interscroller progress with logs and metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters