UNPKG

1.41 kBJavaScriptView Raw
1"use strict";
2/**
3 * @author: JP Lew (jp@cto.ai)
4 * @date: Sunday, 28th April 2019 1:16:46 am
5 * @lastModifiedBy: JP Lew (jp@cto.ai)
6 * @lastModifiedTime: Wednesday, 4th September 2019 3:08:35 pm
7 * @copyright (c) 2019 CTO.ai
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10const tslib_1 = require("tslib");
11const analytics_node_1 = tslib_1.__importDefault(require("analytics-node"));
12const debug_1 = tslib_1.__importDefault(require("debug"));
13const env_1 = require("../constants/env");
14const Feathers_1 = require("./Feathers");
15const debug = debug_1.default('ops:AnalyticsService');
16class AnalyticsService {
17 constructor(writeKey = env_1.OPS_SEGMENT_KEY) {
18 this.segmentClient = new analytics_node_1.default(writeKey);
19 this.api = new Feathers_1.FeathersClient();
20 }
21 /* The track method lets you record the actions your users perform. */
22 track(payload, accessToken) {
23 if (env_1.OPS_DEBUG) {
24 return null;
25 }
26 if (accessToken) {
27 this.api
28 .create('/log/event', { metadata: payload, tags: ['track'] }, {
29 headers: {
30 Authorization: accessToken,
31 },
32 })
33 .catch(err => {
34 debug('%O', err);
35 });
36 }
37 return this.segmentClient.track(payload);
38 }
39}
40exports.AnalyticsService = AnalyticsService;