@amplitude/analytics-core
Advanced tools
Comparing version 0.6.3 to 0.6.4
@@ -9,3 +9,2 @@ import { Event, Config as IConfig, Logger as ILogger, InitOptions, LogLevel, Storage, Transport, Plan, ServerZone } from '@amplitude/analytics-types'; | ||
loggerProvider: Logger; | ||
saveEvents: boolean; | ||
optOut: boolean; | ||
@@ -25,7 +24,6 @@ serverUrl: string; | ||
plan?: Plan; | ||
saveEvents: boolean; | ||
serverUrl: string | undefined; | ||
serverZone?: ServerZone; | ||
transportProvider: Transport; | ||
storageProvider: Storage<Event[]>; | ||
storageProvider?: Storage<Event[]>; | ||
useBatch: boolean; | ||
@@ -32,0 +30,0 @@ private _optOut; |
@@ -7,8 +7,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
var getDefaultConfig = function () { return ({ | ||
flushMaxRetries: 5, | ||
flushQueueSize: 10, | ||
flushIntervalMillis: 1000, | ||
flushMaxRetries: 12, | ||
flushQueueSize: 200, | ||
flushIntervalMillis: 10000, | ||
logLevel: analytics_types_1.LogLevel.Warn, | ||
loggerProvider: new logger_1.Logger(), | ||
saveEvents: true, | ||
optOut: false, | ||
@@ -22,3 +21,3 @@ serverUrl: constants_1.AMPLITUDE_SERVER_URL, | ||
function Config(options) { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c; | ||
this._optOut = false; | ||
@@ -35,3 +34,2 @@ var defaultConfig = (0, exports.getDefaultConfig)(); | ||
this.optOut = (_b = options.optOut) !== null && _b !== void 0 ? _b : defaultConfig.optOut; | ||
this.saveEvents = (_c = options.saveEvents) !== null && _c !== void 0 ? _c : defaultConfig.saveEvents; | ||
this.serverUrl = options.serverUrl; | ||
@@ -41,3 +39,3 @@ this.serverZone = options.serverZone || defaultConfig.serverZone; | ||
this.transportProvider = options.transportProvider; | ||
this.useBatch = (_d = options.useBatch) !== null && _d !== void 0 ? _d : defaultConfig.useBatch; | ||
this.useBatch = (_c = options.useBatch) !== null && _c !== void 0 ? _c : defaultConfig.useBatch; | ||
this.loggerProvider.enable(this.logLevel); | ||
@@ -44,0 +42,0 @@ var serverConfig = (0, exports.createServerConfig)(options.serverUrl, options.serverZone, options.useBatch); |
@@ -8,3 +8,3 @@ import { CoreClient, Config, Event, BaseEvent, EventOptions, Identify, Plugin, Revenue } from '@amplitude/analytics-types'; | ||
constructor(name?: string); | ||
init(_apiKey: string | undefined, _userId: string | undefined, config: T): Promise<void>; | ||
_init(config: T): Promise<void>; | ||
track(eventInput: BaseEvent | string, eventProperties?: Record<string, any>, eventOptions?: EventOptions): Promise<import("@amplitude/analytics-types").Result>; | ||
@@ -11,0 +11,0 @@ logEvent: (eventInput: BaseEvent | string, eventProperties?: Record<string, any> | undefined, eventOptions?: EventOptions | undefined) => Promise<import("@amplitude/analytics-types").Result>; |
@@ -7,2 +7,3 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
var result_builder_1 = require("./utils/result-builder"); | ||
var messages_1 = require("./messages"); | ||
var AmplitudeCore = /** @class */ (function () { | ||
@@ -12,8 +13,8 @@ function AmplitudeCore(name) { | ||
this.logEvent = this.track.bind(this); | ||
this.timeline = new timeline_1.Timeline(); | ||
this.name = name; | ||
} | ||
// NOTE: Do not use `_apiKey` and `_userId` here | ||
AmplitudeCore.prototype.init = function (_apiKey, _userId, config) { | ||
AmplitudeCore.prototype._init = function (config) { | ||
this.config = config; | ||
this.timeline = new timeline_1.Timeline(); | ||
this.timeline.reset(); | ||
return Promise.resolve(); | ||
@@ -58,11 +59,16 @@ }; | ||
AmplitudeCore.prototype.dispatch = function (event) { | ||
var _a; | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
var result, e_1, message; | ||
return (0, tslib_1.__generator)(this, function (_a) { | ||
switch (_a.label) { | ||
return (0, tslib_1.__generator)(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, this.timeline.push(event, this.config)]; | ||
_b.trys.push([0, 2, , 3]); | ||
// skip event processing if opt out | ||
if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.optOut) { | ||
return [2 /*return*/, (0, result_builder_1.buildResult)(event, 0, messages_1.OPT_OUT_MESSAGE)]; | ||
} | ||
return [4 /*yield*/, this.timeline.push(event)]; | ||
case 1: | ||
result = _a.sent(); | ||
result = _b.sent(); | ||
if (result.code === 200) { | ||
@@ -76,3 +82,3 @@ this.config.loggerProvider.log(result.message); | ||
case 2: | ||
e_1 = _a.sent(); | ||
e_1 = _b.sent(); | ||
message = String(e_1); | ||
@@ -79,0 +85,0 @@ this.config.loggerProvider.error(message); |
@@ -6,2 +6,3 @@ export declare const SUCCESS_MESSAGE = "Event tracked successfully"; | ||
export declare const MISSING_API_KEY_MESSAGE = "Event rejected due to missing API key"; | ||
export declare const INVALID_API_KEY = "Invalid API key"; | ||
//# sourceMappingURL=messages.d.ts.map |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MISSING_API_KEY_MESSAGE = exports.OPT_OUT_MESSAGE = exports.MAX_RETRIES_EXCEEDED_MESSAGE = exports.UNEXPECTED_ERROR_MESSAGE = exports.SUCCESS_MESSAGE = void 0; | ||
exports.INVALID_API_KEY = exports.MISSING_API_KEY_MESSAGE = exports.OPT_OUT_MESSAGE = exports.MAX_RETRIES_EXCEEDED_MESSAGE = exports.UNEXPECTED_ERROR_MESSAGE = exports.SUCCESS_MESSAGE = void 0; | ||
exports.SUCCESS_MESSAGE = 'Event tracked successfully'; | ||
@@ -8,2 +8,3 @@ exports.UNEXPECTED_ERROR_MESSAGE = 'Unexpected error occurred'; | ||
exports.MISSING_API_KEY_MESSAGE = 'Event rejected due to missing API key'; | ||
exports.INVALID_API_KEY = 'Invalid API key'; | ||
//# sourceMappingURL=messages.js.map |
@@ -21,13 +21,14 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
Destination.prototype.setup = function (config) { | ||
var _a; | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
var unsent; | ||
var _this = this; | ||
return (0, tslib_1.__generator)(this, function (_a) { | ||
switch (_a.label) { | ||
return (0, tslib_1.__generator)(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
this.config = config; | ||
this.storageKey = "".concat(constants_1.STORAGE_PREFIX, "_").concat(this.config.apiKey.substring(0, 10)); | ||
return [4 /*yield*/, this.config.storageProvider.get(this.storageKey)]; | ||
return [4 /*yield*/, ((_a = this.config.storageProvider) === null || _a === void 0 ? void 0 : _a.get(this.storageKey))]; | ||
case 1: | ||
unsent = _a.sent(); | ||
unsent = _b.sent(); | ||
this.saveEvents(); // sets storage to '[]' | ||
@@ -65,3 +66,3 @@ if (unsent && unsent.length > 0) { | ||
} | ||
void _this.fulfillRequest([context], 500, analytics_types_1.Status.Unknown); | ||
void _this.fulfillRequest([context], 500, messages_1.MAX_RETRIES_EXCEEDED_MESSAGE); | ||
return false; | ||
@@ -184,3 +185,3 @@ }); | ||
var _this = this; | ||
if (res.body.missingField) { | ||
if (res.body.missingField || res.body.error.startsWith(messages_1.INVALID_API_KEY)) { | ||
this.fulfillRequest(list, res.statusCode, res.body.error); | ||
@@ -247,3 +248,3 @@ return; | ||
Destination.prototype.saveEvents = function () { | ||
if (!this.config.saveEvents) { | ||
if (!this.config.storageProvider) { | ||
return; | ||
@@ -250,0 +251,0 @@ } |
@@ -5,7 +5,8 @@ import { Config, Event, EventCallback, Plugin, Result } from '@amplitude/analytics-types'; | ||
applying: boolean; | ||
flushing: boolean; | ||
isReady: boolean; | ||
plugins: Plugin[]; | ||
register(plugin: Plugin, config: Config): Promise<void>; | ||
deregister(pluginName: string): Promise<void>; | ||
push(event: Event, config: Config): Promise<Result>; | ||
reset(): void; | ||
push(event: Event): Promise<Result>; | ||
scheduleApply(timeout: number): void; | ||
@@ -12,0 +13,0 @@ apply(item: [Event, EventCallback] | undefined): Promise<void>; |
@@ -5,3 +5,2 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
var analytics_types_1 = require("@amplitude/analytics-types"); | ||
var messages_1 = require("./messages"); | ||
var result_builder_1 = require("./utils/result-builder"); | ||
@@ -11,4 +10,7 @@ var Timeline = /** @class */ (function () { | ||
this.queue = []; | ||
// Flag to guarantee one schedule apply is running | ||
this.applying = false; | ||
this.flushing = false; | ||
// Flag indicates whether timeline is ready to process event | ||
// Events collected before timeline is ready will stay in the queue to be processed later | ||
this.isReady = false; | ||
this.plugins = []; | ||
@@ -33,9 +35,10 @@ } | ||
}; | ||
Timeline.prototype.push = function (event, config) { | ||
Timeline.prototype.reset = function () { | ||
this.applying = false; | ||
this.isReady = false; | ||
this.plugins = []; | ||
}; | ||
Timeline.prototype.push = function (event) { | ||
var _this = this; | ||
return new Promise(function (resolve) { | ||
if (config.optOut) { | ||
resolve((0, result_builder_1.buildResult)(event, 0, messages_1.OPT_OUT_MESSAGE)); | ||
return; | ||
} | ||
_this.queue.push([event, resolve]); | ||
@@ -47,3 +50,3 @@ _this.scheduleApply(0); | ||
var _this = this; | ||
if (this.applying) | ||
if (this.applying && this.isReady) | ||
return; | ||
@@ -50,0 +53,0 @@ this.applying = true; |
@@ -9,3 +9,2 @@ import { Event, Config as IConfig, Logger as ILogger, InitOptions, LogLevel, Storage, Transport, Plan, ServerZone } from '@amplitude/analytics-types'; | ||
loggerProvider: Logger; | ||
saveEvents: boolean; | ||
optOut: boolean; | ||
@@ -25,7 +24,6 @@ serverUrl: string; | ||
plan?: Plan; | ||
saveEvents: boolean; | ||
serverUrl: string | undefined; | ||
serverZone?: ServerZone; | ||
transportProvider: Transport; | ||
storageProvider: Storage<Event[]>; | ||
storageProvider?: Storage<Event[]>; | ||
useBatch: boolean; | ||
@@ -32,0 +30,0 @@ private _optOut; |
@@ -5,8 +5,7 @@ import { LogLevel, ServerZone, } from '@amplitude/analytics-types'; | ||
export var getDefaultConfig = function () { return ({ | ||
flushMaxRetries: 5, | ||
flushQueueSize: 10, | ||
flushIntervalMillis: 1000, | ||
flushMaxRetries: 12, | ||
flushQueueSize: 200, | ||
flushIntervalMillis: 10000, | ||
logLevel: LogLevel.Warn, | ||
loggerProvider: new Logger(), | ||
saveEvents: true, | ||
optOut: false, | ||
@@ -19,3 +18,3 @@ serverUrl: AMPLITUDE_SERVER_URL, | ||
function Config(options) { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c; | ||
this._optOut = false; | ||
@@ -32,3 +31,2 @@ var defaultConfig = getDefaultConfig(); | ||
this.optOut = (_b = options.optOut) !== null && _b !== void 0 ? _b : defaultConfig.optOut; | ||
this.saveEvents = (_c = options.saveEvents) !== null && _c !== void 0 ? _c : defaultConfig.saveEvents; | ||
this.serverUrl = options.serverUrl; | ||
@@ -38,3 +36,3 @@ this.serverZone = options.serverZone || defaultConfig.serverZone; | ||
this.transportProvider = options.transportProvider; | ||
this.useBatch = (_d = options.useBatch) !== null && _d !== void 0 ? _d : defaultConfig.useBatch; | ||
this.useBatch = (_c = options.useBatch) !== null && _c !== void 0 ? _c : defaultConfig.useBatch; | ||
this.loggerProvider.enable(this.logLevel); | ||
@@ -41,0 +39,0 @@ var serverConfig = createServerConfig(options.serverUrl, options.serverZone, options.useBatch); |
@@ -8,3 +8,3 @@ import { CoreClient, Config, Event, BaseEvent, EventOptions, Identify, Plugin, Revenue } from '@amplitude/analytics-types'; | ||
constructor(name?: string); | ||
init(_apiKey: string | undefined, _userId: string | undefined, config: T): Promise<void>; | ||
_init(config: T): Promise<void>; | ||
track(eventInput: BaseEvent | string, eventProperties?: Record<string, any>, eventOptions?: EventOptions): Promise<import("@amplitude/analytics-types").Result>; | ||
@@ -11,0 +11,0 @@ logEvent: (eventInput: BaseEvent | string, eventProperties?: Record<string, any> | undefined, eventOptions?: EventOptions | undefined) => Promise<import("@amplitude/analytics-types").Result>; |
@@ -5,2 +5,3 @@ import { __awaiter, __generator } from "tslib"; | ||
import { buildResult } from './utils/result-builder'; | ||
import { OPT_OUT_MESSAGE } from './messages'; | ||
var AmplitudeCore = /** @class */ (function () { | ||
@@ -10,8 +11,8 @@ function AmplitudeCore(name) { | ||
this.logEvent = this.track.bind(this); | ||
this.timeline = new Timeline(); | ||
this.name = name; | ||
} | ||
// NOTE: Do not use `_apiKey` and `_userId` here | ||
AmplitudeCore.prototype.init = function (_apiKey, _userId, config) { | ||
AmplitudeCore.prototype._init = function (config) { | ||
this.config = config; | ||
this.timeline = new Timeline(); | ||
this.timeline.reset(); | ||
return Promise.resolve(); | ||
@@ -56,11 +57,16 @@ }; | ||
AmplitudeCore.prototype.dispatch = function (event) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result, e_1, message; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, this.timeline.push(event, this.config)]; | ||
_b.trys.push([0, 2, , 3]); | ||
// skip event processing if opt out | ||
if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.optOut) { | ||
return [2 /*return*/, buildResult(event, 0, OPT_OUT_MESSAGE)]; | ||
} | ||
return [4 /*yield*/, this.timeline.push(event)]; | ||
case 1: | ||
result = _a.sent(); | ||
result = _b.sent(); | ||
if (result.code === 200) { | ||
@@ -74,3 +80,3 @@ this.config.loggerProvider.log(result.message); | ||
case 2: | ||
e_1 = _a.sent(); | ||
e_1 = _b.sent(); | ||
message = String(e_1); | ||
@@ -77,0 +83,0 @@ this.config.loggerProvider.error(message); |
@@ -6,2 +6,3 @@ export declare const SUCCESS_MESSAGE = "Event tracked successfully"; | ||
export declare const MISSING_API_KEY_MESSAGE = "Event rejected due to missing API key"; | ||
export declare const INVALID_API_KEY = "Invalid API key"; | ||
//# sourceMappingURL=messages.d.ts.map |
@@ -6,2 +6,3 @@ export var SUCCESS_MESSAGE = 'Event tracked successfully'; | ||
export var MISSING_API_KEY_MESSAGE = 'Event rejected due to missing API key'; | ||
export var INVALID_API_KEY = 'Invalid API key'; | ||
//# sourceMappingURL=messages.js.map |
import { __awaiter, __generator, __read, __spreadArray } from "tslib"; | ||
import { PluginType, Status, } from '@amplitude/analytics-types'; | ||
import { MISSING_API_KEY_MESSAGE, SUCCESS_MESSAGE, UNEXPECTED_ERROR_MESSAGE } from '../messages'; | ||
import { INVALID_API_KEY, MAX_RETRIES_EXCEEDED_MESSAGE, MISSING_API_KEY_MESSAGE, SUCCESS_MESSAGE, UNEXPECTED_ERROR_MESSAGE, } from '../messages'; | ||
import { STORAGE_PREFIX } from '../constants'; | ||
@@ -19,13 +19,14 @@ import { chunk } from '../utils/chunk'; | ||
Destination.prototype.setup = function (config) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var unsent; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
this.config = config; | ||
this.storageKey = "".concat(STORAGE_PREFIX, "_").concat(this.config.apiKey.substring(0, 10)); | ||
return [4 /*yield*/, this.config.storageProvider.get(this.storageKey)]; | ||
return [4 /*yield*/, ((_a = this.config.storageProvider) === null || _a === void 0 ? void 0 : _a.get(this.storageKey))]; | ||
case 1: | ||
unsent = _a.sent(); | ||
unsent = _b.sent(); | ||
this.saveEvents(); // sets storage to '[]' | ||
@@ -63,3 +64,3 @@ if (unsent && unsent.length > 0) { | ||
} | ||
void _this.fulfillRequest([context], 500, Status.Unknown); | ||
void _this.fulfillRequest([context], 500, MAX_RETRIES_EXCEEDED_MESSAGE); | ||
return false; | ||
@@ -182,3 +183,3 @@ }); | ||
var _this = this; | ||
if (res.body.missingField) { | ||
if (res.body.missingField || res.body.error.startsWith(INVALID_API_KEY)) { | ||
this.fulfillRequest(list, res.statusCode, res.body.error); | ||
@@ -245,3 +246,3 @@ return; | ||
Destination.prototype.saveEvents = function () { | ||
if (!this.config.saveEvents) { | ||
if (!this.config.storageProvider) { | ||
return; | ||
@@ -248,0 +249,0 @@ } |
@@ -5,7 +5,8 @@ import { Config, Event, EventCallback, Plugin, Result } from '@amplitude/analytics-types'; | ||
applying: boolean; | ||
flushing: boolean; | ||
isReady: boolean; | ||
plugins: Plugin[]; | ||
register(plugin: Plugin, config: Config): Promise<void>; | ||
deregister(pluginName: string): Promise<void>; | ||
push(event: Event, config: Config): Promise<Result>; | ||
reset(): void; | ||
push(event: Event): Promise<Result>; | ||
scheduleApply(timeout: number): void; | ||
@@ -12,0 +13,0 @@ apply(item: [Event, EventCallback] | undefined): Promise<void>; |
import { __assign, __awaiter, __generator, __read, __values } from "tslib"; | ||
import { PluginType, } from '@amplitude/analytics-types'; | ||
import { OPT_OUT_MESSAGE } from './messages'; | ||
import { buildResult } from './utils/result-builder'; | ||
@@ -8,4 +7,7 @@ var Timeline = /** @class */ (function () { | ||
this.queue = []; | ||
// Flag to guarantee one schedule apply is running | ||
this.applying = false; | ||
this.flushing = false; | ||
// Flag indicates whether timeline is ready to process event | ||
// Events collected before timeline is ready will stay in the queue to be processed later | ||
this.isReady = false; | ||
this.plugins = []; | ||
@@ -30,9 +32,10 @@ } | ||
}; | ||
Timeline.prototype.push = function (event, config) { | ||
Timeline.prototype.reset = function () { | ||
this.applying = false; | ||
this.isReady = false; | ||
this.plugins = []; | ||
}; | ||
Timeline.prototype.push = function (event) { | ||
var _this = this; | ||
return new Promise(function (resolve) { | ||
if (config.optOut) { | ||
resolve(buildResult(event, 0, OPT_OUT_MESSAGE)); | ||
return; | ||
} | ||
_this.queue.push([event, resolve]); | ||
@@ -44,3 +47,3 @@ _this.scheduleApply(0); | ||
var _this = this; | ||
if (this.applying) | ||
if (this.applying && this.isReady) | ||
return; | ||
@@ -47,0 +50,0 @@ this.applying = true; |
{ | ||
"name": "@amplitude/analytics-core", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"description": "", | ||
@@ -22,2 +22,3 @@ "author": "Amplitude Inc", | ||
"build:esm": "tsc -p ./tsconfig.esm.json", | ||
"clean": "rimraf node_modules lib coverage", | ||
"fix": "yarn fix:eslint & yarn fix:prettier", | ||
@@ -36,3 +37,3 @@ "fix:eslint": "eslint '{src,test}/**/*.ts' --fix", | ||
"dependencies": { | ||
"@amplitude/analytics-types": "^0.6.0", | ||
"@amplitude/analytics-types": "^0.6.1", | ||
"tslib": "^2.3.1" | ||
@@ -43,3 +44,3 @@ }, | ||
], | ||
"gitHead": "ccece5d22caa0ae00b64cece3165ebc0a81dfc93" | ||
"gitHead": "321002ef9a539959373204c3d7b15322b0936752" | ||
} |
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
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
311649
3016
0