JavaScript based A/B testing
This option uses JavaScript to handle the A/B testing directly within your websites frontend. Please note while this approach is more accessible, it may affect performance due to client-side rendering.
By CloudCannon
Create a component within CloudCannon to manage the A/B test content
In Bookshop, you need to create the two following files:
copied
Below are code snippets for the YAML configuration and Liquid template, with optional example code to push events to Google Analytics. (See this lesson for more information on setting up Google Analytics.)
ab.bookshop.yml:
ab.bookshop.yml
copied
ab.eleventy.liquid:
<div class="c-ab" data-ab-define="{{ reference }}" data-ab-ratio="{{ ratio }}">
<div data-ab-reference="{{ reference }}" data-ab-variation="0">
{% for component in variation_one.blocks %}
{% bookshop "{{component._bookshop_name}}" bind: component %}
{% endfor %}
</div>
<template>
<div data-ab-reference="{{ reference }}" data-ab-variation="1">
{% for component in variation_two.blocks %}
{% bookshop "{{component._bookshop_name}}" bind: component %}
{% endfor %}
</div>
</template>
</div>
<script>
{
let ab_component = document.querySelector(".c-ab[data-ab-define='{{ reference }}']");
let ab_obj;
let ref = ab_component.getAttribute("data-ab-define")
let currURL = window.location.href;
let ab = localStorage.getItem("cc-ab") ? JSON.parse(localStorage.getItem("cc-ab")) : []
let ab_filtered = ab.filter(x => x.url === currURL && x.component_ref === ref)
if(ab_filtered.length > 0)
ab_obj = ab_filtered[0]
else
{
let ratio = ab_component.getAttribute("data-ab-ratio")
ratio = ratio.split(":")
let random = Math.floor(Math.random() * (100 - 0) + 0);
let variation = random < ratio[0] ? "0" : "1";
ab_obj = { "url": currURL, "variation": variation, "component_ref": ref }
ab.push(ab_obj)
localStorage.setItem("cc-ab", JSON.stringify(ab))
}
if(ab_obj.variation == "1"){
ab_component.querySelector(`div[data-ab-variation='0'`)?.remove();
let temp = ab_component.getElementsByTagName("template")[0]
ab_component.appendChild(temp.content.cloneNode(true))
}
// Optional Google Analytics example
window.dataLayer = window.dataLayer || [];
dataLayer.push(["event", "ab_test_pageview", { "ab_test_name": `${ref} v${ab_obj.variation}` }]);
}
</script>
Add your components and content
- Insert the A/B test component into your page.
- Add your components and content within the
variation_one
andvariation_two
blocks in the Liquid template:
copied
Go live with your test!
Lessons in this Tutorial
A/B Testing for Static Websites