Skip to content

Commit

Permalink
Add GA UTMs to OAuth flow URL
Browse files Browse the repository at this point in the history
  • Loading branch information
maxxcrawford committed Apr 22, 2020
1 parent a2a780d commit 49ad79c
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions controllers/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const sha1 = require("../sha1-utils");

const log = mozlog("controllers.oauth");

const utmArray = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];

function getUTMNames() {
return utmArray;
}

function init(req, res, next, client = FxAOAuthClient) {
// Set a random state string in a cookie so that we can verify
// the user when they're redirected back to us after auth.
Expand All @@ -22,10 +28,15 @@ function init(req, res, next, client = FxAOAuthClient) {
const url = new URL(client.code.getUri({state}));
const fxaParams = new URL(req.url, AppConstants.SERVER_URL);

req.session.utmContents = {};

url.searchParams.append("access_type", "offline");
url.searchParams.append("action", "email");

for (const param of fxaParams.searchParams.keys()) {
if (utmArray.includes(param)) {
req.session.utmContents[param] = fxaParams.searchParams.get(param);
}
url.searchParams.append(param, fxaParams.searchParams.get(param));
}

Expand Down Expand Up @@ -53,6 +64,16 @@ async function confirmed(req, res, next, client = FxAOAuthClient) {
const existingUser = await DB.getSubscriberByEmail(email);
req.session.user = existingUser;

const returnURL = new URL("/user/dashboard", AppConstants.SERVER_URL);

if (req.session.utmContents) {
getUTMNames().forEach(param => {
if (req.session.utmContents[param]) {
returnURL.searchParams.append(param, req.session.utmContents[param]);
}
});
}

// Check if user is signing up or signing in,
// then add new users to db and send email.
if (!existingUser || existingUser.fxa_refresh_token === null) {
Expand Down Expand Up @@ -91,11 +112,13 @@ async function confirmed(req, res, next, client = FxAOAuthClient) {
);
req.session.user = verifiedSubscriber;

return res.redirect("/user/dashboard");
console.log("L115: ", returnURL.pathname + returnURL.search);
return res.redirect(returnURL.pathname + returnURL.search);
}
// Update existing user's FxA data
await DB._updateFxAData(existingUser, fxaUser.accessToken, fxaUser.refreshToken, fxaProfileData);
return res.redirect("/user/dashboard");
console.log("L120: ", returnURL.pathname + returnURL.search);
res.redirect(returnURL.pathname + returnURL.search);
}

module.exports = {
Expand Down

0 comments on commit 49ad79c

Please sign in to comment.