Dynamic react component to ease AB testing via AB test providers like vwo, optimizely etc.
npm install --save dynamic-react-ab
import React, { Component } from 'react'
import ReactAB from 'dynamic-react-ab'
class Example extends Component {
render () {
return (
<ReactAB ></ReactAB>
)
}
}
Dynamic react component to ease AB testing via AB test providers like VWO, Optimizely etc.
A/B testing also known as split testing, is an experiment where two (or more) variants of a webpage are shown to users at random to determine which variant leads to more acceptance. After finding a better variant, it is then released for all users.
A/B testing is extremely important in the world of user acquisition. A good article on this is: https://m.signalvnoise.com/how-we-lost-and-found-millions-by-not-a-b-testing-e70f27dd783e
However, the current solutions for A/B test in the react community require changes in your codebase. Some of the modules are:
These are the good libraries, but do not provide flexibility to change the experiment quickly (say within 15 min or so).
A dev effort followed by a release is required for most of the experiments which should not be needed as most of them are usually fail fast experiments.
Dynamic component is a blank component ready to consume data on a predefined schema. This way a dev can easily create different components and launch them instantly.
By making changes using a generic component, we give control to react to change the UI on the page instead of fighting with it. It makes React happy and our changes are persisted!
https://github.com/kishorsharma/dynamic-react-ab-demo
http://insights.traveltriangle.com/technical/dynamic-programming-in-react-with-ab-testing/
These props are available in all generic components:
E.g.,:
Let’s define our button component:
{
type: 'button',
keyP: 'defaultButton',
classes: 'custom_btn_class',
styles: { padding: '13px 20px' },
label: 'Generic button',
cta: 'CUSTOM_BTN_EVENT_1'
}
To listen on click we need:
window.addEventListener('CUSTOM_BTN_EVENT_1', function() {
console.log('Generic Button is Clicked');
});
E.g.,:
Let’s define our Link component:
{
type: 'link',
keyP: 'defaultLink',
classes: 'custom_link_class',
styles: { padding: '13px 20px' },
label: 'Generic Link',
cta: 'CUSTOM_LINK_EVENT_1'
}
To listen on click we need:
window.addEventListener('CUSTOM_LINK_EVENT_1', function() {
console.log('Generic Link is Clicked');
});
E.g.,:
Let’s define our Div component:
const divG = {
id: 'myrandomID',
type: 'div',
keyP: 'defaultDiv',
classes: 'customGenDiv',
styles: {},
triggerOnMount: 'DIV_LOADED',
heading: 'Default Heading', // optional
// content: 'some dummy content', // optional either content or contentHtml should be provided
contentHtml: '<p>This is a p tag <button id="a_dynamic_btn" class="btn-filled-sec-large ripple customCss">Sample</button></p><span class="spanner">A simple span class</span> ',
beforeElem: 'detail-container',
divHeight: 100,
divWidth: null,
divPadLeft: 26,
// This can have multiple Button and Link component as CTAs.
ctaList: [{
type: 'button',
keyP: 'defaultDivButton1',
classes: 'btn-filled-sec-large ripple',
styles: {},
label: 'Open Modal',
cta: 'CUSTOM_DIV_BTN_EVENT_1'
}, {
type: 'button',
keyP: 'defaultDivButton2',
classes: 'btn-filled-sec-large ripple',
styles: {},
label: 'Trigger Event Btn 2',
cta: 'CUSTOM_DIV_BTN_EVENT_2'
}]
}
To listen on click of the div cta’s we need:
window.addEventListener('CUSTOM_DIV_BTN_EVENT_1', function() {
console.log('CTA Button of the Generic div is Clicked');
});
E.g.,:
Let’s define our Div component:
const customStyles = {
content: {
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
marginRight: '-50%',
transform: 'translate(-50%, -50%)'
}
}
const modalGen = {
id: 'myrandomID',
type: 'modal',
keyP: 'defaultModal',
classes: 'customGenModal',
styles: customStyles,
openTriggerEvent: 'CUSTOM_OPEN_MODAL',
closeTriggerEvent: 'CUSTOM_CLOSE_MODAL',
triggerOnOpen: 'CUSTOM_MODAL_OPEN',
triggerOnClose: 'CUSTOM_MODAL_CLOSE',
triggerOnMount: 'CUSTOM_MODAL_MOUNTED',
content: {
type: 'div',
key: 'defaultdiv',
classes: 'customGenDiv',
styles: {},
contentHtml: '<p>This is a sampe content using HTML <button id="closeModal">Close</button></p>'
}
}
To listen on open, close or mount events of the modal of div CTAs we need:
window.addEventListener('CUSTOM_MODAL_OPEN', function() {
console.log('Modal Opened');
});
window.addEventListener('CUSTOM_MODAL_CLOSE', function() {
console.log('Modal Closed');
});window.addEventListener('CUSTOM_MODAL_MOUNTED', function() {
console.log('Modal Mounted');
});
You can add a dynamic button with CTA same as openTriggerEvent
to open the modal:
{
type: 'button',
keyP: 'defaultButton',
classes: 'custom_btn_class',
styles: {padding: '13px 20px'},
label: 'Generic button',
cta: 'CUSTOM_OPEN_MODAL'
}
After defining your components as above, you need to update the state of the dynamic-react component:
const newState = {
show: true,
dynamincData: {
triggerOnMount: 'TRRIGER_ON_MOUNT',
comps: [btn, lnk, divG, modalGen]
}
}
var evt = new CustomEvent('REACT_AB_SET_STATE', { detail: newState })
window.dispatchEvent(evt)
You can find an example folder in the repo. This contains a sample app which is using GTM to dynamically inject edit contact form flow.
To use with AB testing providers:
There are plenty of good tutorials for creating variations. Please check the respective site for it.
With Google Tag Manager:
A/B Testing needs custom logic. I am not aware of AB testing via gtag.
For GTM:
GTM is easy to try as it can be used with localhost.
Contributions are welcome. Please raise the PR.
The Code belongs to TravelTriangle Pvt. Ltd.
MIT © kishorsharma