Skip to content

Commit

Permalink
Moved getExperimentBranch to /utils, extended it to both home and sca…
Browse files Browse the repository at this point in the history
…n results files, added UTMs to /scan page
  • Loading branch information
maxxcrawford committed Apr 23, 2020
1 parent cd66598 commit a38f30c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 39 deletions.
41 changes: 2 additions & 39 deletions controllers/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const AppConstants = require("../app-constants");
const DB = require("../db/DB");
const { scanResult } = require("../scan-results");
const { generatePageToken } = require("./utils");
const { generatePageToken, getExperimentBranch } = require("./utils");

const EXPERIMENTS_ENABLED = (AppConstants.EXPERIMENT_ACTIVE === "1");

Expand All @@ -25,6 +25,7 @@ async function home(req, res) {
if (EXPERIMENTS_ENABLED) {
const coinFlipNumber = Math.random() * 100;
experimentBranch = getExperimentBranch(req, coinFlipNumber);
req.session.experimentBranch = experimentBranch;
isUserInExperiment = (experimentBranch === "vb");
experimentBranchB = (experimentBranch === "vb" && isUserInExperiment);
}
Expand Down Expand Up @@ -137,44 +138,6 @@ function notFound(req, res) {
});
}

function getExperimentBranch(req, sorterNum) {

// If we cannot parse req.headers["accept-language"], we should not
// enroll users in the experiment.
if (!req.headers || !req.headers["accept-language"]){
return false;
}

// If the user doesn't have an English variant langauge selected as their primary language,
// we do not enroll them in the experiment.
const lang = req.headers["accept-language"].split(",");
if (!lang[0].includes("en")) {
return false;
}

// If URL param has experimentBranch entry, use that branch;
if (req.query.experimentBranch) {
if (!["va", "vb"].includes(req.query.experimentBranch)) {
return false;
}
req.session.experimentBranch = req.query.experimentBranch;
return req.query.experimentBranch;
}

// If user was already assigned a branch, stay in that branch;
if (req.session.experimentBranch) { return req.session.experimentBranch; }

// Split into two categories
if (sorterNum <= 50) {
req.session.experimentBranch = "vb";
return "vb";
}

return "va";
}



module.exports = {
home,
getAboutPage,
Expand Down
37 changes: 37 additions & 0 deletions controllers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,44 @@ function hasUserSignedUpForRelay(user) {
return false;
}

function getExperimentBranch(req, sorterNum) {

// If we cannot parse req.headers["accept-language"], we should not
// enroll users in the experiment.
if (!req.headers || !req.headers["accept-language"]){
return false;
}

// If the user doesn't have an English variant langauge selected as their primary language,
// we do not enroll them in the experiment.
const lang = req.headers["accept-language"].split(",");
if (!lang[0].includes("en")) {
return false;
}

// If URL param has experimentBranch entry, use that branch;
if (req.query.experimentBranch) {
if (!["va", "vb"].includes(req.query.experimentBranch)) {
return false;
}
req.session.experimentBranch = req.query.experimentBranch;
return req.query.experimentBranch;
}

// If user was already assigned a branch, stay in that branch;
if (req.session.experimentBranch) { return req.session.experimentBranch; }

// Split into two categories
if (sorterNum <= 50) {
// req.session.experimentBranch = "vb";
return "vb";
}

return "va";
}

module.exports = {
generatePageToken,
hasUserSignedUpForRelay,
getExperimentBranch,
};
19 changes: 19 additions & 0 deletions scan-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@ const { URL } = require("url");
const HIBP = require("./hibp");
const sha1 = require("./sha1-utils");

const { getExperimentBranch } = require("./controllers/utils");

const AppConstants = require("./app-constants");
const EXPERIMENTS_ENABLED = (AppConstants.EXPERIMENT_ACTIVE === "1");

const scanResult = async(req, selfScan=false) => {

const allBreaches = req.app.locals.breaches;
let scannedEmail = null;

let experimentBranch = null;
let isUserInExperiment = null;
let experimentBranchB = null;

if (EXPERIMENTS_ENABLED) {
const coinFlipNumber = Math.random() * 100;
experimentBranch = getExperimentBranch(req, coinFlipNumber);
req.session.experimentBranch = experimentBranch;
isUserInExperiment = (experimentBranch === "vb");
experimentBranchB = (experimentBranch === "vb" && isUserInExperiment);
}

const title = req.fluentFormat("scan-title");
let foundBreaches = [];
let specificBreach = null;
Expand Down Expand Up @@ -98,6 +114,9 @@ const scanResult = async(req, selfScan=false) => {
fullReport,
userDash,
scannedEmailId,
experimentBranch,
isUserInExperiment,
experimentBranchB,
};
};

Expand Down

0 comments on commit a38f30c

Please sign in to comment.