Skip to content

Instantly share code, notes, and snippets.

@max8hine
Created September 22, 2020 00:25
Show Gist options
  • Save max8hine/b03b3e195224b84d874ece92d1b84300 to your computer and use it in GitHub Desktop.
Save max8hine/b03b3e195224b84d874ece92d1b84300 to your computer and use it in GitHub Desktop.
AB testing
const variants = {
exp1: "base",
exp2: "variant2",
exp3: "variant3",
};
const experimentSettings = {
activate(exp_name, unique_identifier) {
// Api call to the server
return variants[exp_name] || 'base';
},
tract(event_name, unique_identifier) {
// API call to the server
return true;
}
}
const ExperimentContext = React.createContext(experimentSettings);
class Main extends React.Component {
render() {
return (
<ExperimentContext.Provider>
<App />
</ExperimentContext.Provider>
);
}
}
class AnywhereYouWantToApply extends React.Component {
render() {
return (
<ExperimentContext.Consumer>
{({ activate }) => {
const decision = activate("exp_name", UUID);
if (decision === "variant") return <Variant />;
return <Base />;
}}
</ExperimentContext.Consumer>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment