UNPKG

@expo/xdl

Version:
139 lines (108 loc) 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.AnalyticsClient = void 0; function _analyticsNode() { const data = _interopRequireDefault(require("analytics-node")); _analyticsNode = function () { return data; }; return data; } function _os() { const data = _interopRequireDefault(require("os")); _os = function () { return data; }; return data; } function _ip() { const data = _interopRequireDefault(require("./ip")); _ip = function () { return data; }; return data; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const PLATFORM_TO_ANALYTICS_PLATFORM = { darwin: 'Mac', win32: 'Windows', linux: 'Linux' }; let _userId; let _userTraits; class AnalyticsClient { constructor() { _defineProperty(this, "segmentNodeInstance", void 0); _defineProperty(this, "version", void 0); _defineProperty(this, "userIdentifyCalled", false); } flush() { if (this.segmentNodeInstance) { this.segmentNodeInstance.flush(); } } setSegmentNodeKey(key) { // Do not wait before flushing, we want node to close immediately if the programs ends this.segmentNodeInstance = new (_analyticsNode().default)(key, { flushInterval: 300 }); } setUserProperties(userId, traits) { _userId = userId; _userTraits = traits; this.ensureUserIdentified(); } setVersionName(version) { this.version = version; } logEvent(name, properties = {}) { if (this.segmentNodeInstance && _userId) { this.ensureUserIdentified(); this.segmentNodeInstance.track({ userId: _userId, event: name, properties, context: this.getContext() }); } } ensureUserIdentified() { if (this.segmentNodeInstance && !this.userIdentifyCalled && _userId) { this.segmentNodeInstance.identify({ userId: _userId, traits: _userTraits, context: this.getContext() }); this.userIdentifyCalled = true; } } getContext() { const platform = PLATFORM_TO_ANALYTICS_PLATFORM[_os().default.platform()] || _os().default.platform(); const context = { ip: _ip().default.address(), device: { model: platform, brand: platform }, os: { name: platform, version: _os().default.release() }, app: {} }; if (this.version) { context.app = { version: this.version }; } return context; } } exports.AnalyticsClient = AnalyticsClient; const defaultClient = new AnalyticsClient(); var _default = defaultClient; exports.default = _default; //# sourceMappingURL=__sourcemaps__/Analytics.js.map