Skip to content

Commit

Permalink
Add more variants to frequencyCap test for testing timeout (#1573)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeii authored Sep 9, 2024
1 parent d7acd64 commit 90e1a30
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-adults-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/commercial': patch
---

Add more variants to frequencycap test for testing timeout
6 changes: 6 additions & 0 deletions src/experiments/ab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ export const isUserInVariant = (test: ABTest, variantId: string): boolean => {
const ab = init();
return ab.isUserInVariant(test.id, variantId);
};

export const getVariant = (test: ABTest): string | undefined => {
const participations = getParticipations();

return participations[test.id]?.variant;
};
48 changes: 48 additions & 0 deletions src/experiments/tests/opt-out-frequency-cap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,54 @@ export const optOutFrequencyCap: ABTest = {
/* no-op */
},
},
{
id: 'timeout-0',
test: (): void => {
/* no-op */
},
},
{
id: 'timeout-10',
test: (): void => {
/* no-op */
},
},
{
id: 'timeout-15',
test: (): void => {
/* no-op */
},
},
{
id: 'timeout-20',
test: (): void => {
/* no-op */
},
},
{
id: 'timeout-25',
test: (): void => {
/* no-op */
},
},
{
id: 'timeout-30',
test: (): void => {
/* no-op */
},
},
{
id: 'timeout-50',
test: (): void => {
/* no-op */
},
},
{
id: 'timeout-100',
test: (): void => {
/* no-op */
},
},
],
canRun: () => true,
};
26 changes: 20 additions & 6 deletions src/init/consentless/prepare-ootag.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import type { ConsentState } from '@guardian/libs';
import { loadScript, log } from '@guardian/libs';
import { buildPageTargetingConsentless } from 'core/targeting/build-page-targeting-consentless';
import { isUserInVariant } from 'experiments/ab';
import { getVariant } from 'experiments/ab';
import { optOutFrequencyCap } from 'experiments/tests/opt-out-frequency-cap';
import { commercialFeatures } from 'lib/commercial-features';
import { isUserLoggedInOktaRefactor } from 'lib/identity/api';

const frequencyCapTimeoutFromVariant = (variant: string): number => {
if (!variant.startsWith('timeout-')) {
return 0;
}
const timeout = variant.replace('timeout-', '');
return parseInt(timeout, 10);
};

function initConsentless(consentState: ConsentState): Promise<void> {
return new Promise((resolve) => {
// Stub the command queue
Expand All @@ -14,17 +22,22 @@ function initConsentless(consentState: ConsentState): Promise<void> {
queue: [],
};

const isInFrequencyCapTest = isUserInVariant(
optOutFrequencyCap,
'variant',
);

window.ootag.queue.push(function () {
// Ensures Opt Out logs are namespaced under Commercial
window.ootag.logger = (...args: unknown[]) => {
log('commercial', '[Opt Out Ads]', ...args);
};

const frequencyCapVariant = getVariant(optOutFrequencyCap);

const isInFrequencyCapTest =
frequencyCapVariant !== undefined &&
frequencyCapVariant !== 'control';

const timeoutFrequencyCappingMS = isInFrequencyCapTest
? frequencyCapTimeoutFromVariant(frequencyCapVariant)
: undefined;

window.ootag.initializeOo({
publisher: 33,
// We set our own custom logger above
Expand All @@ -34,6 +47,7 @@ function initConsentless(consentState: ConsentState): Promise<void> {
frequencyScript: isInFrequencyCapTest
? 'https://frequencycappingwithoutpersonaldata.com/script/iframe'
: undefined,
timeoutFrequencyCappingMS,
});

void isUserLoggedInOktaRefactor().then((isSignedIn) => {
Expand Down
1 change: 1 addition & 0 deletions src/types/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ interface OptOutInitializeOptions {
lazyLoading?: { fractionInView?: number; viewPortMargin?: string };
noRequestsOnPageLoad?: 0 | 1;
frequencyScript?: string;
timeoutFrequencyCappingMS?: number;
debug_forceCap?: number;
}

Expand Down

0 comments on commit 90e1a30

Please sign in to comment.