'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _nodeMachineId = require('node-machine-id');

var _analyticsNode = require('analytics-node');

var _analyticsNode2 = _interopRequireDefault(_analyticsNode);

var _debug = require('./debug');

var _debug2 = _interopRequireDefault(_debug);

var _session = require('./session');

var _session2 = _interopRequireDefault(_session);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const { debug } = (0, _debug2.default)('utils-analytics');

const STG_KEY = 'CCupheBjgV6WI0emy3oRUnDyjQ8ngmgB';
const PROD_KEY = 'fLDtpYXRjFYnHlp1gvzl4I3Gv8gDoQ8m';

const ANALYTICS_WRITE_KEY = process.env.SYNCANO_ENV === 'test' ? STG_KEY : PROD_KEY;
const analytics = new _analyticsNode2.default(ANALYTICS_WRITE_KEY, {
  flushAt: 1,
  flushAfter: 100
});

const identify = details => {
  debug('identify');
  analytics.identify({
    userId: details.id,
    traits: {
      'First name': details.first_name,
      'Last name': details.last_name,
      source: 'Sign up',
      email: details.email,
      is_active: details.is_active
    }
  });
};

const track = (eventName, params) => {
  debug('track');
  analytics.track(eventName, params);
};

const trackCommand = (options, additionalParams = {}) => {
  debug('trackCommand');
  const cmd = options[options.length - 1];

  const props = Object.assign({
    version: cmd.parent.version(),
    args: cmd.parent.rawArgs.slice(2).join(' '),
    instance: _session2.default.project ? _session2.default.project.instance : null,
    type: 'command'
  }, additionalParams);

  if (_session2.default.userId) {
    track({
      userId: _session2.default.userId,
      event: cmd.name(),
      properties: props
    });
  } else {
    track({
      anonymousId: (0, _nodeMachineId.machineIdSync)(),
      event: cmd.name(),
      properties: props
    });
  }
};

exports.default = {
  identify,
  track,
  trackCommand
};
module.exports = exports['default'];
//# sourceMappingURL=analytics.js.map