Skip to content

Commit

Permalink
Merge pull request #5 from dollarshaveclub/query-param
Browse files Browse the repository at this point in the history
Query param
  • Loading branch information
briangonzalez authored Oct 29, 2016
2 parents 8f13582 + 91eb26a commit c66d16a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
6 changes: 3 additions & 3 deletions build/test-interface.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/test.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 17 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import camelize from '../vendor/camelize';
const supportsClasslist = "classList" in document.createElement("_");
const storageKey = 'ab-tests';

function getParameterByName(name, url) {
if (!url) { url = window.location.href; } name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " "));
}

class Test {
constructor(name, data = {}, options = {}) {
options = {
Expand Down Expand Up @@ -35,18 +39,6 @@ class Test {

let bucket;

// Support Legacy Test Structure, deprecate soon
let legacyBucket = storage.local.getItem(`test-${name}`);
if (legacyBucket) {
console.warn('Migrating tests');
tests[name] = {
bucket: legacyBucket,
buckets: Object.keys(data)
};
storage.local.setItem(storageKey, JSON.stringify(tests));
storage.local.removeItem(`test-${name}`);
}

// Retrieve Bucket from storage if possible
if (options.persist && tests[name]) {
bucket = tests[name].bucket;
Expand All @@ -64,8 +56,20 @@ class Test {
weights.push( data[names[i]].weight );
}

// Grab assignment via query param. Syntax:
// ?assign=test:bucket
const [assignTest, assignBucket] = (getParameterByName('assign') || '').split(':');
const assignmentMatchesName = assignTest === name;
const isValidBucket = assignBucket in data;

// Use query param assignment if possible
if (assignmentMatchesName && isValidBucket) {
bucket = assignBucket

// Select a random weighted bucket
bucket = Test.chooseWeightedItem(names, weights);
} else {
bucket = Test.chooseWeightedItem(names, weights);
}

// Save
tests[name] = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ab-tester",
"version": "0.5.0",
"version": "1.0.0",
"homepage": "https://github.com/dollarshaveclub/ab-tester.js",
"main": "./index.js",
"repository": {
Expand Down

0 comments on commit c66d16a

Please sign in to comment.