Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit 8fc1679

Browse files
authored
Merge pull request #6851 from mozilla/pb/activity-108
#6851 r=vladikoff
2 parents d16bcab + ec3b88b commit 8fc1679

4 files changed

Lines changed: 45 additions & 13 deletions

File tree

server/lib/amplitude.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
'use strict';
1616

1717
const { GROUPS, initialize } = require('fxa-shared/metrics/amplitude');
18-
const geolocate = require('./geo-locate');
1918
const ua = require('./user-agent');
2019

2120
const SERVICES = require('./configuration').get('oauth_client_id_map');
@@ -129,7 +128,6 @@ function receiveEvent (event, request, data) {
129128
return;
130129
}
131130

132-
const location = geolocate(request);
133131
const userAgent = ua.parse(request.headers['user-agent']);
134132

135133
const amplitudeEvent = transform(
@@ -140,7 +138,7 @@ function receiveEvent (event, request, data) {
140138
mapBrowser(userAgent),
141139
mapOs(userAgent),
142140
mapFormFactor(userAgent),
143-
mapLocation(location)
141+
mapLocation(data.location)
144142
)
145143
);
146144

server/lib/flow-event.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const _ = require('lodash');
77
const amplitude = require('./amplitude');
88
const config = require('./configuration');
99
const flowMetrics = require('./flow-metrics');
10+
const geolocate = require('./geo-locate');
1011
const os = require('os');
1112

1213
const DNT_ALLOWED_DATA = [
@@ -87,6 +88,8 @@ const metricsRequest = (req, metrics, requestReceivedTime) => {
8788
return;
8889
}
8990

91+
metrics.location = geolocate(req);
92+
9093
let emitPerformanceEvents = false;
9194
const events = metrics.events || [];
9295
const performanceCategory = AUTH_VIEWS.has(metrics.initialView) ? 'auth' : 'other';
@@ -204,14 +207,17 @@ function estimateTime (times) {
204207
}
205208

206209
function logFlowEvent (event, data, request) {
210+
const { location } = data;
207211
const eventData = _.assign({
212+
country: location && location.country,
208213
event: event.type,
209214
flow_id: data.flowId, //eslint-disable-line camelcase
210215
flow_time: Math.floor(event.flowTime), //eslint-disable-line camelcase
211216
hostname: HOSTNAME,
212217
locale: request.locale,
213218
op: 'flowEvent',
214219
pid: process.pid,
220+
region: location && location.state,
215221
time: new Date(event.time).toISOString(),
216222
userAgent: request.headers['user-agent'],
217223
v: VERSION

tests/server/amplitude.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ registerSuite('amplitude', {
6969
flowBeginTime: 'qux',
7070
flowId: 'wibble',
7171
lang: 'blee',
72+
location: {
73+
country: 'United States',
74+
state: 'California'
75+
},
7276
service: '1',
7377
uid: 'soop',
7478
utm_campaign: 'melm',
@@ -140,6 +144,10 @@ registerSuite('amplitude', {
140144
flowBeginTime: 'd',
141145
flowId: 'e',
142146
lang: 'f',
147+
location: {
148+
country: 'United Kingdom',
149+
state: 'Dorset'
150+
},
143151
service: 'g',
144152
uid: 'h',
145153
utm_campaign: 'i',
@@ -153,7 +161,7 @@ registerSuite('amplitude', {
153161
const arg = JSON.parse(process.stderr.write.args[0]);
154162
assert.deepEqual(arg, {
155163
app_version: APP_VERSION,
156-
country: 'United States',
164+
country: 'United Kingdom',
157165
device_id: 'b',
158166
device_model: 'iPad',
159167
event_properties: {
@@ -165,7 +173,7 @@ registerSuite('amplitude', {
165173
op: 'amplitudeEvent',
166174
os_name: 'iOS',
167175
os_version: '6.0',
168-
region: 'California',
176+
region: 'Dorset',
169177
session_id: 'd',
170178
time: 'a',
171179
user_id: 'h',
@@ -368,6 +376,10 @@ registerSuite('amplitude', {
368376
flowBeginTime: 'd',
369377
flowId: 'e',
370378
lang: 'f',
379+
location: {
380+
country: 'United States',
381+
state: 'California'
382+
},
371383
service: '2',
372384
uid: 'h',
373385
utm_campaign: 'i',
@@ -869,6 +881,10 @@ registerSuite('amplitude', {
869881
flowBeginTime: 'd',
870882
flowId: 'e',
871883
lang: 'f',
884+
location: {
885+
country: 'United States',
886+
state: 'California'
887+
},
872888
service: 'g',
873889
uid: 'h',
874890
utm_campaign: 'i',
@@ -1061,6 +1077,10 @@ registerSuite('amplitude', {
10611077
flowBeginTime: 'd',
10621078
flowId: 'e',
10631079
lang: 'f',
1080+
location: {
1081+
country: 'United States',
1082+
state: 'California'
1083+
},
10641084
service: 'sync',
10651085
uid: 'h',
10661086
utm_campaign: 'i',

tests/server/flow-event.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ registerSuite('flow-event', {
3232
flowMetrics: {
3333
validate: sandbox.spy(() => flowMetricsValidateResult)
3434
},
35+
geolocate: sandbox.spy(() => ({ country: 'United States', state: 'California' })),
3536
request: {
3637
headers: {
3738
'user-agent': 'bar'
@@ -43,7 +44,8 @@ registerSuite('flow-event', {
4344
flowEvent = proxyquire(path.resolve('server/lib/flow-event'), {
4445
'./amplitude': mocks.amplitude,
4546
'./configuration': mocks.config,
46-
'./flow-metrics': mocks.flowMetrics
47+
'./flow-metrics': mocks.flowMetrics,
48+
'./geo-locate': mocks.geolocate
4749
}).metricsRequest;
4850
},
4951

@@ -88,6 +90,7 @@ registerSuite('flow-event', {
8890
assert.deepEqual(JSON.parse(args[0]), {
8991
/*eslint-disable camelcase*/
9092
context: 'fx_desktop_v3',
93+
country: 'United States',
9194
entrypoint: 'menupanel',
9295
event: 'flow.begin',
9396
flow_id: '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
@@ -97,6 +100,7 @@ registerSuite('flow-event', {
97100
migration: 'amo',
98101
op: 'flowEvent',
99102
pid: process.pid,
103+
region: 'California',
100104
service: '1234567890abcdef',
101105
time: new Date(mocks.time - 1000).toISOString(),
102106
userAgent: mocks.request.headers['user-agent'],
@@ -111,22 +115,22 @@ registerSuite('flow-event', {
111115

112116
'second call to process.stderr.write was correct': () => {
113117
const arg = JSON.parse(process.stderr.write.args[1][0]);
114-
assert.lengthOf(Object.keys(arg), 18);
118+
assert.lengthOf(Object.keys(arg), 20);
115119
assert.equal(arg.event, 'flow.signup.view');
116120
assert.equal(arg.flow_time, 5);
117121
assert.equal(arg.time, new Date(mocks.time - 995).toISOString());
118122
},
119123

120124
'third call to process.stderr.write was correct': () => {
121125
const arg = JSON.parse(process.stderr.write.args[2][0]);
122-
assert.lengthOf(Object.keys(arg), 18);
126+
assert.lengthOf(Object.keys(arg), 20);
123127
assert.equal(arg.event, 'flow.signup.good-offset-now');
124128
assert.equal(arg.time, new Date(mocks.time).toISOString());
125129
},
126130

127131
'fourth call to process.stderr.write was correct': () => {
128132
const arg = JSON.parse(process.stderr.write.args[3][0]);
129-
assert.lengthOf(Object.keys(arg), 18);
133+
assert.lengthOf(Object.keys(arg), 20);
130134
assert.equal(arg.event, 'flow.signup.good-offset-oldest');
131135
assert.equal(arg.time, new Date(mocks.time - config.flow_id_expiry).toISOString());
132136
},
@@ -159,6 +163,10 @@ registerSuite('flow-event', {
159163
type: 'wibble'
160164
});
161165
assert.equal(args[1], mocks.request);
166+
assert.deepEqual(args[2].location, {
167+
country: 'United States',
168+
state: 'California'
169+
});
162170
mocks.amplitude.firstCall.calledBefore(process.stderr.write.firstCall);
163171
},
164172

@@ -799,7 +807,7 @@ registerSuite('flow-event', {
799807
'process.stderr.write was called correctly': () => {
800808
assert.equal(process.stderr.write.callCount, 1);
801809
const arg = JSON.parse(process.stderr.write.args[0][0]);
802-
assert.lengthOf(Object.keys(arg), 17);
810+
assert.lengthOf(Object.keys(arg), 19);
803811
assert.isUndefined(arg.utm_campaign); //eslint-disable-line camelcase
804812
}
805813
}
@@ -816,7 +824,7 @@ registerSuite('flow-event', {
816824
'process.stderr.write was called correctly': () => {
817825
assert.equal(process.stderr.write.callCount, 1);
818826
const arg = JSON.parse(process.stderr.write.args[0][0]);
819-
assert.lengthOf(Object.keys(arg), 17);
827+
assert.lengthOf(Object.keys(arg), 19);
820828
assert.isUndefined(arg.utm_content); //eslint-disable-line camelcase
821829
}
822830
}
@@ -833,7 +841,7 @@ registerSuite('flow-event', {
833841
'process.stderr.write was called correctly': () => {
834842
assert.equal(process.stderr.write.callCount, 1);
835843
const arg = JSON.parse(process.stderr.write.args[0][0]);
836-
assert.lengthOf(Object.keys(arg), 17);
844+
assert.lengthOf(Object.keys(arg), 19);
837845
assert.isUndefined(arg.utm_medium); //eslint-disable-line camelcase
838846
}
839847
}
@@ -850,7 +858,7 @@ registerSuite('flow-event', {
850858
'process.stderr.write was called correctly': () => {
851859
assert.equal(process.stderr.write.callCount, 1);
852860
const arg = JSON.parse(process.stderr.write.args[0][0]);
853-
assert.lengthOf(Object.keys(arg), 17);
861+
assert.lengthOf(Object.keys(arg), 19);
854862
assert.isUndefined(arg.utm_source); //eslint-disable-line camelcase
855863
}
856864
}

0 commit comments

Comments
 (0)