Skip to content

Commit d65433a

Browse files
authored
Merge pull request guardian#24077 from guardian/toggle-force-send-metrics
Force send core web vital data for Improve/Collective skin
2 parents 756f7a7 + 2aaf3ee commit d65433a

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

static/src/javascripts/projects/commercial/modules/header-bidding/prebid/prebid.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isString, log } from '@guardian/libs';
33
import { captureCommercialMetrics } from 'commercial/commercial-metrics';
44
import type { Advert } from 'commercial/modules/dfp/Advert';
55
import config from '../../../../../lib/config';
6+
import { setForceSendMetrics } from '../../../../common/modules/analytics/forceSendMetrics';
67
import { dfpEnv } from '../../dfp/dfp-env';
78
import { getAdvertById } from '../../dfp/get-advert-by-id';
89
import { getHeaderBiddingAdSlots } from '../slot-config';
@@ -330,7 +331,14 @@ const initialise = (window: Window, framework = 'tcfv2'): void => {
330331
* */
331332
advert.hasPrebidSize = true;
332333

333-
if (data.bidderCode === 'improvedigital') captureCommercialMetrics();
334+
/**
335+
* If improve skin has won auction, toggle force send metrics on.
336+
* TODO remove after improve skin release.
337+
* */
338+
if (data.bidderCode === 'improvedigital') {
339+
setForceSendMetrics(true);
340+
captureCommercialMetrics();
341+
}
334342
});
335343
};
336344

static/src/javascripts/projects/common/modules/analytics/coreVitals.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getCookie } from '@guardian/libs';
22
import { getCLS, getFCP, getFID, getLCP, getTTFB } from 'web-vitals';
33
import reportError from 'lib/report-error';
4+
import { forceSendMetrics } from './forceSendMetrics';
45
import { shouldCaptureMetrics } from './shouldCaptureMetrics';
56

67
type CoreWebVitalsPayload = {
@@ -30,21 +31,19 @@ const jsonData: CoreWebVitalsPayload = {
3031
ttfb: null,
3132
};
3233

34+
// By default, sample 1% of users
35+
const userInSample = Math.random() < 1 / 100;
36+
// unless we are forcing metrics for this user because they are participating in an AB test
37+
// for which we need to capture all metrics
38+
const captureMetrics = shouldCaptureMetrics();
39+
// or we are force sending for this page view for some other reason with forceSendMetrics.
40+
3341
/**
34-
* Sends core web vitals data for a sample of page views to the data lake.
42+
* Calls functions of web-vitals library to collect core web vitals data, registering callbacks which
43+
* send it to the data lake for a sample of page views.
3544
* Equivalent dotcom-rendering functionality is here: https://git.io/JBRIt
3645
*/
3746
export const coreVitals = (): void => {
38-
// By default, sample 1% of users
39-
const inSample = Math.random() < 1 / 100;
40-
41-
// Unless we are forcing metrics for this user
42-
const captureMetrics = shouldCaptureMetrics();
43-
44-
if (!captureMetrics && !inSample) {
45-
return;
46-
}
47-
4847
type CoreVitalsArgs = {
4948
name: string;
5049
value: number;
@@ -59,6 +58,10 @@ export const coreVitals = (): void => {
5958
};
6059

6160
const jsonToSend = ({ name, value }: CoreVitalsArgs): void => {
61+
if (!captureMetrics && !userInSample && !forceSendMetrics) {
62+
return;
63+
}
64+
6265
switch (name) {
6366
case 'FCP':
6467
jsonData.fcp = nineDigitPrecision(value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export let forceSendMetrics = false;
2+
3+
export const setForceSendMetrics = (val: boolean): void => {
4+
forceSendMetrics = val;
5+
};

0 commit comments

Comments
 (0)