Skip to content

Commit

Permalink
Merge pull request #6 from dollarshaveclub/query-param-overwrite-loca…
Browse files Browse the repository at this point in the history
…l-storage

Ignore localStorage assignment if query param assignment present
  • Loading branch information
briangonzalez authored Oct 29, 2016
2 parents c66d16a + e90f411 commit 20f5d60
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
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.

24 changes: 13 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import storage from '../vendor/storage';
import camelize from '../vendor/camelize';
import getParam from '../vendor/get-param'

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 @@ -39,8 +36,19 @@ class Test {

let bucket;

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

const useStoredAssignment = options.persist && tests[name];
const shouldAssign = (assignmentMatchesName && isValidBucket) || !useStoredAssignment;

console.log(shouldAssign, useStoredAssignment)

// Retrieve Bucket from storage if possible
if (options.persist && tests[name]) {
if (!shouldAssign) {
bucket = tests[name].bucket;
}

Expand All @@ -56,12 +64,6 @@ 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
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": "1.0.0",
"version": "1.1.0",
"homepage": "https://github.com/dollarshaveclub/ab-tester.js",
"main": "./index.js",
"repository": {
Expand Down
9 changes: 9 additions & 0 deletions vendor/get-param.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

export default function getParamByName(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, " "));
}

0 comments on commit 20f5d60

Please sign in to comment.