<commit_before>'use strict';

const _ = require('underscore');
const app = require('express')();
const Analytics = require('analytics-node');
const { secrets }  = require('app/config/config');
const { guid } = require('./users');

const segmentKey = process.env.SEGMENT_WRITE_KEY || secrets.segmentKey;
const env = process.env.ANALYTICS_ENV || app.get('env');
let analytics;

// Flush events immediately if on dev environment
if (env !== 'production') {
  analytics = new Analytics(segmentKey, { flushAt: 1 });
} else {
  analytics = new Analytics(segmentKey);
}

function generateOpts(req) {
  const user = req.user || req.anonymousUser;
  const { id: userId, anonymousId } = user;
  let opts = userId ? { userId } : { anonymousId };

  // Log the environment to differentiate events from production vs dev
  opts.properties = { environment: env, platform: 'Web' };

  return opts;
}

module.exports = {
  // https://segment.com/docs/sources/server/node/#identify
  identify(req) {
    let opts = generateOpts(req);

    if (req.user) {
      const { email, name, username } = req.user;
      opts.traits = { email, name, username };
    }

    analytics.identify(opts);
  },

  // https://segment.com/docs/sources/server/node/#track
  track(req, event, properties) {
    let opts = _.extend(generateOpts(req), { event, properties });
    analytics.track(opts);
  },

  // https://segment.com/docs/sources/server/node/#page
  page(req, category, name, properties) {
    let opts = _.extend(generateOpts(req), { category, name });
    opts.properties = _.extend(opts.properties, properties);
    analytics.page(opts);
  },

  // https://segment.com/docs/sources/server/node/#alias
  alias(req) {
    const { id: userId } = req.user;
    const { anonymousId: previousId } = req.anonymousUser;

    analytics.alias({ previousId, userId });
  }
};<commit_msg>Copy properties properly for analytics.track() calls<commit_after>'use strict';

const _ = require('underscore');
const app = require('express')();
const Analytics = require('analytics-node');
const { secrets }  = require('app/config/config');
const { guid } = require('./users');

const segmentKey = process.env.SEGMENT_WRITE_KEY || secrets.segmentKey;
const env = process.env.ANALYTICS_ENV || app.get('env');
let analytics;

// Flush events immediately if on dev environment
if (env !== 'production') {
  analytics = new Analytics(segmentKey, { flushAt: 1 });
} else {
  analytics = new Analytics(segmentKey);
}

function generateOpts(req) {
  const user = req.user || req.anonymousUser;
  const { id: userId, anonymousId } = user;
  let opts = userId ? { userId } : { anonymousId };

  // Log the environment to differentiate events from production vs dev
  opts.properties = { environment: env, platform: 'Web' };

  return opts;
}

module.exports = {
  // https://segment.com/docs/sources/server/node/#identify
  identify(req) {
    let opts = generateOpts(req);

    if (req.user) {
      const { email, name, username } = req.user;
      opts.traits = { email, name, username };
    }

    analytics.identify(opts);
  },

  // https://segment.com/docs/sources/server/node/#track
  track(req, event, properties) {
    let opts = _.extend(generateOpts(req), { event });
    _.extend(opts.properties, properties);
    analytics.track(opts);
  },

  // https://segment.com/docs/sources/server/node/#page
  page(req, category, name, properties) {
    let opts = _.extend(generateOpts(req), { category, name });
    _.extend(opts.properties, properties);
    analytics.page(opts);
  },

  // https://segment.com/docs/sources/server/node/#alias
  alias(req) {
    const { id: userId } = req.user;
    const { anonymousId: previousId } = req.anonymousUser;

    analytics.alias({ previousId, userId });
  }
};
{'fraction_non_alphanumeric': 0.155154091392136, 'fraction_numerical': 0.0005313496280552603, 'mean_word_length': 3.65389369592089, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1634158', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1155, 'n_words': 271}