From 5e04f367c18dfa334850784b70d367e137a0c2ae Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Tue, 5 Mar 2019 12:58:40 -0800 Subject: [PATCH] utms for everybody --- app/controller.js | 8 ++++++++ app/metrics.js | 9 ++++++++- app/user.js | 34 ++++++++++++++++++++++++++++++++++ server/amplitude.js | 8 +++++++- 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/app/controller.js b/app/controller.js index cd67e97a..05d4b420 100644 --- a/app/controller.js +++ b/app/controller.js @@ -106,6 +106,14 @@ export default function(state, emitter) { }); emitter.on('signup-cta', source => { + const query = state.query; + state.user.startAuthFlow(source, { + campaign: query.utm_campaign, + content: query.utm_content, + medium: query.utm_medium, + source: query.utm_source, + term: query.utm_term + }); state.modal = signupDialog(source); render(); }); diff --git a/app/metrics.js b/app/metrics.js index d22d007a..c256bf85 100644 --- a/app/metrics.js +++ b/app/metrics.js @@ -16,8 +16,15 @@ export default function initialize(state, emitter) { } emitter.on('DOMContentLoaded', () => { // experiment = storage.enrolled[0]; + const query = appState.query; addEvent('client_visit', { - entrypoint: appState.route === '/' ? 'upload' : 'download' + entrypoint: appState.route === '/' ? 'upload' : 'download', + referrer: document.referrer, + utm_campaign: query.utm_campaign, + utm_content: query.utm_content, + utm_medium: query.utm_medium, + utm_source: query.utm_source, + utm_term: query.utm_term }); }); emitter.on('experiment', experimentEvent); diff --git a/app/user.js b/app/user.js index f3df940d..3f912705 100644 --- a/app/user.js +++ b/app/user.js @@ -94,6 +94,29 @@ export default class User { return this.loggedIn ? hashId(this.storage.id) : hashId(anonId); } + async startAuthFlow(source, utms = {}) { + try { + const params = new URLSearchParams({ + entrypoint: `send-${source}`, + form_type: 'email', + utm_source: utms.source || 'send', + utm_campaign: utms.campaign || 'none' + }); + const res = await fetch( + `${this.authConfig.issuer}/metrics-flow?${params.toString()}` + ); + const { flowId, flowBeginTime } = await res.json(); + this.flowId = flowId; + this.flowBeginTime = flowBeginTime; + this.utms = utms; + } catch (e) { + console.error(e); + this.flowId = null; + this.flowBeginTime = null; + this.utms = null; + } + } + async login(email) { const state = arrayToB64(crypto.getRandomValues(new Uint8Array(16))); storage.set('oauthState', state); @@ -111,6 +134,17 @@ export default class User { if (email) { options.email = email; } + if (this.flowId && this.flowBeginTime) { + options.flow_id = this.flowId; + options.flow_begin_time = this.flowBeginTime; + } + if (this.utms) { + options.utm_campaign = this.utms.campaign || 'none'; + options.utm_content = this.utms.content || 'none'; + options.utm_medium = this.utms.medium || 'none'; + options.utm_source = this.utms.source || 'send'; + options.utm_term = this.utms.term || 'none'; + } const params = new URLSearchParams(options); location.assign( `${this.authConfig.authorization_endpoint}?${params.toString()}` diff --git a/server/amplitude.js b/server/amplitude.js index a4eb373a..22e2d389 100644 --- a/server/amplitude.js +++ b/server/amplitude.js @@ -104,10 +104,16 @@ function clientEvent(event, ua, language, session_id, deltaT, platform, ip) { entrypoint: ep.entrypoint, file_count: ep.file_count, password_protected: ep.password_protected, + referrer: ep.referrer, size: ep.size, time_limit: ep.time_limit, trigger: ep.trigger, - ttl: ep.ttl + ttl: ep.ttl, + utm_campaign: ep.utm_campaign, + utm_content: ep.utm_content, + utm_medium: ep.utm_medium, + utm_source: ep.utm_source, + utm_term: ep.utm_term }; const user_properties = { active_count: up.active_count,