@cto.ai/ops
Version:
💻 CTO.ai - The CLI built for Teams 🚀
77 lines (76 loc) • 2.63 kB
JavaScript
;
/**
* @author: JP Lew (jp@cto.ai)
* @date: Sunday, 28th April 2019 1:16:46 am
* @lastModifiedBy: JP Lew (jp@cto.ai)
* @lastModifiedTime: Wednesday, 4th September 2019 3:08:35 pm
* @copyright (c) 2019 CTO.ai
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnalyticsService = void 0;
const tslib_1 = require("tslib");
const analytics_node_1 = tslib_1.__importDefault(require("analytics-node"));
const axios_1 = tslib_1.__importDefault(require("axios"));
const debug_1 = tslib_1.__importDefault(require("debug"));
const env_1 = require("./../constants/env");
const debug = (0, debug_1.default)('ops:AnalyticsService');
// interface AnalyticsTrack {
// userId?: string | number
// teamId?: string | number
// anonymousId?: string | number
// event: string
// cliEvent?: string
// properties?: any
// timestamp?: Date
// context?: any
// integrations?: any
// }
class AnalyticsService {
constructor(writeKey = env_1.OPS_SEGMENT_KEY) {
this.ANALYTICS_URL = `${env_1.OPS_API_HOST}analytics-service/private/events`;
this.segmentClient = new analytics_node_1.default(writeKey);
}
// The track method lets you record the actions your users perform.
track(event, properties, config) {
if (env_1.OPS_DEBUG) {
return null;
}
const { user: { email }, team: { name: activeTeam }, version, tokens: { accessToken }, } = config;
properties = Object.assign(Object.assign({}, properties), { team: activeTeam, email });
// TODO figure out why version is available for every command
if (version) {
properties.version = version;
}
if (email && activeTeam && accessToken) {
axios_1.default
.post(this.ANALYTICS_URL, {
metadata: {
userId: email,
event,
properties,
},
tags: ['track'],
}, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
.catch(err => {
debug('%O', err);
});
}
// TODO: There are type errors in this call, are we using the Segment client correctly?
return this.segmentClient.track({
event,
properties,
// @ts-ignore
team: activeTeam,
userId: email,
}, (err) => {
if (err) {
debug('%O', err);
}
});
}
}
exports.AnalyticsService = AnalyticsService;