-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding experiment function for rum bundler APIs (#264)
- Loading branch information
Showing
7 changed files
with
216 additions
and
0 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 |
---|---|---|
|
@@ -135,3 +135,6 @@ junit | |
|
||
# Files deliberately excluded from version control | ||
*DO-NOT-COMMIT* | ||
|
||
# Mac OS | ||
.DS_Store |
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
83 changes: 83 additions & 0 deletions
83
packages/spacecat-shared-rum-api-client/src/functions/experiment.js
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,83 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
const EXPERIMENT_CHECKPOINT = ['experiment']; | ||
const METRIC_CHECKPOINTS = ['click', 'convert', 'formsubmit']; | ||
const CHECKPOINTS = [...EXPERIMENT_CHECKPOINT, ...METRIC_CHECKPOINTS]; | ||
|
||
function toClassName(name) { | ||
return typeof name === 'string' | ||
? name.toLowerCase().replace(/[^0-9a-z]/gi, '-').replace(/-+/g, '-').replace(/^-|-$/g, '') | ||
: ''; | ||
} | ||
|
||
function getOrCreateExperimentObject(urlInsights, experimentName) { | ||
let experimentObject = urlInsights.find((e) => e.experiment === toClassName(experimentName)); | ||
if (!experimentObject) { | ||
experimentObject = { | ||
experiment: toClassName(experimentName), | ||
variants: [], | ||
}; | ||
urlInsights.push(experimentObject); | ||
} | ||
return experimentObject; | ||
} | ||
|
||
function getOrCreateVariantObject(variants, variantName) { | ||
let variantObject = variants.find((v) => v.name === variantName); | ||
if (!variantObject) { | ||
variantObject = { | ||
name: variantName, | ||
views: 0, | ||
click: {}, | ||
convert: {}, | ||
formsubmit: {}, | ||
}; | ||
variants.push(variantObject); | ||
} | ||
return variantObject; | ||
} | ||
|
||
function handler(bundles) { | ||
const experimentInsights = {}; | ||
for (const bundle of bundles) { | ||
const experimentEvent = bundle.events.find((e) => e.checkpoint === 'experiment'); | ||
if (experimentEvent) { | ||
const { url, weight } = bundle; | ||
if (!experimentInsights[url]) { | ||
experimentInsights[url] = []; | ||
} | ||
const experimentName = experimentEvent.source; | ||
const variantName = experimentEvent.target; | ||
const experimentObject = getOrCreateExperimentObject(experimentInsights[url], experimentName); | ||
const variantObject = getOrCreateVariantObject(experimentObject.variants, variantName); | ||
variantObject.views += weight; | ||
|
||
for (const event of bundle.events) { | ||
if (METRIC_CHECKPOINTS.includes(event.checkpoint)) { | ||
const { source, checkpoint } = event; | ||
if (!variantObject[checkpoint][source]) { | ||
variantObject[checkpoint][source] = weight; | ||
} else { | ||
variantObject[checkpoint][source] += weight; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return experimentInsights; | ||
} | ||
|
||
export default { | ||
handler, | ||
checkpoints: CHECKPOINTS, | ||
}; |
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
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
46 changes: 46 additions & 0 deletions
46
packages/spacecat-shared-rum-api-client/test/fixtures/experiments.json
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,46 @@ | ||
{ | ||
"https://www.aem.live/home": [ | ||
{ | ||
"experiment": "short-home", | ||
"variants": [ | ||
{ | ||
"name": "challenger-1", | ||
"views": 1300, | ||
"click": { | ||
".hero": 100, | ||
".header #navmenu-0": 100, | ||
".roi-calculator .button": 100, | ||
".hero .button": 100 | ||
}, | ||
"convert": {}, | ||
"formsubmit": {} | ||
}, | ||
{ | ||
"name": "control", | ||
"views": 800, | ||
"click": { | ||
".hero .button": 100, | ||
".header .button": 200, | ||
".header #navmenu-0": 200 | ||
}, | ||
"convert": {}, | ||
"formsubmit": {} | ||
} | ||
] | ||
} | ||
], | ||
"https://www.aem.live/new-exp-page": [ | ||
{ | ||
"experiment": "", | ||
"variants": [ | ||
{ | ||
"name": "https://www.aem.live/some-other-page", | ||
"views": 100, | ||
"click": {}, | ||
"convert": {}, | ||
"formsubmit": {} | ||
} | ||
] | ||
} | ||
] | ||
} |
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