Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / web / track-user-login.js
blob7e449ab14051c29d5901eab62e3b826ca7dba99c
1 'use strict';
3 var env = require('gitter-web-env');
4 var stats = env.stats;
6 var _ = require('lodash');
7 var emailAddressService = require('gitter-web-email-addresses');
8 var useragentTagger = require('./user-agent-tagger');
10 // Use this whenever a user logs in again
11 module.exports = function trackUserLogin(req, user, provider) {
12 return emailAddressService(user).then(function(email) {
13 var properties = useragentTagger(req);
15 const emailList = user.emails || [];
16 emailList.unshift(email);
17 // We are sanitizing the data a bit here so the database
18 // doesn't store `null` and duplicate case different emails
19 const lowerCaseEmailList = emailList.filter(email => !!email).map(email => email.toLowerCase());
20 user.emails = _.uniq(lowerCaseEmailList);
22 stats.userUpdate(
23 Object.assign({}, user, {
24 // this is only set because stats.userUpdate requires it
25 email: email
26 }),
27 properties
30 // NOTE: other stats calls also pass in source and googleAnalyticsUniqueId
31 stats.event(
32 'user_login',
33 _.extend(
35 userId: user.id,
36 method: provider + '_oauth',
37 username: user.username
39 properties
43 // Persist the new emails
44 return user.save();
45 });