Skip to content

Commit

Permalink
Merge pull request #17 from peterlazzarino/debug/menu
Browse files Browse the repository at this point in the history
Debug/menu
  • Loading branch information
peterlazzarino committed Feb 12, 2019
2 parents 3dcbbbc + d6917b7 commit 3386719
Show file tree
Hide file tree
Showing 19 changed files with 11,721 additions and 354 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,39 @@ React component for integrating test experiences with your react code from a sou
$ npm install --save react-controlled-ab
$ yarn add react-controlled-ab
```

## Usage

to be written
```javascript

import React, { Component } from 'react';
import ABTest, { DebugMenu } from "react-controlled-ab"
import { subscribeToCampaign } from "react-controlled-ab/datalayers/vwo/Datalayer"

<ABTest campaign={50} subscribeFunc={subscribeToCampaign}>
<span>Test 50 - Instance 1 - Control</span>
<span>Test 50 - Instance 1 - Variant B</span>
<span>Test 50 - Instance 1 - Variant C</span>
</ABTest>

```

## Props

### campaign

Type: string Default: undefined
Type: number Default: undefined

The number of the campaign you are testing, should correspond to the campaign ID in VWO.

### subscribeFunc

Type: function Default: undefined

The name of the campaign you are testing, should correspond to the campaign in evergage but is really just a way to group experiences.
The function imported from the datalayer you'd like to use. This prop allows ABTest to know when the campaign variant is identified and or changed.

### onExperience

Expand Down
47 changes: 47 additions & 0 deletions campaign/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from "react";
import * as PropTypes from "prop-types";
import * as indexOf from "array-index-of";

const campaignChangeHandlers = [];
const campaignChangeSubscribers = [];

export const setHandler = (id, callback) => {
if(!campaignChangeHandlers[id]){
campaignChangeHandlers[id] = [];
}
campaignChangeHandlers[id].push({
id,
callback
})
}

export const subscribeToCampaigns = (callback) => {
campaignChangeSubscribers.push(callback);
}

export const sendInitialValue = (id, value, callback) => {
sendValue(value, callback);
notifyCampaignSubscribers(id, value);
}

export const updateCampaign = (id, value) => {
const callbacks = getCallbacks(id);
callbacks.forEach(({ callback }) => {
callback(value);
});
notifyCampaignSubscribers(id, value);
}

const notifyCampaignSubscribers = (id, value) => {
campaignChangeSubscribers.forEach((cb) => {
cb(id, value)
})
}

const getCallbacks = (id) => {
return campaignChangeHandlers[id];
}

const sendValue = (value, callback) => {
callback(value);
}
12 changes: 8 additions & 4 deletions components/ABTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ export default class ABTest extends React.Component<IABTestProps, IABTestState>
this.callbackExperience(0);
}
}
public handleEvent (campaign) {
public handleEvent (variantIndex) {
this.setState({
selectedExperience: campaign.variantIndex,
selectedExperience: variantIndex,
campaignEventReceived: true,
});
this.callbackExperience(campaign.variantIndex);
this.callbackExperience(variantIndex);
}
public callbackExperience (experienceId) {
const { campaign } = this.props;
Expand All @@ -73,6 +73,9 @@ export default class ABTest extends React.Component<IABTestProps, IABTestState>
public renderExperience (children, selectedExperience, campaign) {
try {
const experiencedReceived = children[selectedExperience];
if (!experiencedReceived) {
return null;
}
return (
experiencedReceived
);
Expand All @@ -90,7 +93,8 @@ export default class ABTest extends React.Component<IABTestProps, IABTestState>
if(!canUseDOM) {
return null;
}
if (!supressFallback && placeholder && selectedExperience === undefined) {
const noExperience = typeof selectedExperience === "undefined";
if (!supressFallback && placeholder && noExperience) {
const placeholderStyle = {
visibility: "hidden",
};
Expand Down
55 changes: 55 additions & 0 deletions components/DebugMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as React from "react";
import { subscribeToCampaigns, updateCampaign } from "../campaign";

export default class DebugMenu extends React.Component<any, any> {
constructor () {
super();
this.state = {
campaigns: [],
};
}
public componentDidMount () {
subscribeToCampaigns(this.showCampaign);
}
public render () {
const rows = [];
for(const key of Object.keys(this.state.campaigns)){
rows.push(
<div>
Campaign ID: <b>{key}</b> -
Variant :
<select
onChange={({ target: { value }}) => { updateCampaign(key, parseInt(value, 10));} }
value={this.state.campaigns[key]}
>
<option value={0}>Control</option>
<option value={1}>B</option>
<option value={2}>C</option>
<option value={4}>D</option>
<option value={5}>E</option>
</select>
</div>,
);
}
return (
<div style={{
position: "fixed",
bottom: 0,
right: 15,
padding: "0 15px 15px",
width: "300px",
backgroundColor: "#efefef",
}}>
<h3 style={{ textAlign: "center" }}>Active A/B Tests</h3>
{rows}
</div>
);
}
private showCampaign = (id, value) => {
const newCampaignState = { ...this.state.campaigns };
newCampaignState[id] = value;
this.setState({
campaigns: newCampaignState,
});
}
}
39 changes: 39 additions & 0 deletions datalayers/vwo/Datalayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as React from "react";
import * as PropTypes from "prop-types";
import * as indexOf from "array-index-of";
import { sendInitialValue, setHandler } from "../../campaign"

const campaignValues = [];

export const subscribeToCampaign = (callback, campaignId) => {
setHandler(campaignId, callback)
sendInitialValue(campaignId, findCampaignValue(campaignId), callback)
}

const findCampaignValue = (campaignId) => {
if(campaignValues[campaignId]){
return campaignValues[campaignId];
}
let attempts = 0;
let campaignValue = 0;
let interval = setInterval(function(){
const campaignDefaultVal = getCookie(`_vis_opt_exp_${campaignId}_combi`);
const campaignDebugVal = getCookie(`debug_vis_opt_exp_${campaignId}_combi`);
const campaignVal = campaignDefaultVal || campaignDebugVal;
if(campaignVal){
clearInterval(interval);
campaignValue = parseInt(campaignVal) - 1;
campaignValues[campaignId] = campaignVal;
return campaignValue;
} else if (attempts > 10){
clearInterval(interval);
}
}, 50);
return campaignValue;
}

const getCookie = (name) => {
let value = "; " + document.cookie;
let parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
23 changes: 23 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
68 changes: 68 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.<br>
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br>
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.<br>
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.<br>
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br>
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

### Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

### Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

### Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

### Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

### `npm run build` fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
Loading

0 comments on commit 3386719

Please sign in to comment.