This is one half of an interactive visual A/B test designer like the ones in Optimizely and Google Optimize. This script is meant to be included on the page you are A/B testing. A companion script would run on your Experimentation Platform.
Features:
- Click to select elements like you would with DevTools
- Apply DOM mutations and inject CSS on the target site to preview A/B test variations
How it works:
- The Experimentation Platform (ExP) loads the target site in an iframe
- The ExP sends commands to the iframe using postMessage (e.g. "select element", "inject CSS", "mutate DOM")
- The iframe sends data back to the ExP also using postMessage (e.g. the selected element data)
On the target page you want to run an A/B test on:
<!-- The origin of your experimentation platform (for security) -->
<script>window.EXP_PLATFORM_ORIGIN="https://example.com";</script>
<script async src="https://unpkg.com/ab-designer/dist/ab-designer.umd.production.min.js"></script>
These commands are sent from the Experimentation Platform to this script running within an iframe using postMessage.
Acts like the DevTools Inspect tool. As you hover over elements, they are highlighted and the selector is shown in a tooltip.
{
"command": "selectElement"
}
Stops the DevTools Inspect behavior and goes back to an interactive page.
{
"command": "stopSelectElement"
}
Forces the hover state for the specified element
{
"command": "hoverElement",
"selector": "h1",
"ancestor": 0
}
The ancestor property lets you specify how far up the DOM tree to walk from the selector before applying the hover. 0
means use the element directly, 1
means use the parent, 2
is the grandparent, etc..
Forces the selected state for the specified element
{
"command": "selectElement",
"selector": "h1",
"ancestor": 0
}
The ancestor property lets you specify how far up the DOM tree to walk from the selector before applying the selected state. 0
means use the element directly, 1
means use the parent, 2
is the grandparent, etc..
Apply the specified DOM mutations to the page using Dom Mutator. These calls are not cumulative; each time this is called it reverts all previous DOM mutations and starts fresh.
{
"command": "mutateDOM",
"mutations": [
{
"selector": "h1",
"action": "set",
"attribute": "html",
"value": "Hello <strong>World</strong>"
}
]
}
Inject the specified CSS to the page in an inline <style>
tag. Like mutate, these calls are not cumulative and all previous CSS injections are removed first when called.
{
"command": "injectCSS",
"css": "body { color: red; }"
}
Check if the script is loaded and ready. Causes the iframe to send the visualDesignerReady
event in response.
{
"command": "isReady"
}
These events are sent back to the parent page via postMessage
Sent when the page finishes loading or when an isReady
command is sent.
{
"event": "visualDesignerReady"
}
Sent when a new element is hovered over while in Inspector mode. Uses the Finder library to generate unique CSS selectors.
{
"event": "elementHover",
"selector": ".my-title",
"display": "h1",
"breadcrumb": ["html","body","main","div"]
}
Sent when an element is clicked while in DevTools Inspect mode. Uses the Finder library to generate unique CSS selectors.
{
"event": "elementSelected",
"selector": ".link",
"display": "a",
"breadcrumb": ["html","body","main","div"],
"innerHTML": "Click Here",
"attributes": [
{
"name": "href",
"value": "/about"
}
]
}