128 lines
3.1 KiB
JavaScript
128 lines
3.1 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.flush = flush;
|
||
|
exports.setSegmentNodeKey = setSegmentNodeKey;
|
||
|
exports.setSegmentWebInstance = setSegmentWebInstance;
|
||
|
exports.setUserProperties = setUserProperties;
|
||
|
exports.setVersionName = setVersionName;
|
||
|
exports.logEvent = logEvent;
|
||
|
|
||
|
var _ip;
|
||
|
|
||
|
function _load_ip() {
|
||
|
return _ip = _interopRequireDefault(require('./ip'));
|
||
|
}
|
||
|
|
||
|
var _os = _interopRequireDefault(require('os'));
|
||
|
|
||
|
var _analyticsNode;
|
||
|
|
||
|
function _load_analyticsNode() {
|
||
|
return _analyticsNode = _interopRequireDefault(require('analytics-node'));
|
||
|
}
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
let _segmentNodeInstance;
|
||
|
|
||
|
let _segmentWebInstance;
|
||
|
let _userId;
|
||
|
let _version;
|
||
|
const PLATFORM_TO_ANALYTICS_PLATFORM = {
|
||
|
darwin: 'Mac',
|
||
|
win32: 'Windows',
|
||
|
linux: 'Linux'
|
||
|
};
|
||
|
|
||
|
function flush(cb) {
|
||
|
if (_segmentWebInstance) _segmentWebInstance.flush(cb);
|
||
|
if (_segmentNodeInstance) _segmentNodeInstance.flush(cb);
|
||
|
}
|
||
|
|
||
|
function setSegmentNodeKey(key) {
|
||
|
// Do not wait before flushing, we want node to close immediately if the programs ends
|
||
|
_segmentNodeInstance = new (_analyticsNode || _load_analyticsNode()).default(key, { flushAfter: 300 });
|
||
|
}
|
||
|
|
||
|
function setSegmentWebInstance(instance) {
|
||
|
_segmentWebInstance = instance;
|
||
|
}
|
||
|
|
||
|
function setUserProperties(userId, traits) {
|
||
|
_userId = userId;
|
||
|
|
||
|
if (_segmentNodeInstance) {
|
||
|
_segmentNodeInstance.identify({
|
||
|
userId,
|
||
|
traits,
|
||
|
context: _getContext()
|
||
|
});
|
||
|
}
|
||
|
|
||
|
if (_segmentWebInstance) {
|
||
|
// The Amplitude SDK isn't initialized right away, so call setVersion before every call to make sure it's actually updated.
|
||
|
setVersionName(_version);
|
||
|
|
||
|
window.analytics.identify(userId, traits, {
|
||
|
context: _getContext()
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function setVersionName(version) {
|
||
|
_version = version;
|
||
|
|
||
|
if (_segmentWebInstance && window.amplitude && window.amplitude.getInstance && window.amplitude.getInstance()) {
|
||
|
// Segment injects amplitude into the window. Call this manually because Segment isn't passing it along.
|
||
|
window.amplitude.getInstance().setVersionName(version);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function logEvent(name, properties = {}) {
|
||
|
if (_segmentNodeInstance && _userId) {
|
||
|
_segmentNodeInstance.track({
|
||
|
userId: _userId,
|
||
|
event: name,
|
||
|
properties,
|
||
|
context: _getContext()
|
||
|
});
|
||
|
}
|
||
|
|
||
|
if (_segmentWebInstance) {
|
||
|
// The Amplitude SDK isn't initialized right away, so call setVersion before every call to make sure it's actually updated.
|
||
|
setVersionName(_version);
|
||
|
|
||
|
window.analytics.track(name, properties, {
|
||
|
context: _getContext()
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function _getContext() {
|
||
|
let platform = PLATFORM_TO_ANALYTICS_PLATFORM[_os.default.platform()];
|
||
|
let context = {
|
||
|
ip: (_ip || _load_ip()).default.address(),
|
||
|
device: {
|
||
|
model: platform,
|
||
|
brand: platform
|
||
|
},
|
||
|
os: {
|
||
|
name: platform,
|
||
|
version: _os.default.release()
|
||
|
},
|
||
|
app: {}
|
||
|
};
|
||
|
|
||
|
if (_version) {
|
||
|
context.app = {
|
||
|
version: _version
|
||
|
};
|
||
|
}
|
||
|
|
||
|
return context;
|
||
|
}
|
||
|
//# sourceMappingURL=__sourcemaps__/Analytics.js.map
|