Permalink
Browse files

feat(metrics): add country and region to activity and flow events

  • Loading branch information...
philbooth committed Dec 28, 2018
1 parent 49d83bb commit c0c9739a23eacbef8aadef0a74fab88de439c233
@@ -156,8 +156,11 @@ module.exports = (log, config) => {
}

function emitActivityEvent (event, request, data) {
const { location } = request.app.geo
data = Object.assign({
country: location && location.country,
event,
region: location && location.state,
userAgent: request.headers['user-agent']
}, data)

@@ -172,9 +175,12 @@ module.exports = (log, config) => {
return P.resolve()
}

const { location } = request.app.geo
return request.gatherMetricsContext({
country: location && location.country,
event: event,
locale: request.app && request.app.locale,
region: location && location.state,
userAgent: request.headers['user-agent']
}).then(data => {
if (data.flow_id) {
@@ -261,7 +261,9 @@ describe('lib/devices:', () => {
args = log.activityEvent.args[0]
assert.equal(args.length, 1, 'log.activityEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United States',
event: 'device.created',
region: 'California',
service: undefined,
userAgent: 'test user-agent',
uid: sessionToken.uid,
@@ -358,7 +360,9 @@ describe('lib/devices:', () => {
args = log.activityEvent.args[0]
assert.equal(args.length, 1, 'log.activityEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United States',
event: 'device.updated',
region: 'California',
service: undefined,
userAgent: 'test user-agent',
uid: sessionToken.uid,
@@ -84,7 +84,9 @@ describe('metrics/events', () => {
let args = log.activityEvent.args[0]
assert.equal(args.length, 1, 'log.activityEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United States',
event: 'device.created',
region: 'California',
userAgent: 'foo',
service: 'bar',
uid: 'baz'
@@ -116,7 +118,9 @@ describe('metrics/events', () => {
const args = log.activityEvent.args[0]
assert.equal(args.length, 1, 'log.activityEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United States',
event: 'device.created',
region: 'California',
userAgent: 'test user-agent',
service: 'bar'
}, 'argument was event data')
@@ -139,7 +143,9 @@ describe('metrics/events', () => {
const args = log.activityEvent.args[0]
assert.equal(args.length, 1, 'log.activityEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United States',
event: 'device.created',
region: 'California',
service: undefined,
userAgent: 'test user-agent'
}, 'argument was event data')
@@ -189,13 +195,15 @@ describe('metrics/events', () => {
args = log.flowEvent.args[0]
assert.equal(args.length, 1, 'log.flowEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United States',
event: 'email.verification.sent',
flow_id: 'bar',
flow_time: 1000,
flowBeginTime: time - 1000,
flowCompleteSignal: 'account.signed',
flowType: undefined,
locale: 'en-US',
region: 'California',
time,
uid: 'deadbeef',
userAgent: 'test user-agent',
@@ -222,7 +230,12 @@ describe('metrics/events', () => {
const request = {
app: {
devices: P.resolve(),
geo: {},
geo: {
location: {
country: 'United Kingdom',
state: 'Dorset'
}
},
locale: 'en',
ua: {}
},
@@ -249,13 +262,15 @@ describe('metrics/events', () => {
const args = log.flowEvent.args[0]
assert.equal(args.length, 1, 'log.flowEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United Kingdom',
event: 'email.verification.sent',
flow_id: 'bar',
flow_time: 1000,
flowBeginTime: time - 1000,
flowCompleteSignal: 'account.signed',
flowType: undefined,
locale: 'en',
region: 'Dorset',
time,
userAgent: 'foo'
}, 'argument was event data')
@@ -295,13 +310,15 @@ describe('metrics/events', () => {
const args = log.flowEvent.args[0]
assert.equal(args.length, 1, 'log.flowEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United States',
event: 'email.verification.sent',
flow_id: 'bar',
flow_time: 1000,
flowBeginTime: time - 1000,
flowCompleteSignal: 'account.signed',
flowType: undefined,
locale: 'en-US',
region: 'California',
time,
uid: 'deadbeef',
userAgent: 'test user-agent'
@@ -342,13 +359,15 @@ describe('metrics/events', () => {
const args = log.flowEvent.args[0]
assert.equal(args.length, 1, 'log.flowEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United States',
event: 'email.verification.sent',
flow_id: 'bar',
flow_time: 1000,
flowBeginTime: time - 1000,
flowCompleteSignal: 'account.signed',
flowType: undefined,
locale: 'en-US',
region: 'California',
time,
uid: 'deadbeef',
userAgent: 'test user-agent'
@@ -389,13 +408,15 @@ describe('metrics/events', () => {
const args = log.flowEvent.args[0]
assert.equal(args.length, 1, 'log.flowEvent was passed one argument')
assert.deepEqual(args[0], {
country: 'United States',
event: 'email.verification.sent',
flow_id: 'bar',
flow_time: 1000,
flowBeginTime: time - 1000,
flowCompleteSignal: 'account.signed',
flowType: undefined,
locale: 'en-US',
region: 'California',
time,
userAgent: 'test user-agent'
}, 'argument was event data')
@@ -435,25 +456,29 @@ describe('metrics/events', () => {

assert.equal(log.flowEvent.callCount, 2, 'log.flowEvent was called twice')
assert.deepEqual(log.flowEvent.args[0][0], {
country: 'United States',
event: 'email.verification.sent',
flow_id: 'bar',
flow_time: 2000,
flowBeginTime: time - 2000,
flowCompleteSignal: 'email.verification.sent',
flowType: 'registration',
locale: 'fr',
region: 'California',
time,
uid: 'qux',
userAgent: 'test user-agent'
}, 'argument was event data first time')
assert.deepEqual(log.flowEvent.args[1][0], {
country: 'United States',
event: 'flow.complete',
flow_id: 'bar',
flow_time: 2000,
flowBeginTime: time - 2000,
flowCompleteSignal: 'email.verification.sent',
flowType: 'registration',
locale: 'fr',
region: 'California',
time,
uid: 'qux',
userAgent: 'test user-agent'
@@ -562,7 +587,9 @@ describe('metrics/events', () => {
.then(() => {
assert.equal(log.activityEvent.callCount, 1, 'log.activityEvent was called once')
assert.deepEqual(log.activityEvent.args[0][0], {
country: 'United States',
event: 'account.keyfetch',
region: 'California',
userAgent: 'test user-agent',
service: undefined,
uid: 'baz'
@@ -572,6 +599,7 @@ describe('metrics/events', () => {

assert.equal(log.flowEvent.callCount, 1, 'log.flowEvent was called once')
assert.deepEqual(log.flowEvent.args[0][0], {
country: 'United States',
time,
event: 'account.keyfetch',
flow_id: 'bar',
@@ -580,6 +608,7 @@ describe('metrics/events', () => {
flowCompleteSignal: undefined,
flowType: undefined,
locale: 'en-US',
region: 'California',
uid: 'baz',
userAgent: 'test user-agent'
}, 'flow event data was correct')
@@ -721,27 +750,31 @@ describe('metrics/events', () => {
let args = log.flowEvent.args[0]
assert.equal(args.length, 1, 'log.flowEvent was passed one argument first time')
assert.deepEqual(args[0], {
country: 'United States',
event: 'route./account/create.200',
flow_id: 'bar',
flow_time: 1000,
flowBeginTime: time - 1000,
flowCompleteSignal: undefined,
flowType: undefined,
locale: 'en-US',
region: 'California',
time,
userAgent: 'test user-agent'
}, 'argument was route summary event data')

args = log.flowEvent.args[1]
assert.equal(args.length, 1, 'log.flowEvent was passed one argument second time')
assert.deepEqual(args[0], {
country: 'United States',
event: 'route.performance./account/create',
flow_id: 'bar',
flow_time: 42,
flowBeginTime: time - 1000,
flowCompleteSignal: undefined,
flowType: undefined,
locale: 'en-US',
region: 'California',
time,
userAgent: 'test user-agent'
}, 'argument was performance event data')
@@ -778,13 +811,15 @@ describe('metrics/events', () => {

assert.equal(log.flowEvent.callCount, 1, 'log.flowEvent was called once')
assert.deepEqual(log.flowEvent.args[0][0], {
country: 'United States',
event: 'route./account/login.399',
flow_id: 'bar',
flow_time: 1000,
flowBeginTime: time - 1000,
flowCompleteSignal: undefined,
flowType: undefined,
locale: 'en-US',
region: 'California',
time,
userAgent: 'test user-agent'
}, 'argument was event data')
@@ -821,13 +856,15 @@ describe('metrics/events', () => {

assert.equal(log.flowEvent.callCount, 1, 'log.flowEvent was called once')
assert.deepEqual(log.flowEvent.args[0][0], {
country: 'United States',
event: 'route./recovery_email/resend_code.400.999',
flow_id: 'bar',
flow_time: 1000,
flowBeginTime: time - 1000,
flowCompleteSignal: undefined,
flowType: undefined,
locale: 'en-US',
region: 'California',
time,
userAgent: 'test user-agent'
}, 'argument was event data')
@@ -894,13 +931,15 @@ describe('metrics/events', () => {

assert.equal(log.flowEvent.callCount, 1, 'log.flowEvent was called once')
assert.deepEqual(log.flowEvent.args[0][0], {
country: 'United States',
event: 'route./account/destroy.400.42',
flow_id: 'bar',
flow_time: 1000,
flowBeginTime: time - 1000,
flowCompleteSignal: undefined,
flowType: undefined,
locale: 'en-US',
region: 'California',
time,
userAgent: 'test user-agent'
}, 'argument was event data')
Oops, something went wrong.

0 comments on commit c0c9739

Please sign in to comment.