Skip to content

Commit

Permalink
Fix persisting of forced ab test to localstorage (#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeii authored Nov 12, 2024
1 parent 6bc1ec6 commit 9c0e78d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-ads-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/commercial': minor
---

Fix persisting of forced ab tests to localstorage
12 changes: 7 additions & 5 deletions docs/ab-testing/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

### Setup

1. Follow steps 1-6 in [the DCR documentation](https://github.com/guardian/frontend/blob/main/common/app/conf/switches/ABTestSwitches.scala)
1. Create a test in [src/experiments/tests](https://github.com/guardian/commercial-core/blob/main/src/experiments/tests)
1. Add the test to [concurrent tests](https://github.com/guardian/commercial-core/blob/main/src/experiments/ab-tests.ts)
1. Follow steps 1-6 in [the DCR documentation](https://github.com/guardian/dotcom-rendering/blob/main/dotcom-rendering/docs/development/ab-testing-in-dcr.md)
1. Create a test in [src/experiments/tests](https://github.com/guardian/commercial/blob/main/src/experiments/tests)
1. Add the test to [concurrent tests](https://github.com/guardian/commercial/blob/main/src/experiments/ab-tests.ts)

### Example usage

```ts
import { isInVariantSynchronous } from 'experiments/ab';
import { isUserInVariant } from 'experiments/ab';
import { sectionAdDensity } from 'experiments/tests/section-ad-density';

const isInVariant = isInVariantSynchronous(sectionAdDensity, 'variant');
const isInVariant = isUserInVariant(sectionAdDensity, 'variant');
```

### How to test
Expand All @@ -23,6 +23,8 @@ Use the URL opt-in link to force yourself into a particular variant, e.g. `http:

If you test has multiple variants, you can test each one by updating the `yourVariant` part of the above URL.

To remove yourself from the test unset the varaint e.g. `http://localhost:3030/Front/https://www.theguardian.com/uk#ab-yourTest=`

## Server-side tests

### Setup
Expand Down
6 changes: 6 additions & 0 deletions src/core/lib/ab-localstorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ export const getParticipationsFromLocalStorage = (): Participations => {
const participations = storage.local.get(participationsKey);
return isParticipations(participations) ? participations : {};
};

export const setParticipationsInLocalStorage = (
participations: Participations,
): void => {
storage.local.set(participationsKey, participations);
};
14 changes: 10 additions & 4 deletions src/experiments/ab-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ export const getForcedParticipationsFromUrl = (): Participations => {

return tokens.reduce((obj, token) => {
const [testId, variantId] = token.split('=');
if (testId && variantId) {
if (testId) {
if (variantId) {
return {
...obj,
[testId]: {
variant: variantId,
},
};
}
return {
...obj,
[testId]: {
variant: variantId,
},
[testId]: undefined,
};
}
return obj;
Expand Down
13 changes: 12 additions & 1 deletion src/experiments/ab.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { ABTest } from '@guardian/ab-core';
import { AB } from '@guardian/ab-core';
import { getCookie, log } from '@guardian/libs';
import {
getParticipationsFromLocalStorage,
setParticipationsInLocalStorage,
} from '../core/lib/ab-localstorage';
import { concurrentTests } from './ab-tests';
import { getForcedParticipationsFromUrl } from './ab-url';

Expand Down Expand Up @@ -71,13 +75,20 @@ const abTestSwitches = Object.entries(window.guardian.config.switches).reduce(
);

const init = () => {
const forcedTestVariants = {
...getParticipationsFromLocalStorage(),
...getForcedParticipationsFromUrl(),
};

setParticipationsInLocalStorage(forcedTestVariants);

const ab = new AB({
mvtId: mvtId ?? -1,
mvtMaxValue,
pageIsSensitive: window.guardian.config.page.isSensitive,
abTestSwitches,
arrayOfTestObjects: concurrentTests,
forcedTestVariants: getForcedParticipationsFromUrl(),
forcedTestVariants,
ophanRecord: getOphan().record,
serverSideTests: window.guardian.config.tests ?? {},
errorReporter: (error) => {
Expand Down

0 comments on commit 9c0e78d

Please sign in to comment.