Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / web / track-new-user.js
blob46db93be0fcd637f92daa00a000ce7b904ea08c7
1 'use strict';
3 const _ = require('lodash');
4 var env = require('gitter-web-env');
5 var stats = env.stats;
7 var emailAddressService = require('gitter-web-email-addresses');
8 var gaCookieParser = require('./ga-cookie-parser');
10 // Use this whenever a user first signs up.
11 module.exports = function trackNewUser(req, user, provider) {
12 // NOTE: tracking a signup after an invite is separate to this
13 emailAddressService(user).then(function(email) {
14 const emailList = user.emails || [];
15 emailList.unshift(email);
16 // We are sanitizing the data a bit here so the database
17 // doesn't store `null` and duplicate case different emails
18 const lowerCaseEmailList = emailList.filter(email => !!email).map(email => email.toLowerCase());
19 user.emails = _.uniq(lowerCaseEmailList);
21 stats.userUpdate(
22 Object.assign({}, user, {
23 // this is only set because stats.userUpdate requires it
24 email: email
28 // NOTE: other stats calls also pass in properties
29 stats.event('new_user', {
30 userId: user.id,
31 email: email,
32 method: provider + '_oauth',
33 username: user.username,
34 source: req.session.source,
35 googleAnalyticsUniqueId: gaCookieParser(req)
36 });
38 // Persist the new emails
39 return user.save();
40 });