Skip to content

Commit

Permalink
Query param assignment, eg: ?assign=testname:bucketname
Browse files Browse the repository at this point in the history
  • Loading branch information
briangonzalez committed Oct 28, 2016
1 parent eda4878 commit 91eb26a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 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.

18 changes: 17 additions & 1 deletion 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 @@ -52,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 91eb26a

Please sign in to comment.