@segment/analytics-next
Version:
Analytics Next (aka Analytics 2.0) is the latest version of Segment’s JavaScript SDK - enabling you to send your data to any tool without having to learn, test, or use a new API every time.
77 lines • 3.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventFactory = void 0;
var tslib_1 = require("tslib");
var uuid_1 = require("@lukeed/uuid");
var page_1 = require("../page");
var analytics_core_1 = require("@segment/analytics-core");
tslib_1.__exportStar(require("./interfaces"), exports);
var EventFactory = /** @class */ (function (_super) {
tslib_1.__extends(EventFactory, _super);
function EventFactory(user) {
var _this = _super.call(this, {
createMessageId: function () { return "ajs-next-".concat(Date.now(), "-").concat((0, uuid_1.v4)()); },
onEventMethodCall: function (_a) {
var options = _a.options;
_this.maybeUpdateAnonId(options);
},
onFinishedEvent: function (event) {
_this.addIdentity(event);
return event;
},
}) || this;
_this.user = user;
return _this;
}
/**
* Updates the anonymousId *globally* if it's provided in the options.
* This should generally be done in the identify method, but some customers rely on this.
*/
EventFactory.prototype.maybeUpdateAnonId = function (options) {
(options === null || options === void 0 ? void 0 : options.anonymousId) && this.user.anonymousId(options.anonymousId);
};
/**
* add user id / anonymous id to the event
*/
EventFactory.prototype.addIdentity = function (event) {
if (this.user.id()) {
event.userId = this.user.id();
}
if (this.user.anonymousId()) {
event.anonymousId = this.user.anonymousId();
}
};
EventFactory.prototype.track = function (event, properties, options, globalIntegrations, pageCtx) {
var ev = _super.prototype.track.call(this, event, properties, options, globalIntegrations);
(0, page_1.addPageContext)(ev, pageCtx);
return ev;
};
EventFactory.prototype.page = function (category, page, properties, options, globalIntegrations, pageCtx) {
var ev = _super.prototype.page.call(this, category, page, properties, options, globalIntegrations);
(0, page_1.addPageContext)(ev, pageCtx);
return ev;
};
EventFactory.prototype.screen = function (category, screen, properties, options, globalIntegrations, pageCtx) {
var ev = _super.prototype.screen.call(this, category, screen, properties, options, globalIntegrations);
(0, page_1.addPageContext)(ev, pageCtx);
return ev;
};
EventFactory.prototype.identify = function (userId, traits, options, globalIntegrations, pageCtx) {
var ev = _super.prototype.identify.call(this, userId, traits, options, globalIntegrations);
(0, page_1.addPageContext)(ev, pageCtx);
return ev;
};
EventFactory.prototype.group = function (groupId, traits, options, globalIntegrations, pageCtx) {
var ev = _super.prototype.group.call(this, groupId, traits, options, globalIntegrations);
(0, page_1.addPageContext)(ev, pageCtx);
return ev;
};
EventFactory.prototype.alias = function (to, from, options, globalIntegrations, pageCtx) {
var ev = _super.prototype.alias.call(this, to, from, options, globalIntegrations);
(0, page_1.addPageContext)(ev, pageCtx);
return ev;
};
return EventFactory;
}(analytics_core_1.CoreEventFactory));
exports.EventFactory = EventFactory;
//# sourceMappingURL=index.js.map