Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix experimentation plugin subtree #446

Merged
merged 27 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6f8650f
Merge commit 'c662bc6e8b6371af461e405d5264241219fcdef8' as 'solutions…
vtsaplin Dec 20, 2023
c662bc6
Squashed 'solutions/plugins/experimentation/' content from commit b03…
vtsaplin Dec 20, 2023
4696701
feat: add experimentation plugin
vtsaplin Dec 20, 2023
ebe995b
docs: update readme
vtsaplin Dec 20, 2023
1b9f376
fix: experimentation plugin is not initialized
vtsaplin Dec 20, 2023
8914558
fix: experimentation plugin is not initialized
vtsaplin Dec 20, 2023
3917b61
feat: log experiment details
vtsaplin Jan 3, 2024
3c2aa87
feat: add experiment details
vtsaplin Jan 3, 2024
808b606
feat: add experiment details
vtsaplin Jan 3, 2024
1f47d58
feat: add experiment details
vtsaplin Jan 3, 2024
7b6161c
feat: add experiment details
vtsaplin Jan 3, 2024
b3cb298
feat: add experiment details
vtsaplin Jan 3, 2024
3546179
feat: add experiment details
vtsaplin Jan 3, 2024
da8b65c
feat: add experiment details
vtsaplin Jan 3, 2024
c8a7bf9
feat: add experiment details
vtsaplin Jan 3, 2024
d0d66f6
feat: add experiment details
vtsaplin Jan 3, 2024
3cdcfba
feat: add experiment details
vtsaplin Jan 3, 2024
95b5135
feat: add experiment details
vtsaplin Jan 4, 2024
72c1148
Merge commit '2e2bc6235c38414185a1eb217c5d45272fef9d9c' into experime…
vtsaplin Jan 8, 2024
2e2bc62
Squashed 'solutions/plugins/experimentation/' changes from b033075..1…
vtsaplin Jan 8, 2024
47390b3
docs: update readme
vtsaplin Jan 10, 2024
2de63f0
chore: linting
vtsaplin Jan 10, 2024
e8bf43c
Merge branch 'main' into experimentation
vtsaplin Jan 10, 2024
91abc9b
Squashed 'solutions/plugins/experimentation/' changes from 162896d..2…
vtsaplin Jan 29, 2024
a3c2ab9
Merge commit '91abc9ba92b43f6f74beaf694651ee6a97713f3f' into experime…
vtsaplin Jan 29, 2024
866ec3b
Merge branch 'main' into experimentation
vtsaplin Jan 29, 2024
e066432
chore: merge with remote main
vtsaplin Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions solutions/plugins/experimentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ window.hlx.plugins.add('experimentation', {
});
```

### Without the plugin system
### On top of a regular boilerplate project

To properly connect and configure the plugin for your project, you'll need to edit your `scripts.js` in your AEM project and add the following:
Typically, you'd know you don't have the plugin system if you don't see a reference to `window.hlx.plugins` in your `scripts.js`. In that case, you can still manually instrument this plugin in your project by falling back to a more manual instrumentation. To properly connect and configure the plugin for your project, you'll need to edit your `scripts.js` in your AEM project and add the following:

1. at the start of the file:
```js
Expand Down Expand Up @@ -151,6 +151,10 @@ runEager.call(document, {
// short durations of those campaigns/experiments
rumSamplingRate: 10,

// the storage type used to persist data between page views
// (for instance to remember what variant in an experiment the user was served)
storage: window.SessionStorage,

/* Audiences related properties */
// See more details on the dedicated Audiences page linked below
audiences: {},
Expand Down
6 changes: 3 additions & 3 deletions solutions/plugins/experimentation/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,14 @@ export async function runExperiment(document, options, context) {
}

// Fullpage content experiment
document.body.classList.add(`experiment-${experimentConfig.id}`);
document.body.classList.add(`experiment-${context.toClassName(experimentConfig.id)}`);
const result = await replaceInner(pages[index], document.querySelector('main'));
experimentConfig.servedExperience = result || currentPath;
if (!result) {
// eslint-disable-next-line no-console
console.debug(`failed to serve variant ${window.hlx.experiment.selectedVariant}. Falling back to ${experimentConfig.variantNames[0]}.`);
}
document.body.classList.add(`variant-${result ? experimentConfig.selectedVariant : experimentConfig.variantNames[0]}`);
document.body.classList.add(`variant-${context.toClassName(result ? experimentConfig.selectedVariant : experimentConfig.variantNames[0])}`);
context.sampleRUM('experiment', {
source: experimentConfig.id,
target: result ? experimentConfig.selectedVariant : experimentConfig.variantNames[0],
Expand Down Expand Up @@ -577,7 +577,7 @@ export async function serveAudience(document, options, context) {
}
}

window.hlx.patchBlockConfig.push((config) => {
window.hlx.patchBlockConfig?.push((config) => {
const { experiment } = window.hlx;

// No experiment is running
Expand Down
1 change: 1 addition & 0 deletions solutions/plugins/experimentation/src/preview.css
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
.hlx-popup h5 {
font-family: system-ui, sans-serif;
margin: 0;
color: inherit;
}

.hlx-popup h4 {
Expand Down
13 changes: 7 additions & 6 deletions solutions/plugins/experimentation/src/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,23 @@ async function fetchRumData(experiment, options) {

// the query is a bit slow, so I'm only fetching the results when the popup is opened
const resultsURL = new URL('https://helix-pages.anywhere.run/helix-services/run-query@v3/rum-experiments');
resultsURL.searchParams.set(options.experimentsQueryParameter, experiment);
resultsURL.searchParams.set('domainKey', options.domainKey);
// restrict results to the production host, this also reduces query cost
if (typeof options.isProd === 'function' && options.isProd()) {
resultsURL.searchParams.set('domain', window.location.host);
resultsURL.searchParams.set('url', window.location.host);
} else if (options.prodHost) {
resultsURL.searchParams.set('domain', options.prodHost);
resultsURL.searchParams.set('url', options.prodHost);
}
resultsURL.searchParams.set('domainkey', options.domainKey);
resultsURL.searchParams.set('experiment', experiment);

const response = await fetch(resultsURL.href);
if (!response.ok) {
return null;
}

const { results } = await response.json();
if (!results.length) {
const { data } = results;
if (!data.length) {
return null;
}

Expand All @@ -200,7 +201,7 @@ async function fetchRumData(experiment, options) {
return o;
}, {});

const variantsAsNums = results.map(numberify);
const variantsAsNums = data.map(numberify);
const totals = Object.entries(
variantsAsNums.reduce((o, v) => {
Object.entries(v).forEach(([k, val]) => {
Expand Down
Loading