Skip to content

Commit

Permalink
Fixed mozilla#1776 - Rewrote share URL redirects to append contextual…
Browse files Browse the repository at this point in the history
… UTMs to that users' session
  • Loading branch information
maxxcrawford committed Aug 17, 2020
1 parent 59ba9ee commit bf4964d
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 11 deletions.
20 changes: 17 additions & 3 deletions controllers/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ const AppConstants = require("../app-constants");
const DB = require("../db/DB");
const HIBP = require("../hibp");
const { scanResult } = require("../scan-results");
const { generatePageToken, getExperimentBranch, getExperimentFlags } = require("./utils");
const {
generatePageToken,
getExperimentBranch,
getExperimentFlags,
getUTMContents,
} = require("./utils");

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

Expand All @@ -29,6 +34,14 @@ async function home(req, res) {
return res.redirect("/user/dashboard");
}

// Rewrites the /share/{COLOR} links to /
if (req.session.redirectHome) {
req.session.redirectHome = false;
return res.redirect("/");
}

// Note - If utmOverrides get set, they are unenrolled from the experiment
const utmOverrides = getUTMContents(req);
const experimentFlags = getExperimentFlags(req, EXPERIMENTS_ENABLED);

// Growth Experiment
Expand All @@ -39,6 +52,7 @@ async function home(req, res) {
});
}


if (req.params && req.params.breach) {
req.query.breach = req.params.breach;
}
Expand All @@ -65,18 +79,18 @@ async function home(req, res) {
pageToken: formTokens.pageToken,
csrfToken: formTokens.csrfToken,
experimentFlags,
utmOverrides,
});
}



res.render("monitor", {
title: req.fluentFormat("home-title"),
featuredBreach: featuredBreach,
scanFeaturedBreach,
pageToken: formTokens.pageToken,
csrfToken: formTokens.csrfToken,
experimentFlags,
utmOverrides,
});
}

Expand Down
4 changes: 3 additions & 1 deletion controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { resultsSummary } = require("../scan-results");
const sha1 = require("../sha1-utils");

const EXPERIMENTS_ENABLED = (AppConstants.EXPERIMENT_ACTIVE === "1");
const { getExperimentFlags } = require("./utils");
const { getExperimentFlags, getUTMContents } = require("./utils");

const FXA_MONITOR_SCOPE = "https://identity.mozilla.com/apps/monitor";

Expand Down Expand Up @@ -229,6 +229,7 @@ async function getDashboard(req, res) {
const user = req.user;
const allBreaches = req.app.locals.breaches;
const { verifiedEmails, unverifiedEmails } = await getAllEmailsAndBreaches(user, allBreaches);
const utmOverrides = getUTMContents(req);

// Growth Experiment
const experimentFlags = getExperimentFlags(req, EXPERIMENTS_ENABLED);
Expand All @@ -249,6 +250,7 @@ async function getDashboard(req, res) {
unverifiedEmails,
whichPartial: "dashboards/breaches-dash",
experimentFlags,
utmOverrides,
});
}

Expand Down
15 changes: 15 additions & 0 deletions controllers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ function getExperimentBranch(req, sorterNum = false, language = false, variation

}

function getUTMContents(req) {
if (!req) {
throw new Error("No request availabe");
}

// If UTMs are set previously, set them again.
if (req.session.utmOverrides) {
return req.session.utmOverrides;
}

req.session.utmOverrides = false;
return false;
}

function getExperimentFlags(req, EXPERIMENTS_ENABLED) {
if (!req) {
throw new Error("No request availabe");
Expand All @@ -208,4 +222,5 @@ module.exports = {
hasUserSignedUpForRelay,
getExperimentBranch,
getExperimentFlags,
getUTMContents,
};
51 changes: 51 additions & 0 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { FXA } = require("./lib/fxa");
const { FluentError } = require("./locale-utils");
const mozlog = require("./log");

const HIBP = require("./hibp");

const log = mozlog("middleware");

Expand Down Expand Up @@ -173,6 +174,55 @@ async function requireSessionUser(req, res, next) {
next();
}

function getShareUTMs(req, res, next) {
const generalShareUrls = [
"/share/orange",
"/share/purple",
"/share/blue",
"/share/",
];

const colors = [
"orange",
"purple",
"blue",
];

const urlArray = req.url.split("/");
const color = urlArray.slice(-1)[0];

req.session.utmOverrides = {
campaignName: "shareLinkTraffic",
campaignTerm: "default",
};

// Set Color Var in UTM
if (color.length && colors.includes(color)) {
req.session.utmOverrides.campaignTerm = color;
}

if (color.length && !colors.includes(color)) {
const allBreaches = req.app.locals.breaches;
const breachName = color;
const featuredBreach = HIBP.getBreachByName(allBreaches, breachName);

if (featuredBreach) {
req.session.utmOverrides.campaignTerm = featuredBreach.Name;
}
}

if (generalShareUrls.includes(req.url)) {
// If not breach specific, redirect to "/"
req.session.redirectHome = true;
}

// Kick user from experiment
req.session.experimentFlags = {
excludeFromExperiment: true,
};

next();
}


module.exports = {
Expand All @@ -185,4 +235,5 @@ module.exports = {
clientErrorHandler,
errorHandler,
requireSessionUser,
getShareUTMs,
};
12 changes: 12 additions & 0 deletions public/js/fxa-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ function setGAListeners(){
ga("set", "anonymizeIp", true);
ga("set", "dimension6", `${document.body.dataset.signedInUser}`);


// Set Share URL UTMs
if ( document.body.dataset.utm_campaign && !document.body.dataset.experiment ) {
// campaignName
ga("set", "campaignName", `${document.body.dataset.utm_campaign}`);
ga("set", "dimension9", `${document.body.dataset.utm_campaign}`);
// campaignKeyword / term
ga("set", "campaignKeyword", `${document.body.dataset.utm_term}`);
ga("set", "dimension8", `${document.body.dataset.utm_term}`);
}

// Growth Experiment
if (document.body.dataset.experiment) {
// If an experiment is active, set the "Growth Experiment Version"
Expand All @@ -236,6 +247,7 @@ function setGAListeners(){
ga("set", "campaignKeyword", `${document.body.dataset.utm_term}`);
}


ga("send", "pageview", {
hitCallback: function() {
removeUtmsFromUrl();
Expand Down
13 changes: 7 additions & 6 deletions routes/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ const csrf = require("csurf");

const {home, getAboutPage, getAllBreaches, getBentoStrings, getSecurityTips, notFound} = require("../controllers/home");

const { getShareUTMs } = require("../middleware");

const router = express.Router();
const csrfProtection = csrf();

router.get("/", csrfProtection, home);
router.get("/share/", csrfProtection, home);
router.get("/share/orange", csrfProtection, home);
router.get("/share/purple", csrfProtection, home);
router.get("/share/red", csrfProtection, home);
router.get("/share/blue", csrfProtection, home);
router.get("/share/:breach", csrfProtection, home);
router.get("/share/orange", csrfProtection, getShareUTMs, home);
router.get("/share/purple", csrfProtection, getShareUTMs, home);
router.get("/share/blue", csrfProtection, getShareUTMs, home);
router.get("/share/:breach", csrfProtection, getShareUTMs, home);
router.get("/share/", csrfProtection, getShareUTMs, home);
router.get("/about", getAboutPage);
router.get("/breaches", getAllBreaches);
router.get("/security-tips", getSecurityTips);
Expand Down
4 changes: 3 additions & 1 deletion views/layouts/default.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
<link rel="icon" href="/img/favicons/favicon-128.png" sizes="128x128" />
<link rel="icon" href="/img/favicons/favicon-256.png" sizes="256x256" />
</head>
<body {{> analytics/default_dataset }} data-bento-app-id="fx-monitor" {{#if experimentFlags.experimentBranch }} {{> analytics/experiment }} {{/if}}>
<body {{> analytics/default_dataset }} data-bento-app-id="fx-monitor"
{{#if experimentFlags.experimentBranch }} {{> analytics/experiment }} {{/if}}
{{#if utmOverrides.campaignName }} {{> analytics/share }} {{/if}} >
{{> header/header }}
{{{ body }}}
{{> footer }}
Expand Down
2 changes: 2 additions & 0 deletions views/partials/analytics/share.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data-utm_term="{{utmOverrides.campaignTerm}}"
data-utm_campaign="{{utmOverrides.campaignName}}"

0 comments on commit bf4964d

Please sign in to comment.