@segment/analytics-core
Advanced tools
Comparing version 1.5.0 to 1.5.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EventFactory = void 0; | ||
exports.CoreEventFactory = void 0; | ||
var tslib_1 = require("tslib"); | ||
@@ -10,15 +10,25 @@ tslib_1.__exportStar(require("./interfaces"), exports); | ||
/** | ||
* This is currently only used by node.js, but the original idea was to have something that could be shared between browser and node. | ||
* Unfortunately, there are some differences in the way the two environments handle events, so this is not currently shared. | ||
* Internal settings object that is used internally by the factory | ||
*/ | ||
var EventFactory = /** @class */ (function () { | ||
function EventFactory(settings) { | ||
this.user = settings.user; | ||
var InternalEventFactorySettings = /** @class */ (function () { | ||
function InternalEventFactorySettings(settings) { | ||
var _a, _b; | ||
this.settings = settings; | ||
this.createMessageId = settings.createMessageId; | ||
this.onEventMethodCall = (_a = settings.onEventMethodCall) !== null && _a !== void 0 ? _a : (function () { }); | ||
this.onFinishedEvent = (_b = settings.onFinishedEvent) !== null && _b !== void 0 ? _b : (function () { }); | ||
} | ||
EventFactory.prototype.track = function (event, properties, options, globalIntegrations) { | ||
return InternalEventFactorySettings; | ||
}()); | ||
var CoreEventFactory = /** @class */ (function () { | ||
function CoreEventFactory(settings) { | ||
this.settings = new InternalEventFactorySettings(settings); | ||
} | ||
CoreEventFactory.prototype.track = function (event, properties, options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'track', options: options }); | ||
return this.normalize(tslib_1.__assign(tslib_1.__assign({}, this.baseEvent()), { event: event, type: 'track', properties: properties !== null && properties !== void 0 ? properties : {}, options: tslib_1.__assign({}, options), integrations: tslib_1.__assign({}, globalIntegrations) })); | ||
}; | ||
EventFactory.prototype.page = function (category, page, properties, options, globalIntegrations) { | ||
CoreEventFactory.prototype.page = function (category, page, properties, options, globalIntegrations) { | ||
var _a; | ||
this.settings.onEventMethodCall({ type: 'page', options: options }); | ||
var event = { | ||
@@ -40,3 +50,4 @@ type: 'page', | ||
}; | ||
EventFactory.prototype.screen = function (category, screen, properties, options, globalIntegrations) { | ||
CoreEventFactory.prototype.screen = function (category, screen, properties, options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'screen', options: options }); | ||
var event = { | ||
@@ -56,11 +67,14 @@ type: 'screen', | ||
}; | ||
EventFactory.prototype.identify = function (userId, traits, options, globalIntegrations) { | ||
CoreEventFactory.prototype.identify = function (userId, traits, options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'identify', options: options }); | ||
return this.normalize(tslib_1.__assign(tslib_1.__assign({}, this.baseEvent()), { type: 'identify', userId: userId, traits: traits !== null && traits !== void 0 ? traits : {}, options: tslib_1.__assign({}, options), integrations: globalIntegrations })); | ||
}; | ||
EventFactory.prototype.group = function (groupId, traits, options, globalIntegrations) { | ||
CoreEventFactory.prototype.group = function (groupId, traits, options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'group', options: options }); | ||
return this.normalize(tslib_1.__assign(tslib_1.__assign({}, this.baseEvent()), { type: 'group', traits: traits !== null && traits !== void 0 ? traits : {}, options: tslib_1.__assign({}, options), integrations: tslib_1.__assign({}, globalIntegrations), // | ||
groupId: groupId })); | ||
}; | ||
EventFactory.prototype.alias = function (to, from, // TODO: can we make this undefined? | ||
CoreEventFactory.prototype.alias = function (to, from, // TODO: can we make this undefined? | ||
options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'alias', options: options }); | ||
var base = { | ||
@@ -80,17 +94,7 @@ userId: to, | ||
}; | ||
EventFactory.prototype.baseEvent = function () { | ||
var base = { | ||
CoreEventFactory.prototype.baseEvent = function () { | ||
return { | ||
integrations: {}, | ||
options: {}, | ||
}; | ||
if (!this.user) | ||
return base; | ||
var user = this.user; | ||
if (user.id()) { | ||
base.userId = user.id(); | ||
} | ||
if (user.anonymousId()) { | ||
base.anonymousId = user.anonymousId(); | ||
} | ||
return base; | ||
}; | ||
@@ -101,3 +105,3 @@ /** | ||
*/ | ||
EventFactory.prototype.context = function (options) { | ||
CoreEventFactory.prototype.context = function (options) { | ||
var _a; | ||
@@ -131,3 +135,3 @@ /** | ||
}; | ||
EventFactory.prototype.normalize = function (event) { | ||
CoreEventFactory.prototype.normalize = function (event) { | ||
var _a, _b; | ||
@@ -152,10 +156,10 @@ var integrationBooleans = Object.keys((_a = event.integrations) !== null && _a !== void 0 ? _a : {}).reduce(function (integrationNames, name) { | ||
var options = event.options, rest = tslib_1.__rest(event, ["options"]); | ||
var body = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({ timestamp: new Date() }, rest), { integrations: allIntegrations, context: context }), overrides); | ||
var evt = tslib_1.__assign(tslib_1.__assign({}, body), { messageId: options.messageId || this.createMessageId() }); | ||
var evt = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({ timestamp: new Date() }, rest), { context: context, integrations: allIntegrations }), overrides), { messageId: options.messageId || this.settings.createMessageId() }); | ||
this.settings.onFinishedEvent(evt); | ||
(0, assertions_1.validateEvent)(evt); | ||
return evt; | ||
}; | ||
return EventFactory; | ||
return CoreEventFactory; | ||
}()); | ||
exports.EventFactory = EventFactory; | ||
exports.CoreEventFactory = CoreEventFactory; | ||
//# sourceMappingURL=index.js.map |
@@ -47,3 +47,4 @@ "use strict"; | ||
}); | ||
_this.plugins = _this.plugins.filter(function (p) { return p === plugin; }); | ||
// Filter out the failed plugin by excluding it from the list | ||
_this.plugins = _this.plugins.filter(function (p) { return p !== plugin; }); | ||
}); | ||
@@ -50,0 +51,0 @@ return [3 /*break*/, 3]; |
@@ -9,2 +9,3 @@ "use strict"; | ||
var nilError = 'is nil'; | ||
// user identity check could hypothetically could be used in the browser event factory, but not 100% sure -- so this is node only for now | ||
function assertUserIdentity(event) { | ||
@@ -72,5 +73,4 @@ var USER_FIELD_NAME = '.userId/anonymousId/previousId/groupId'; | ||
} | ||
assertUserIdentity(event); | ||
} | ||
exports.validateEvent = validateEvent; | ||
//# sourceMappingURL=assertions.js.map |
@@ -7,15 +7,25 @@ import { __assign, __rest } from "tslib"; | ||
/** | ||
* This is currently only used by node.js, but the original idea was to have something that could be shared between browser and node. | ||
* Unfortunately, there are some differences in the way the two environments handle events, so this is not currently shared. | ||
* Internal settings object that is used internally by the factory | ||
*/ | ||
var EventFactory = /** @class */ (function () { | ||
function EventFactory(settings) { | ||
this.user = settings.user; | ||
var InternalEventFactorySettings = /** @class */ (function () { | ||
function InternalEventFactorySettings(settings) { | ||
var _a, _b; | ||
this.settings = settings; | ||
this.createMessageId = settings.createMessageId; | ||
this.onEventMethodCall = (_a = settings.onEventMethodCall) !== null && _a !== void 0 ? _a : (function () { }); | ||
this.onFinishedEvent = (_b = settings.onFinishedEvent) !== null && _b !== void 0 ? _b : (function () { }); | ||
} | ||
EventFactory.prototype.track = function (event, properties, options, globalIntegrations) { | ||
return InternalEventFactorySettings; | ||
}()); | ||
var CoreEventFactory = /** @class */ (function () { | ||
function CoreEventFactory(settings) { | ||
this.settings = new InternalEventFactorySettings(settings); | ||
} | ||
CoreEventFactory.prototype.track = function (event, properties, options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'track', options: options }); | ||
return this.normalize(__assign(__assign({}, this.baseEvent()), { event: event, type: 'track', properties: properties !== null && properties !== void 0 ? properties : {}, options: __assign({}, options), integrations: __assign({}, globalIntegrations) })); | ||
}; | ||
EventFactory.prototype.page = function (category, page, properties, options, globalIntegrations) { | ||
CoreEventFactory.prototype.page = function (category, page, properties, options, globalIntegrations) { | ||
var _a; | ||
this.settings.onEventMethodCall({ type: 'page', options: options }); | ||
var event = { | ||
@@ -37,3 +47,4 @@ type: 'page', | ||
}; | ||
EventFactory.prototype.screen = function (category, screen, properties, options, globalIntegrations) { | ||
CoreEventFactory.prototype.screen = function (category, screen, properties, options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'screen', options: options }); | ||
var event = { | ||
@@ -53,11 +64,14 @@ type: 'screen', | ||
}; | ||
EventFactory.prototype.identify = function (userId, traits, options, globalIntegrations) { | ||
CoreEventFactory.prototype.identify = function (userId, traits, options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'identify', options: options }); | ||
return this.normalize(__assign(__assign({}, this.baseEvent()), { type: 'identify', userId: userId, traits: traits !== null && traits !== void 0 ? traits : {}, options: __assign({}, options), integrations: globalIntegrations })); | ||
}; | ||
EventFactory.prototype.group = function (groupId, traits, options, globalIntegrations) { | ||
CoreEventFactory.prototype.group = function (groupId, traits, options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'group', options: options }); | ||
return this.normalize(__assign(__assign({}, this.baseEvent()), { type: 'group', traits: traits !== null && traits !== void 0 ? traits : {}, options: __assign({}, options), integrations: __assign({}, globalIntegrations), // | ||
groupId: groupId })); | ||
}; | ||
EventFactory.prototype.alias = function (to, from, // TODO: can we make this undefined? | ||
CoreEventFactory.prototype.alias = function (to, from, // TODO: can we make this undefined? | ||
options, globalIntegrations) { | ||
this.settings.onEventMethodCall({ type: 'alias', options: options }); | ||
var base = { | ||
@@ -77,17 +91,7 @@ userId: to, | ||
}; | ||
EventFactory.prototype.baseEvent = function () { | ||
var base = { | ||
CoreEventFactory.prototype.baseEvent = function () { | ||
return { | ||
integrations: {}, | ||
options: {}, | ||
}; | ||
if (!this.user) | ||
return base; | ||
var user = this.user; | ||
if (user.id()) { | ||
base.userId = user.id(); | ||
} | ||
if (user.anonymousId()) { | ||
base.anonymousId = user.anonymousId(); | ||
} | ||
return base; | ||
}; | ||
@@ -98,3 +102,3 @@ /** | ||
*/ | ||
EventFactory.prototype.context = function (options) { | ||
CoreEventFactory.prototype.context = function (options) { | ||
var _a; | ||
@@ -128,3 +132,3 @@ /** | ||
}; | ||
EventFactory.prototype.normalize = function (event) { | ||
CoreEventFactory.prototype.normalize = function (event) { | ||
var _a, _b; | ||
@@ -149,10 +153,10 @@ var integrationBooleans = Object.keys((_a = event.integrations) !== null && _a !== void 0 ? _a : {}).reduce(function (integrationNames, name) { | ||
var options = event.options, rest = __rest(event, ["options"]); | ||
var body = __assign(__assign(__assign({ timestamp: new Date() }, rest), { integrations: allIntegrations, context: context }), overrides); | ||
var evt = __assign(__assign({}, body), { messageId: options.messageId || this.createMessageId() }); | ||
var evt = __assign(__assign(__assign(__assign({ timestamp: new Date() }, rest), { context: context, integrations: allIntegrations }), overrides), { messageId: options.messageId || this.settings.createMessageId() }); | ||
this.settings.onFinishedEvent(evt); | ||
validateEvent(evt); | ||
return evt; | ||
}; | ||
return EventFactory; | ||
return CoreEventFactory; | ||
}()); | ||
export { EventFactory }; | ||
export { CoreEventFactory }; | ||
//# sourceMappingURL=index.js.map |
@@ -44,3 +44,4 @@ import { __awaiter, __extends, __generator } from "tslib"; | ||
}); | ||
_this.plugins = _this.plugins.filter(function (p) { return p === plugin; }); | ||
// Filter out the failed plugin by excluding it from the list | ||
_this.plugins = _this.plugins.filter(function (p) { return p !== plugin; }); | ||
}); | ||
@@ -47,0 +48,0 @@ return [3 /*break*/, 3]; |
@@ -6,2 +6,3 @@ import { ValidationError } from './errors'; | ||
var nilError = 'is nil'; | ||
// user identity check could hypothetically could be used in the browser event factory, but not 100% sure -- so this is node only for now | ||
export function assertUserIdentity(event) { | ||
@@ -62,4 +63,3 @@ var USER_FIELD_NAME = '.userId/anonymousId/previousId/groupId'; | ||
} | ||
assertUserIdentity(event); | ||
} | ||
//# sourceMappingURL=assertions.js.map |
export * from './interfaces'; | ||
import { ID, User } from '../user'; | ||
import { ID } from '../user'; | ||
import { Integrations, EventProperties, CoreSegmentEvent, CoreOptions, UserTraits, GroupTraits } from './interfaces'; | ||
interface EventFactorySettings { | ||
export type EventMethodCallHook = ({ type, options, }: { | ||
type: 'track' | 'identify' | 'page' | 'group' | 'alias' | 'screen'; | ||
options?: CoreOptions; | ||
}) => void; | ||
export type EventHook = (event: CoreSegmentEvent) => void; | ||
export interface EventFactorySettings { | ||
/** | ||
* Universal `messageId` builder for all events (these must be unique) | ||
*/ | ||
createMessageId: () => string; | ||
user?: User; | ||
/** | ||
* Hook to do something with an event right before they are returned from the factory. | ||
* This includes event modification or additional validation. | ||
*/ | ||
onFinishedEvent?: EventHook; | ||
/** | ||
* Hook whenever an event method is called (track, page, etc.) | ||
* Can be used to update Options (or just listen) | ||
*/ | ||
onEventMethodCall?: EventMethodCallHook; | ||
} | ||
/** | ||
* This is currently only used by node.js, but the original idea was to have something that could be shared between browser and node. | ||
* Unfortunately, there are some differences in the way the two environments handle events, so this is not currently shared. | ||
*/ | ||
export declare class EventFactory { | ||
createMessageId: EventFactorySettings['createMessageId']; | ||
user?: User; | ||
export declare abstract class CoreEventFactory { | ||
private settings; | ||
constructor(settings: EventFactorySettings); | ||
@@ -29,4 +41,4 @@ track(event: string, properties?: EventProperties, options?: CoreOptions, globalIntegrations?: Integrations): CoreSegmentEvent; | ||
private context; | ||
normalize(event: CoreSegmentEvent): CoreSegmentEvent; | ||
private normalize; | ||
} | ||
//# sourceMappingURL=index.d.ts.map |
export type ID = string | null | undefined; | ||
export interface User { | ||
id(): ID; | ||
anonymousId(): ID; | ||
} | ||
//# sourceMappingURL=index.d.ts.map |
{ | ||
"name": "@segment/analytics-core", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
export * from './interfaces' | ||
import { dset } from 'dset' | ||
import { ID, User } from '../user' | ||
import { ID } from '../user' | ||
import { | ||
@@ -14,23 +14,54 @@ Integrations, | ||
import { pickBy } from '../utils/pick' | ||
import type { RemoveIndexSignature } from '../utils/ts-helpers' | ||
import { validateEvent } from '../validation/assertions' | ||
import type { RemoveIndexSignature } from '../utils/ts-helpers' | ||
interface EventFactorySettings { | ||
export type EventMethodCallHook = ({ | ||
type, | ||
options, | ||
}: { | ||
type: 'track' | 'identify' | 'page' | 'group' | 'alias' | 'screen' | ||
options?: CoreOptions | ||
}) => void | ||
export type EventHook = (event: CoreSegmentEvent) => void | ||
export interface EventFactorySettings { | ||
/** | ||
* Universal `messageId` builder for all events (these must be unique) | ||
*/ | ||
createMessageId: () => string | ||
user?: User | ||
/** | ||
* Hook to do something with an event right before they are returned from the factory. | ||
* This includes event modification or additional validation. | ||
*/ | ||
onFinishedEvent?: EventHook | ||
/** | ||
* Hook whenever an event method is called (track, page, etc.) | ||
* Can be used to update Options (or just listen) | ||
*/ | ||
onEventMethodCall?: EventMethodCallHook | ||
} | ||
/** | ||
* This is currently only used by node.js, but the original idea was to have something that could be shared between browser and node. | ||
* Unfortunately, there are some differences in the way the two environments handle events, so this is not currently shared. | ||
* Internal settings object that is used internally by the factory | ||
*/ | ||
export class EventFactory { | ||
createMessageId: EventFactorySettings['createMessageId'] | ||
user?: User | ||
class InternalEventFactorySettings { | ||
public createMessageId: EventFactorySettings['createMessageId'] | ||
public onEventMethodCall: EventMethodCallHook | ||
public onFinishedEvent: EventHook | ||
constructor(settings: EventFactorySettings) { | ||
this.user = settings.user | ||
constructor(public settings: EventFactorySettings) { | ||
this.createMessageId = settings.createMessageId | ||
this.onEventMethodCall = settings.onEventMethodCall ?? (() => {}) | ||
this.onFinishedEvent = settings.onFinishedEvent ?? (() => {}) | ||
} | ||
} | ||
export abstract class CoreEventFactory { | ||
private settings: InternalEventFactorySettings | ||
constructor(settings: EventFactorySettings) { | ||
this.settings = new InternalEventFactorySettings(settings) | ||
} | ||
track( | ||
@@ -42,2 +73,3 @@ event: string, | ||
) { | ||
this.settings.onEventMethodCall({ type: 'track', options }) | ||
return this.normalize({ | ||
@@ -60,2 +92,3 @@ ...this.baseEvent(), | ||
): CoreSegmentEvent { | ||
this.settings.onEventMethodCall({ type: 'page', options }) | ||
const event: CoreSegmentEvent = { | ||
@@ -91,2 +124,3 @@ type: 'page', | ||
): CoreSegmentEvent { | ||
this.settings.onEventMethodCall({ type: 'screen', options }) | ||
const event: CoreSegmentEvent = { | ||
@@ -119,2 +153,3 @@ type: 'screen', | ||
): CoreSegmentEvent { | ||
this.settings.onEventMethodCall({ type: 'identify', options }) | ||
return this.normalize({ | ||
@@ -136,2 +171,3 @@ ...this.baseEvent(), | ||
): CoreSegmentEvent { | ||
this.settings.onEventMethodCall({ type: 'group', options }) | ||
return this.normalize({ | ||
@@ -153,2 +189,3 @@ ...this.baseEvent(), | ||
): CoreSegmentEvent { | ||
this.settings.onEventMethodCall({ type: 'alias', options }) | ||
const base: CoreSegmentEvent = { | ||
@@ -179,20 +216,6 @@ userId: to, | ||
private baseEvent(): Partial<CoreSegmentEvent> { | ||
const base: Partial<CoreSegmentEvent> = { | ||
return { | ||
integrations: {}, | ||
options: {}, | ||
} | ||
if (!this.user) return base | ||
const user = this.user | ||
if (user.id()) { | ||
base.userId = user.id() | ||
} | ||
if (user.anonymousId()) { | ||
base.anonymousId = user.anonymousId() | ||
} | ||
return base | ||
} | ||
@@ -243,3 +266,3 @@ | ||
public normalize(event: CoreSegmentEvent): CoreSegmentEvent { | ||
private normalize(event: CoreSegmentEvent): CoreSegmentEvent { | ||
const integrationBooleans = Object.keys(event.integrations ?? {}).reduce( | ||
@@ -278,18 +301,16 @@ (integrationNames, name) => { | ||
const body = { | ||
const evt: CoreSegmentEvent = { | ||
timestamp: new Date(), | ||
...rest, | ||
context, | ||
integrations: allIntegrations, | ||
context, | ||
...overrides, | ||
messageId: options.messageId || this.settings.createMessageId(), | ||
} | ||
const evt: CoreSegmentEvent = { | ||
...body, | ||
messageId: options.messageId || this.createMessageId(), | ||
} | ||
this.settings.onFinishedEvent(evt) | ||
validateEvent(evt) | ||
validateEvent(evt) | ||
return evt | ||
} | ||
} |
@@ -64,3 +64,4 @@ import { CoreAnalytics } from '../analytics' | ||
this.plugins = this.plugins.filter((p) => p === plugin) | ||
// Filter out the failed plugin by excluding it from the list | ||
this.plugins = this.plugins.filter((p) => p !== plugin) | ||
}) | ||
@@ -67,0 +68,0 @@ } else { |
export type ID = string | null | undefined | ||
// TODO: this is a base user | ||
export interface User { | ||
id(): ID | ||
anonymousId(): ID | ||
} |
@@ -9,2 +9,3 @@ import { CoreSegmentEvent } from '../events' | ||
// user identity check could hypothetically could be used in the browser event factory, but not 100% sure -- so this is node only for now | ||
export function assertUserIdentity(event: CoreSegmentEvent): void { | ||
@@ -78,4 +79,2 @@ const USER_FIELD_NAME = '.userId/anonymousId/previousId/groupId' | ||
} | ||
assertUserIdentity(event) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
282956
5123