@amplitude/analytics-core 2.5.4 → 2.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/lib/cjs/__mocks__/logger.js +1 -0
  2. package/lib/cjs/__mocks__/logger.js.map +1 -1
  3. package/lib/cjs/config.js +1 -0
  4. package/lib/cjs/config.js.map +1 -1
  5. package/lib/cjs/constants.js +1 -0
  6. package/lib/cjs/constants.js.map +1 -1
  7. package/lib/cjs/core-client.js +2 -1
  8. package/lib/cjs/core-client.js.map +1 -1
  9. package/lib/cjs/event-bridge/event-bridge-channel.js +1 -0
  10. package/lib/cjs/event-bridge/event-bridge-channel.js.map +1 -1
  11. package/lib/cjs/event-bridge/event-bridge-container.js +1 -0
  12. package/lib/cjs/event-bridge/event-bridge-container.js.map +1 -1
  13. package/lib/cjs/event-bridge/event-bridge.js +1 -0
  14. package/lib/cjs/event-bridge/event-bridge.js.map +1 -1
  15. package/lib/cjs/identify.js +1 -0
  16. package/lib/cjs/identify.js.map +1 -1
  17. package/lib/cjs/index.js +1 -0
  18. package/lib/cjs/index.js.map +1 -1
  19. package/lib/cjs/logger.js +1 -0
  20. package/lib/cjs/logger.js.map +1 -1
  21. package/lib/cjs/messages.js +1 -0
  22. package/lib/cjs/messages.js.map +1 -1
  23. package/lib/cjs/plugins/destination.d.ts +6 -4
  24. package/lib/cjs/plugins/destination.d.ts.map +1 -1
  25. package/lib/cjs/plugins/destination.js +77 -49
  26. package/lib/cjs/plugins/destination.js.map +1 -1
  27. package/lib/cjs/revenue.d.ts +2 -0
  28. package/lib/cjs/revenue.d.ts.map +1 -1
  29. package/lib/cjs/revenue.js +6 -0
  30. package/lib/cjs/revenue.js.map +1 -1
  31. package/lib/cjs/storage/memory.js +1 -0
  32. package/lib/cjs/storage/memory.js.map +1 -1
  33. package/lib/cjs/timeline.d.ts +1 -1
  34. package/lib/cjs/timeline.d.ts.map +1 -1
  35. package/lib/cjs/timeline.js +6 -1
  36. package/lib/cjs/timeline.js.map +1 -1
  37. package/lib/cjs/transports/base.js +1 -0
  38. package/lib/cjs/transports/base.js.map +1 -1
  39. package/lib/cjs/utils/chunk.js +1 -0
  40. package/lib/cjs/utils/chunk.js.map +1 -1
  41. package/lib/cjs/utils/debug.js +1 -0
  42. package/lib/cjs/utils/debug.js.map +1 -1
  43. package/lib/cjs/utils/event-builder.js +1 -0
  44. package/lib/cjs/utils/event-builder.js.map +1 -1
  45. package/lib/cjs/utils/result-builder.js +1 -0
  46. package/lib/cjs/utils/result-builder.js.map +1 -1
  47. package/lib/cjs/utils/return-wrapper.js +1 -0
  48. package/lib/cjs/utils/return-wrapper.js.map +1 -1
  49. package/lib/cjs/utils/uuid.js +1 -0
  50. package/lib/cjs/utils/uuid.js.map +1 -1
  51. package/lib/cjs/utils/valid-properties.js +1 -0
  52. package/lib/cjs/utils/valid-properties.js.map +1 -1
  53. package/lib/esm/core-client.js +1 -1
  54. package/lib/esm/core-client.js.map +1 -1
  55. package/lib/esm/plugins/destination.d.ts +6 -4
  56. package/lib/esm/plugins/destination.d.ts.map +1 -1
  57. package/lib/esm/plugins/destination.js +76 -49
  58. package/lib/esm/plugins/destination.js.map +1 -1
  59. package/lib/esm/revenue.d.ts +2 -0
  60. package/lib/esm/revenue.d.ts.map +1 -1
  61. package/lib/esm/revenue.js +5 -0
  62. package/lib/esm/revenue.js.map +1 -1
  63. package/lib/esm/timeline.d.ts +1 -1
  64. package/lib/esm/timeline.d.ts.map +1 -1
  65. package/lib/esm/timeline.js +5 -1
  66. package/lib/esm/timeline.js.map +1 -1
  67. package/package.json +3 -3
@@ -30,7 +30,16 @@ var Destination = /** @class */ (function () {
30
30
  this.retryTimeout = 1000;
31
31
  this.throttleTimeout = 30000;
32
32
  this.storageKey = '';
33
- this.scheduled = null;
33
+ // Indicator of whether events that are scheduled (but not flushed yet).
34
+ // When flush:
35
+ // 1. assign `scheduleId` to `flushId`
36
+ // 2. set `scheduleId` to null
37
+ this.scheduleId = null;
38
+ // Timeout in milliseconds of current schedule
39
+ this.scheduledTimeout = 0;
40
+ // Indicator of whether current flush resolves.
41
+ // When flush resolves, set `flushId` to null
42
+ this.flushId = null;
34
43
  this.queue = [];
35
44
  }
36
45
  Destination.prototype.setup = function (config) {
@@ -67,60 +76,58 @@ var Destination = /** @class */ (function () {
67
76
  callback: function (result) { return resolve(result); },
68
77
  timeout: 0,
69
78
  };
70
- void _this.addToQueue(context);
79
+ _this.queue.push(context);
80
+ _this.schedule(_this.config.flushIntervalMillis);
81
+ _this.saveEvents();
71
82
  });
72
83
  };
73
- Destination.prototype.getTryableList = function (list) {
84
+ Destination.prototype.removeEventsExceedFlushMaxRetries = function (list) {
74
85
  var _this = this;
75
86
  return list.filter(function (context) {
87
+ context.attempts += 1;
76
88
  if (context.attempts < _this.config.flushMaxRetries) {
77
- context.attempts += 1;
78
89
  return true;
79
90
  }
80
91
  void _this.fulfillRequest([context], 500, MAX_RETRIES_EXCEEDED_MESSAGE);
81
92
  return false;
82
93
  });
83
94
  };
84
- Destination.prototype.scheduleTryable = function (list, shouldAddToQueue) {
95
+ Destination.prototype.scheduleEvents = function (list) {
85
96
  var _this = this;
86
- if (shouldAddToQueue === void 0) { shouldAddToQueue = false; }
87
97
  list.forEach(function (context) {
88
- // Only need to concat the queue for the first time
89
- if (shouldAddToQueue) {
90
- _this.queue = _this.queue.concat(context);
91
- }
92
- if (context.timeout === 0) {
93
- _this.schedule(_this.config.flushIntervalMillis);
94
- return;
95
- }
96
- setTimeout(function () {
97
- context.timeout = 0;
98
- _this.schedule(0);
99
- }, context.timeout);
98
+ _this.schedule(context.timeout === 0 ? _this.config.flushIntervalMillis : context.timeout);
100
99
  });
101
100
  };
102
- Destination.prototype.addToQueue = function () {
103
- var list = [];
104
- for (var _i = 0; _i < arguments.length; _i++) {
105
- list[_i] = arguments[_i];
106
- }
107
- var tryable = this.getTryableList(list);
108
- this.scheduleTryable(tryable, true);
109
- this.saveEvents();
110
- };
101
+ // Schedule a flush in timeout when
102
+ // 1. No schedule
103
+ // 2. Timeout greater than existing timeout.
104
+ // This makes sure that when throttled, no flush when throttle timeout expires.
111
105
  Destination.prototype.schedule = function (timeout) {
112
106
  var _this = this;
113
- if (this.scheduled || this.config.offline) {
107
+ if (this.config.offline) {
114
108
  return;
115
109
  }
116
- this.scheduled = setTimeout(function () {
117
- void _this.flush(true).then(function () {
118
- if (_this.queue.length > 0) {
119
- _this.schedule(timeout);
120
- }
121
- });
122
- }, timeout);
110
+ if (this.scheduleId === null || (this.scheduleId && timeout > this.scheduledTimeout)) {
111
+ if (this.scheduleId) {
112
+ clearTimeout(this.scheduleId);
113
+ }
114
+ this.scheduledTimeout = timeout;
115
+ this.scheduleId = setTimeout(function () {
116
+ _this.queue = _this.queue.map(function (context) {
117
+ context.timeout = 0;
118
+ return context;
119
+ });
120
+ void _this.flush(true);
121
+ }, timeout);
122
+ return;
123
+ }
124
+ };
125
+ // Mark current schedule is flushed.
126
+ Destination.prototype.resetSchedule = function () {
127
+ this.scheduleId = null;
128
+ this.scheduledTimeout = 0;
123
129
  };
130
+ // Flush all events regardless of their timeout
124
131
  Destination.prototype.flush = function (useRetry) {
125
132
  if (useRetry === void 0) { useRetry = false; }
126
133
  return __awaiter(this, void 0, void 0, function () {
@@ -131,21 +138,41 @@ var Destination = /** @class */ (function () {
131
138
  case 0:
132
139
  // Skip flush if offline
133
140
  if (this.config.offline) {
141
+ this.resetSchedule();
134
142
  this.config.loggerProvider.debug('Skipping flush while offline.');
135
143
  return [2 /*return*/];
136
144
  }
145
+ if (this.flushId) {
146
+ this.resetSchedule();
147
+ this.config.loggerProvider.debug('Skipping flush because previous flush has not resolved.');
148
+ return [2 /*return*/];
149
+ }
150
+ this.flushId = this.scheduleId;
151
+ this.resetSchedule();
137
152
  list = [];
138
153
  later = [];
139
154
  this.queue.forEach(function (context) { return (context.timeout === 0 ? list.push(context) : later.push(context)); });
140
- if (this.scheduled) {
141
- clearTimeout(this.scheduled);
142
- this.scheduled = null;
143
- }
144
155
  batches = chunk(list, this.config.flushQueueSize);
145
- return [4 /*yield*/, Promise.all(batches.map(function (batch) { return _this.send(batch, useRetry); }))];
156
+ // Promise.all() doesn't guarantee resolve order.
157
+ // Sequentially resolve to make sure backend receives events in order
158
+ return [4 /*yield*/, batches.reduce(function (promise, batch) { return __awaiter(_this, void 0, void 0, function () {
159
+ return __generator(this, function (_a) {
160
+ switch (_a.label) {
161
+ case 0: return [4 /*yield*/, promise];
162
+ case 1:
163
+ _a.sent();
164
+ return [4 /*yield*/, this.send(batch, useRetry)];
165
+ case 2: return [2 /*return*/, _a.sent()];
166
+ }
167
+ });
168
+ }); }, Promise.resolve())];
146
169
  case 1:
170
+ // Promise.all() doesn't guarantee resolve order.
171
+ // Sequentially resolve to make sure backend receives events in order
147
172
  _a.sent();
148
- this.scheduleTryable(later);
173
+ // Mark current flush is done
174
+ this.flushId = null;
175
+ this.scheduleEvents(this.queue);
149
176
  return [2 /*return*/];
150
177
  }
151
178
  });
@@ -257,8 +284,8 @@ var Destination = /** @class */ (function () {
257
284
  // log intermediate event status before retry
258
285
  this.config.loggerProvider.warn(getResponseBodyString(res));
259
286
  }
260
- var tryable = this.getTryableList(retry);
261
- this.scheduleTryable(tryable);
287
+ var tryable = this.removeEventsExceedFlushMaxRetries(retry);
288
+ this.scheduleEvents(tryable);
262
289
  };
263
290
  Destination.prototype.handlePayloadTooLargeResponse = function (res, list) {
264
291
  if (list.length === 1) {
@@ -268,8 +295,8 @@ var Destination = /** @class */ (function () {
268
295
  // log intermediate event status before retry
269
296
  this.config.loggerProvider.warn(getResponseBodyString(res));
270
297
  this.config.flushQueueSize /= 2;
271
- var tryable = this.getTryableList(list);
272
- this.scheduleTryable(tryable);
298
+ var tryable = this.removeEventsExceedFlushMaxRetries(list);
299
+ this.scheduleEvents(tryable);
273
300
  };
274
301
  Destination.prototype.handleRateLimitResponse = function (res, list) {
275
302
  var _this = this;
@@ -294,8 +321,8 @@ var Destination = /** @class */ (function () {
294
321
  // log intermediate event status before retry
295
322
  this.config.loggerProvider.warn(getResponseBodyString(res));
296
323
  }
297
- var tryable = this.getTryableList(retry);
298
- this.scheduleTryable(tryable);
324
+ var tryable = this.removeEventsExceedFlushMaxRetries(retry);
325
+ this.scheduleEvents(tryable);
299
326
  };
300
327
  Destination.prototype.handleOtherResponse = function (list) {
301
328
  var _this = this;
@@ -303,8 +330,8 @@ var Destination = /** @class */ (function () {
303
330
  context.timeout = context.attempts * _this.retryTimeout;
304
331
  return context;
305
332
  });
306
- var tryable = this.getTryableList(later);
307
- this.scheduleTryable(tryable);
333
+ var tryable = this.removeEventsExceedFlushMaxRetries(later);
334
+ this.scheduleEvents(tryable);
308
335
  };
309
336
  Destination.prototype.fulfillRequest = function (list, code, message) {
310
337
  this.removeEvents(list);
@@ -1 +1 @@
1
- {"version":3,"file":"destination.js","sourceRoot":"","sources":["../../../src/plugins/destination.ts"],"names":[],"mappings":";AAAA,OAAO,EAUL,MAAM,GAEP,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,eAAe,EACf,4BAA4B,EAC5B,uBAAuB,EACvB,eAAe,EACf,wBAAwB,GACzB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAChE,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAErC,SAAS,eAAe,CAAC,KAAc;IACrC,IAAI,KAAK,YAAY,KAAK;QAAE,OAAO,KAAK,CAAC,OAAO,CAAC;IACjD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,GAAa;IACjD,IAAI,kBAAkB,GAAG,EAAE,CAAC;IAC5B,IAAI;Q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IAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,UAAC,OAAO,IAAK,OAAA,OAAO,CAAC,KAAK,EAAb,CAAa,CAAC,CAAC;QACjE,KAAK,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;IACvE,CAAC;IAED;;OAEG;IACH,kCAAY,GAAZ,UAAa,cAAyB;QACpC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAC5B,UAAC,aAAa,IAAK,OAAA,CAAC,cAAc,CAAC,IAAI,CAAC,UAAC,OAAO,IAAK,OAAA,OAAO,CAAC,KAAK,CAAC,SAAS,KAAK,aAAa,CAAC,KAAK,CAAC,SAAS,EAAzD,CAAyD,CAAC,EAA5F,CAA4F,CAChH,CAAC;QAEF,IAAI,CAAC,UAAU,EAAE,CAAC;IACpB,CAAC;IACH,kBAAC;AAAD,CAAC,AArTD,IAqTC","sourcesContent":["import {\n Config,\n DestinationContext as Context,\n DestinationPlugin,\n Event,\n InvalidResponse,\n PayloadTooLargeResponse,\n RateLimitResponse,\n Response,\n Result,\n Status,\n SuccessResponse,\n} from '@amplitude/analytics-types';\nimport {\n INVALID_API_KEY,\n MAX_RETRIES_EXCEEDED_MESSAGE,\n MISSING_API_KEY_MESSAGE,\n SUCCESS_MESSAGE,\n UNEXPECTED_ERROR_MESSAGE,\n} from '../messages';\nimport { STORAGE_PREFIX } from '../constants';\nimport { chunk } from '../utils/chunk';\nimport { buildResult } from '../utils/result-builder';\nimport { createServerConfig, RequestMetadata } from '../config';\nimport { UUID } from '../utils/uuid';\n\nfunction getErrorMessage(error: unknown) {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n\nexport function getResponseBodyString(res: Response) {\n let responseBodyString = '';\n try {\n if ('body' in res) {\n responseBodyString = JSON.stringify(res.body, null, 2);\n }\n } catch {\n // to avoid crash, but don't care about the error, add comment to avoid empty block lint error\n }\n return responseBodyString;\n}\n\nexport class Destination implements DestinationPlugin {\n name = 'amplitude';\n type = 'destination' as const;\n\n retryTimeout = 1000;\n throttleTimeout = 30000;\n storageKey = '';\n // this.config is defined in setup() which will always be called first\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n config: Config;\n private scheduled: ReturnType<typeof setTimeout> | null = null;\n queue: Context[] = [];\n\n async setup(config: Config): Promise<undefined> {\n this.config = config;\n\n this.storageKey = `${STORAGE_PREFIX}_${this.config.apiKey.substring(0, 10)}`;\n const unsent = await this.config.storageProvider?.get(this.storageKey);\n if (unsent && unsent.length > 0) {\n void Promise.all(unsent.map((event) => this.execute(event))).catch();\n }\n\n return Promise.resolve(undefined);\n }\n\n execute(event: Event): Promise<Result> {\n // Assign insert_id for dropping invalid event later\n if (!event.insert_id) {\n event.insert_id = UUID();\n }\n\n return new Promise((resolve) => {\n const context = {\n event,\n attempts: 0,\n callback: (result: Result) => resolve(result),\n timeout: 0,\n };\n void this.addToQueue(context);\n });\n }\n\n getTryableList(list: Context[]) {\n return list.filter((context) => {\n if (context.attempts < this.config.flushMaxRetries) {\n context.attempts += 1;\n return true;\n }\n void this.fulfillRequest([context], 500, MAX_RETRIES_EXCEEDED_MESSAGE);\n return false;\n });\n }\n\n scheduleTryable(list: Context[], shouldAddToQueue = false) {\n list.forEach((context) => {\n // Only need to concat the queue for the first time\n if (shouldAddToQueue) {\n this.queue = this.queue.concat(context);\n }\n if (context.timeout === 0) {\n this.schedule(this.config.flushIntervalMillis);\n return;\n }\n\n setTimeout(() => {\n context.timeout = 0;\n this.schedule(0);\n }, context.timeout);\n });\n }\n\n addToQueue(...list: Context[]) {\n const tryable = this.getTryableList(list);\n this.scheduleTryable(tryable, true);\n this.saveEvents();\n }\n\n schedule(timeout: number) {\n if (this.scheduled || this.config.offline) {\n return;\n }\n\n this.scheduled = setTimeout(() => {\n void this.flush(true).then(() => {\n if (this.queue.length > 0) {\n this.schedule(timeout);\n }\n });\n }, timeout);\n }\n\n async flush(useRetry = false) {\n // Skip flush if offline\n if (this.config.offline) {\n this.config.loggerProvider.debug('Skipping flush while offline.');\n return;\n }\n\n const list: Context[] = [];\n const later: Context[] = [];\n this.queue.forEach((context) => (context.timeout === 0 ? list.push(context) : later.push(context)));\n\n if (this.scheduled) {\n clearTimeout(this.scheduled);\n this.scheduled = null;\n }\n\n const batches = chunk(list, this.config.flushQueueSize);\n\n await Promise.all(batches.map((batch) => this.send(batch, useRetry)));\n\n this.scheduleTryable(later);\n }\n\n async send(list: Context[], useRetry = true) {\n if (!this.config.apiKey) {\n return this.fulfillRequest(list, 400, MISSING_API_KEY_MESSAGE);\n }\n\n const payload = {\n api_key: this.config.apiKey,\n events: list.map((context) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { extra, ...eventWithoutExtra } = context.event;\n return eventWithoutExtra;\n }),\n options: {\n min_id_length: this.config.minIdLength,\n },\n client_upload_time: new Date().toISOString(),\n request_metadata: this.config.requestMetadata,\n };\n this.config.requestMetadata = new RequestMetadata();\n\n try {\n const { serverUrl } = createServerConfig(this.config.serverUrl, this.config.serverZone, this.config.useBatch);\n const res = await this.config.transportProvider.send(serverUrl, payload);\n if (res === null) {\n this.fulfillRequest(list, 0, UNEXPECTED_ERROR_MESSAGE);\n return;\n }\n if (!useRetry) {\n if ('body' in res) {\n this.fulfillRequest(list, res.statusCode, `${res.status}: ${getResponseBodyString(res)}`);\n } else {\n this.fulfillRequest(list, res.statusCode, res.status);\n }\n return;\n }\n this.handleResponse(res, list);\n } catch (e) {\n const errorMessage = getErrorMessage(e);\n this.config.loggerProvider.error(errorMessage);\n this.handleResponse({ status: Status.Failed, statusCode: 0 }, list);\n }\n }\n\n handleResponse(res: Response, list: Context[]) {\n const { status } = res;\n\n switch (status) {\n case Status.Success: {\n this.handleSuccessResponse(res, list);\n break;\n }\n case Status.Invalid: {\n this.handleInvalidResponse(res, list);\n break;\n }\n case Status.PayloadTooLarge: {\n this.handlePayloadTooLargeResponse(res, list);\n break;\n }\n case Status.RateLimit: {\n this.handleRateLimitResponse(res, list);\n break;\n }\n default: {\n // log intermediate event status before retry\n this.config.loggerProvider.warn(`{code: 0, error: \"Status '${status}' provided for ${list.length} events\"}`);\n this.handleOtherResponse(list);\n break;\n }\n }\n }\n\n handleSuccessResponse(res: SuccessResponse, list: Context[]) {\n this.fulfillRequest(list, res.statusCode, SUCCESS_MESSAGE);\n }\n\n handleInvalidResponse(res: InvalidResponse, list: Context[]) {\n if (res.body.missingField || res.body.error.startsWith(INVALID_API_KEY)) {\n this.fulfillRequest(list, res.statusCode, res.body.error);\n return;\n }\n\n const dropIndex = [\n ...Object.values(res.body.eventsWithInvalidFields),\n ...Object.values(res.body.eventsWithMissingFields),\n ...Object.values(res.body.eventsWithInvalidIdLengths),\n ...res.body.silencedEvents,\n ].flat();\n const dropIndexSet = new Set(dropIndex);\n\n const retry = list.filter((context, index) => {\n if (dropIndexSet.has(index)) {\n this.fulfillRequest([context], res.statusCode, res.body.error);\n return;\n }\n return true;\n });\n\n if (retry.length > 0) {\n // log intermediate event status before retry\n this.config.loggerProvider.warn(getResponseBodyString(res));\n }\n\n const tryable = this.getTryableList(retry);\n this.scheduleTryable(tryable);\n }\n\n handlePayloadTooLargeResponse(res: PayloadTooLargeResponse, list: Context[]) {\n if (list.length === 1) {\n this.fulfillRequest(list, res.statusCode, res.body.error);\n return;\n }\n\n // log intermediate event status before retry\n this.config.loggerProvider.warn(getResponseBodyString(res));\n\n this.config.flushQueueSize /= 2;\n\n const tryable = this.getTryableList(list);\n this.scheduleTryable(tryable);\n }\n\n handleRateLimitResponse(res: RateLimitResponse, list: Context[]) {\n const dropUserIds = Object.keys(res.body.exceededDailyQuotaUsers);\n const dropDeviceIds = Object.keys(res.body.exceededDailyQuotaDevices);\n const throttledIndex = res.body.throttledEvents;\n const dropUserIdsSet = new Set(dropUserIds);\n const dropDeviceIdsSet = new Set(dropDeviceIds);\n const throttledIndexSet = new Set(throttledIndex);\n\n const retry = list.filter((context, index) => {\n if (\n (context.event.user_id && dropUserIdsSet.has(context.event.user_id)) ||\n (context.event.device_id && dropDeviceIdsSet.has(context.event.device_id))\n ) {\n this.fulfillRequest([context], res.statusCode, res.body.error);\n return;\n }\n if (throttledIndexSet.has(index)) {\n context.timeout = this.throttleTimeout;\n }\n return true;\n });\n\n if (retry.length > 0) {\n // log intermediate event status before retry\n this.config.loggerProvider.warn(getResponseBodyString(res));\n }\n\n const tryable = this.getTryableList(retry);\n this.scheduleTryable(tryable);\n }\n\n handleOtherResponse(list: Context[]) {\n const later = list.map((context) => {\n context.timeout = context.attempts * this.retryTimeout;\n return context;\n });\n\n const tryable = this.getTryableList(later);\n this.scheduleTryable(tryable);\n }\n\n fulfillRequest(list: Context[], code: number, message: string) {\n this.removeEvents(list);\n list.forEach((context) => context.callback(buildResult(context.event, code, message)));\n }\n\n /**\n * This is called on\n * 1) new events are added to queue; or\n * 2) response comes back for a request\n *\n * Update the event storage based on the queue\n */\n saveEvents() {\n if (!this.config.storageProvider) {\n return;\n }\n\n const updatedEvents = this.queue.map((context) => context.event);\n void this.config.storageProvider.set(this.storageKey, updatedEvents);\n }\n\n /**\n * This is called on response comes back for a request\n */\n removeEvents(eventsToRemove: Context[]) {\n this.queue = this.queue.filter(\n (queuedContext) => !eventsToRemove.some((context) => context.event.insert_id === queuedContext.event.insert_id),\n );\n\n this.saveEvents();\n }\n}\n"]}
1
+ {"version":3,"file":"destination.js","sourceRoot":"","sources":["../../../src/plugins/destination.ts"],"names":[],"mappings":";AAAA,OAAO,EAUL,MAAM,GAEP,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,eAAe,EACf,4BAA4B,EAC5B,uBAAuB,EACvB,eAAe,EACf,wBAAwB,GACzB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,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pC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAC5B,UAAC,aAAa,IAAK,OAAA,CAAC,cAAc,CAAC,IAAI,CAAC,UAAC,OAAO,IAAK,OAAA,OAAO,CAAC,KAAK,CAAC,SAAS,KAAK,aAAa,CAAC,KAAK,CAAC,SAAS,EAAzD,CAAyD,CAAC,EAA5F,CAA4F,CAChH,CAAC;QAEF,IAAI,CAAC,UAAU,EAAE,CAAC;IACpB,CAAC;IACH,kBAAC;AAAD,CAAC,AA7UD,IA6UC","sourcesContent":["import {\n Config,\n DestinationContext as Context,\n DestinationPlugin,\n Event,\n InvalidResponse,\n PayloadTooLargeResponse,\n RateLimitResponse,\n Response,\n Result,\n Status,\n SuccessResponse,\n} from '@amplitude/analytics-types';\nimport {\n INVALID_API_KEY,\n MAX_RETRIES_EXCEEDED_MESSAGE,\n MISSING_API_KEY_MESSAGE,\n SUCCESS_MESSAGE,\n UNEXPECTED_ERROR_MESSAGE,\n} from '../messages';\nimport { STORAGE_PREFIX } from '../constants';\nimport { chunk } from '../utils/chunk';\nimport { buildResult } from '../utils/result-builder';\nimport { createServerConfig, RequestMetadata } from '../config';\nimport { UUID } from '../utils/uuid';\n\nfunction getErrorMessage(error: unknown) {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n\nexport function getResponseBodyString(res: Response) {\n let responseBodyString = '';\n try {\n if ('body' in res) {\n responseBodyString = JSON.stringify(res.body, null, 2);\n }\n } catch {\n // to avoid crash, but don't care about the error, add comment to avoid empty block lint error\n }\n return responseBodyString;\n}\n\nexport class Destination implements DestinationPlugin {\n name = 'amplitude';\n type = 'destination' as const;\n\n retryTimeout = 1000;\n throttleTimeout = 30000;\n storageKey = '';\n // this.config is defined in setup() which will always be called first\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n config: Config;\n // Indicator of whether events that are scheduled (but not flushed yet).\n // When flush:\n // 1. assign `scheduleId` to `flushId`\n // 2. set `scheduleId` to null\n scheduleId: ReturnType<typeof setTimeout> | null = null;\n // Timeout in milliseconds of current schedule\n scheduledTimeout = 0;\n // Indicator of whether current flush resolves.\n // When flush resolves, set `flushId` to null\n flushId: ReturnType<typeof setTimeout> | null = null;\n queue: Context[] = [];\n\n async setup(config: Config): Promise<undefined> {\n this.config = config;\n\n this.storageKey = `${STORAGE_PREFIX}_${this.config.apiKey.substring(0, 10)}`;\n const unsent = await this.config.storageProvider?.get(this.storageKey);\n if (unsent && unsent.length > 0) {\n void Promise.all(unsent.map((event) => this.execute(event))).catch();\n }\n\n return Promise.resolve(undefined);\n }\n\n execute(event: Event): Promise<Result> {\n // Assign insert_id for dropping invalid event later\n if (!event.insert_id) {\n event.insert_id = UUID();\n }\n\n return new Promise((resolve) => {\n const context = {\n event,\n attempts: 0,\n callback: (result: Result) => resolve(result),\n timeout: 0,\n };\n this.queue.push(context);\n this.schedule(this.config.flushIntervalMillis);\n this.saveEvents();\n });\n }\n\n removeEventsExceedFlushMaxRetries(list: Context[]) {\n return list.filter((context) => {\n context.attempts += 1;\n if (context.attempts < this.config.flushMaxRetries) {\n return true;\n }\n void this.fulfillRequest([context], 500, MAX_RETRIES_EXCEEDED_MESSAGE);\n return false;\n });\n }\n\n scheduleEvents(list: Context[]) {\n list.forEach((context) => {\n this.schedule(context.timeout === 0 ? this.config.flushIntervalMillis : context.timeout);\n });\n }\n\n // Schedule a flush in timeout when\n // 1. No schedule\n // 2. Timeout greater than existing timeout.\n // This makes sure that when throttled, no flush when throttle timeout expires.\n schedule(timeout: number) {\n if (this.config.offline) {\n return;\n }\n\n if (this.scheduleId === null || (this.scheduleId && timeout > this.scheduledTimeout)) {\n if (this.scheduleId) {\n clearTimeout(this.scheduleId);\n }\n this.scheduledTimeout = timeout;\n this.scheduleId = setTimeout(() => {\n this.queue = this.queue.map((context) => {\n context.timeout = 0;\n return context;\n });\n void this.flush(true);\n }, timeout);\n return;\n }\n }\n\n // Mark current schedule is flushed.\n resetSchedule() {\n this.scheduleId = null;\n this.scheduledTimeout = 0;\n }\n\n // Flush all events regardless of their timeout\n async flush(useRetry = false) {\n // Skip flush if offline\n if (this.config.offline) {\n this.resetSchedule();\n this.config.loggerProvider.debug('Skipping flush while offline.');\n return;\n }\n\n if (this.flushId) {\n this.resetSchedule();\n this.config.loggerProvider.debug('Skipping flush because previous flush has not resolved.');\n return;\n }\n\n this.flushId = this.scheduleId;\n this.resetSchedule();\n\n const list: Context[] = [];\n const later: Context[] = [];\n this.queue.forEach((context) => (context.timeout === 0 ? list.push(context) : later.push(context)));\n\n const batches = chunk(list, this.config.flushQueueSize);\n\n // Promise.all() doesn't guarantee resolve order.\n // Sequentially resolve to make sure backend receives events in order\n await batches.reduce(async (promise, batch) => {\n await promise;\n return await this.send(batch, useRetry);\n }, Promise.resolve());\n\n // Mark current flush is done\n this.flushId = null;\n\n this.scheduleEvents(this.queue);\n }\n\n async send(list: Context[], useRetry = true) {\n if (!this.config.apiKey) {\n return this.fulfillRequest(list, 400, MISSING_API_KEY_MESSAGE);\n }\n\n const payload = {\n api_key: this.config.apiKey,\n events: list.map((context) => {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { extra, ...eventWithoutExtra } = context.event;\n return eventWithoutExtra;\n }),\n options: {\n min_id_length: this.config.minIdLength,\n },\n client_upload_time: new Date().toISOString(),\n request_metadata: this.config.requestMetadata,\n };\n this.config.requestMetadata = new RequestMetadata();\n\n try {\n const { serverUrl } = createServerConfig(this.config.serverUrl, this.config.serverZone, this.config.useBatch);\n const res = await this.config.transportProvider.send(serverUrl, payload);\n if (res === null) {\n this.fulfillRequest(list, 0, UNEXPECTED_ERROR_MESSAGE);\n return;\n }\n if (!useRetry) {\n if ('body' in res) {\n this.fulfillRequest(list, res.statusCode, `${res.status}: ${getResponseBodyString(res)}`);\n } else {\n this.fulfillRequest(list, res.statusCode, res.status);\n }\n return;\n }\n this.handleResponse(res, list);\n } catch (e) {\n const errorMessage = getErrorMessage(e);\n this.config.loggerProvider.error(errorMessage);\n this.handleResponse({ status: Status.Failed, statusCode: 0 }, list);\n }\n }\n\n handleResponse(res: Response, list: Context[]) {\n const { status } = res;\n\n switch (status) {\n case Status.Success: {\n this.handleSuccessResponse(res, list);\n break;\n }\n case Status.Invalid: {\n this.handleInvalidResponse(res, list);\n break;\n }\n case Status.PayloadTooLarge: {\n this.handlePayloadTooLargeResponse(res, list);\n break;\n }\n case Status.RateLimit: {\n this.handleRateLimitResponse(res, list);\n break;\n }\n default: {\n // log intermediate event status before retry\n this.config.loggerProvider.warn(`{code: 0, error: \"Status '${status}' provided for ${list.length} events\"}`);\n this.handleOtherResponse(list);\n break;\n }\n }\n }\n\n handleSuccessResponse(res: SuccessResponse, list: Context[]) {\n this.fulfillRequest(list, res.statusCode, SUCCESS_MESSAGE);\n }\n\n handleInvalidResponse(res: InvalidResponse, list: Context[]) {\n if (res.body.missingField || res.body.error.startsWith(INVALID_API_KEY)) {\n this.fulfillRequest(list, res.statusCode, res.body.error);\n return;\n }\n\n const dropIndex = [\n ...Object.values(res.body.eventsWithInvalidFields),\n ...Object.values(res.body.eventsWithMissingFields),\n ...Object.values(res.body.eventsWithInvalidIdLengths),\n ...res.body.silencedEvents,\n ].flat();\n const dropIndexSet = new Set(dropIndex);\n\n const retry = list.filter((context, index) => {\n if (dropIndexSet.has(index)) {\n this.fulfillRequest([context], res.statusCode, res.body.error);\n return;\n }\n return true;\n });\n\n if (retry.length > 0) {\n // log intermediate event status before retry\n this.config.loggerProvider.warn(getResponseBodyString(res));\n }\n\n const tryable = this.removeEventsExceedFlushMaxRetries(retry);\n this.scheduleEvents(tryable);\n }\n\n handlePayloadTooLargeResponse(res: PayloadTooLargeResponse, list: Context[]) {\n if (list.length === 1) {\n this.fulfillRequest(list, res.statusCode, res.body.error);\n return;\n }\n\n // log intermediate event status before retry\n this.config.loggerProvider.warn(getResponseBodyString(res));\n\n this.config.flushQueueSize /= 2;\n\n const tryable = this.removeEventsExceedFlushMaxRetries(list);\n this.scheduleEvents(tryable);\n }\n\n handleRateLimitResponse(res: RateLimitResponse, list: Context[]) {\n const dropUserIds = Object.keys(res.body.exceededDailyQuotaUsers);\n const dropDeviceIds = Object.keys(res.body.exceededDailyQuotaDevices);\n const throttledIndex = res.body.throttledEvents;\n const dropUserIdsSet = new Set(dropUserIds);\n const dropDeviceIdsSet = new Set(dropDeviceIds);\n const throttledIndexSet = new Set(throttledIndex);\n\n const retry = list.filter((context, index) => {\n if (\n (context.event.user_id && dropUserIdsSet.has(context.event.user_id)) ||\n (context.event.device_id && dropDeviceIdsSet.has(context.event.device_id))\n ) {\n this.fulfillRequest([context], res.statusCode, res.body.error);\n return;\n }\n if (throttledIndexSet.has(index)) {\n context.timeout = this.throttleTimeout;\n }\n return true;\n });\n\n if (retry.length > 0) {\n // log intermediate event status before retry\n this.config.loggerProvider.warn(getResponseBodyString(res));\n }\n\n const tryable = this.removeEventsExceedFlushMaxRetries(retry);\n this.scheduleEvents(tryable);\n }\n\n handleOtherResponse(list: Context[]) {\n const later = list.map((context) => {\n context.timeout = context.attempts * this.retryTimeout;\n return context;\n });\n\n const tryable = this.removeEventsExceedFlushMaxRetries(later);\n this.scheduleEvents(tryable);\n }\n\n fulfillRequest(list: Context[], code: number, message: string) {\n this.removeEvents(list);\n list.forEach((context) => context.callback(buildResult(context.event, code, message)));\n }\n\n /**\n * This is called on\n * 1) new events are added to queue; or\n * 2) response comes back for a request\n *\n * Update the event storage based on the queue\n */\n saveEvents() {\n if (!this.config.storageProvider) {\n return;\n }\n\n const updatedEvents = this.queue.map((context) => context.event);\n void this.config.storageProvider.set(this.storageKey, updatedEvents);\n }\n\n /**\n * This is called on response comes back for a request\n */\n removeEvents(eventsToRemove: Context[]) {\n this.queue = this.queue.filter(\n (queuedContext) => !eventsToRemove.some((context) => context.event.insert_id === queuedContext.event.insert_id),\n );\n\n this.saveEvents();\n }\n}\n"]}
@@ -4,6 +4,7 @@ export declare class Revenue implements IRevenue {
4
4
  private quantity;
5
5
  private price;
6
6
  private revenueType?;
7
+ private currency?;
7
8
  private properties?;
8
9
  private revenue?;
9
10
  constructor();
@@ -11,6 +12,7 @@ export declare class Revenue implements IRevenue {
11
12
  setQuantity(quantity: number): this;
12
13
  setPrice(price: number): this;
13
14
  setRevenueType(revenueType: string): this;
15
+ setCurrency(currency: string): this;
14
16
  setRevenue(revenue: number): this;
15
17
  setEventProperties(properties: {
16
18
  [key: string]: ValidPropertyType;
@@ -1 +1 @@
1
- {"version":3,"file":"revenue.d.ts","sourceRoot":"","sources":["../../src/revenue.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sBAAsB,EACtB,OAAO,IAAI,QAAQ,EACnB,iBAAiB,EAClB,MAAM,4BAA4B,CAAC;AAGpC,qBAAa,OAAQ,YAAW,QAAQ;IACtC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,WAAW,CAAC,CAAS;IAC7B,OAAO,CAAC,UAAU,CAAC,CAAyB;IAC5C,OAAO,CAAC,OAAO,CAAC,CAAS;;IAQzB,YAAY,CAAC,SAAS,EAAE,MAAM;IAK9B,WAAW,CAAC,QAAQ,EAAE,MAAM;IAO5B,QAAQ,CAAC,KAAK,EAAE,MAAM;IAKtB,cAAc,CAAC,WAAW,EAAE,MAAM;IAKlC,UAAU,CAAC,OAAO,EAAE,MAAM;IAK1B,kBAAkB,CAAC,UAAU,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAAA;KAAE;IAOnE,kBAAkB,IAAI,sBAAsB;CAS7C"}
1
+ {"version":3,"file":"revenue.d.ts","sourceRoot":"","sources":["../../src/revenue.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sBAAsB,EACtB,OAAO,IAAI,QAAQ,EACnB,iBAAiB,EAClB,MAAM,4BAA4B,CAAC;AAGpC,qBAAa,OAAQ,YAAW,QAAQ;IACtC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,WAAW,CAAC,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,CAAS;IAC1B,OAAO,CAAC,UAAU,CAAC,CAAyB;IAC5C,OAAO,CAAC,OAAO,CAAC,CAAS;;IAQzB,YAAY,CAAC,SAAS,EAAE,MAAM;IAK9B,WAAW,CAAC,QAAQ,EAAE,MAAM;IAO5B,QAAQ,CAAC,KAAK,EAAE,MAAM;IAKtB,cAAc,CAAC,WAAW,EAAE,MAAM;IAKlC,WAAW,CAAC,QAAQ,EAAE,MAAM;IAK5B,UAAU,CAAC,OAAO,EAAE,MAAM;IAK1B,kBAAkB,CAAC,UAAU,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAAA;KAAE;IAOnE,kBAAkB,IAAI,sBAAsB;CAU7C"}
@@ -25,6 +25,10 @@ var Revenue = /** @class */ (function () {
25
25
  this.revenueType = revenueType;
26
26
  return this;
27
27
  };
28
+ Revenue.prototype.setCurrency = function (currency) {
29
+ this.currency = currency;
30
+ return this;
31
+ };
28
32
  Revenue.prototype.setRevenue = function (revenue) {
29
33
  this.revenue = revenue;
30
34
  return this;
@@ -41,6 +45,7 @@ var Revenue = /** @class */ (function () {
41
45
  eventProperties[RevenueProperty.REVENUE_QUANTITY] = this.quantity;
42
46
  eventProperties[RevenueProperty.REVENUE_PRICE] = this.price;
43
47
  eventProperties[RevenueProperty.REVENUE_TYPE] = this.revenueType;
48
+ eventProperties[RevenueProperty.REVENUE_CURRENCY] = this.currency;
44
49
  eventProperties[RevenueProperty.REVENUE] = this.revenue;
45
50
  return eventProperties;
46
51
  };
@@ -1 +1 @@
1
- {"version":3,"file":"revenue.js","sourceRoot":"","sources":["../../src/revenue.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,eAAe,GAIhB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CA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AAG,IAAI,CAAC,KAAK,CAAC;QAC5D,eAAe,CAAC,eAAe,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC;QACjE,eAAe,CAAC,eAAe,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC;QACxD,OAAO,eAAe,CAAC;IACzB,CAAC;IACH,cAAC;AAAD,CAAC,AAzDD,IAyDC","sourcesContent":["import {\n RevenueProperty,\n RevenueEventProperties,\n Revenue as IRevenue,\n ValidPropertyType,\n} from '@amplitude/analytics-types';\nimport { isValidObject } from './utils/valid-properties';\n\nexport class Revenue implements IRevenue {\n private productId: string;\n private quantity: number;\n private price: number;\n private revenueType?: string;\n private properties?: { [key: string]: any };\n private revenue?: number;\n\n constructor() {\n this.productId = '';\n this.quantity = 1;\n this.price = 0.0;\n }\n\n setProductId(productId: string) {\n this.productId = productId;\n return this;\n }\n\n setQuantity(quantity: number) {\n if (quantity > 0) {\n this.quantity = quantity;\n }\n return this;\n }\n\n setPrice(price: number) {\n this.price = price;\n return this;\n }\n\n setRevenueType(revenueType: string) {\n this.revenueType = revenueType;\n return this;\n }\n\n setRevenue(revenue: number) {\n this.revenue = revenue;\n return this;\n }\n\n setEventProperties(properties: { [key: string]: ValidPropertyType }) {\n if (isValidObject(properties)) {\n this.properties = properties;\n }\n return this;\n }\n\n getEventProperties(): RevenueEventProperties {\n const eventProperties: RevenueEventProperties = this.properties ? { ...this.properties } : {};\n eventProperties[RevenueProperty.REVENUE_PRODUCT_ID] = this.productId;\n eventProperties[RevenueProperty.REVENUE_QUANTITY] = this.quantity;\n eventProperties[RevenueProperty.REVENUE_PRICE] = this.price;\n eventProperties[RevenueProperty.REVENUE_TYPE] = this.revenueType;\n eventProperties[RevenueProperty.REVENUE] = this.revenue;\n return eventProperties;\n }\n}\n"]}
1
+ {"version":3,"file":"revenue.js","sourceRoot":"","sources":["../../src/revenue.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,eAAe,GAIhB,MAAM,4BAA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AC,kBAAkB,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC;QACrE,eAAe,CAAC,eAAe,CAAC,gBAAgB,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;QAClE,eAAe,CAAC,eAAe,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC;QAC5D,eAAe,CAAC,eAAe,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC;QACjE,eAAe,CAAC,eAAe,CAAC,gBAAgB,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;QAClE,eAAe,CAAC,eAAe,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC;QACxD,OAAO,eAAe,CAAC;IACzB,CAAC;IACH,cAAC;AAAD,CAAC,AAhED,IAgEC","sourcesContent":["import {\n RevenueProperty,\n RevenueEventProperties,\n Revenue as IRevenue,\n ValidPropertyType,\n} from '@amplitude/analytics-types';\nimport { isValidObject } from './utils/valid-properties';\n\nexport class Revenue implements IRevenue {\n private productId: string;\n private quantity: number;\n private price: number;\n private revenueType?: string;\n private currency?: string;\n private properties?: { [key: string]: any };\n private revenue?: number;\n\n constructor() {\n this.productId = '';\n this.quantity = 1;\n this.price = 0.0;\n }\n\n setProductId(productId: string) {\n this.productId = productId;\n return this;\n }\n\n setQuantity(quantity: number) {\n if (quantity > 0) {\n this.quantity = quantity;\n }\n return this;\n }\n\n setPrice(price: number) {\n this.price = price;\n return this;\n }\n\n setRevenueType(revenueType: string) {\n this.revenueType = revenueType;\n return this;\n }\n\n setCurrency(currency: string) {\n this.currency = currency;\n return this;\n }\n\n setRevenue(revenue: number) {\n this.revenue = revenue;\n return this;\n }\n\n setEventProperties(properties: { [key: string]: ValidPropertyType }) {\n if (isValidObject(properties)) {\n this.properties = properties;\n }\n return this;\n }\n\n getEventProperties(): RevenueEventProperties {\n const eventProperties: RevenueEventProperties = this.properties ? { ...this.properties } : {};\n eventProperties[RevenueProperty.REVENUE_PRODUCT_ID] = this.productId;\n eventProperties[RevenueProperty.REVENUE_QUANTITY] = this.quantity;\n eventProperties[RevenueProperty.REVENUE_PRICE] = this.price;\n eventProperties[RevenueProperty.REVENUE_TYPE] = this.revenueType;\n eventProperties[RevenueProperty.REVENUE_CURRENCY] = this.currency;\n eventProperties[RevenueProperty.REVENUE] = this.revenue;\n return eventProperties;\n }\n}\n"]}
@@ -6,7 +6,7 @@ export declare class Timeline {
6
6
  plugins: Plugin[];
7
7
  constructor(client: CoreClient);
8
8
  register(plugin: Plugin, config: Config): Promise<void>;
9
- deregister(pluginName: string): Promise<void>;
9
+ deregister(pluginName: string, config: Config): Promise<void>;
10
10
  reset(client: CoreClient): void;
11
11
  push(event: Event): Promise<Result>;
12
12
  scheduleApply(timeout: number): void;
@@ -1 +1 @@
1
- {"version":3,"file":"timeline.d.ts","sourceRoot":"","sources":["../../src/timeline.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,MAAM,EACN,UAAU,EAGV,KAAK,EACL,aAAa,EACb,MAAM,EACN,MAAM,EACP,MAAM,4BAA4B,CAAC;AAIpC,qBAAa,QAAQ;IAQP,OAAO,CAAC,MAAM;IAP1B,KAAK,EAAE,CAAC,KAAK,EAAE,aAAa,CAAC,EAAE,CAAM;IAErC,QAAQ,UAAS;IAGjB,OAAO,EAAE,MAAM,EAAE,CAAM;gBAEH,MAAM,EAAE,UAAU;IAEhC,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAmBvC,UAAU,CAAC,UAAU,EAAE,MAAM;IAOnC,KAAK,CAAC,MAAM,EAAE,UAAU;IAQxB,IAAI,CAAC,KAAK,EAAE,KAAK;IAOjB,aAAa,CAAC,OAAO,EAAE,MAAM;IAavB,KAAK,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,aAAa,CAAC,GAAG,SAAS;IAgE9C,KAAK;CAgBZ"}
1
+ {"version":3,"file":"timeline.d.ts","sourceRoot":"","sources":["../../src/timeline.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,MAAM,EACN,UAAU,EAGV,KAAK,EACL,aAAa,EACb,MAAM,EACN,MAAM,EACP,MAAM,4BAA4B,CAAC;AAIpC,qBAAa,QAAQ;IAQP,OAAO,CAAC,MAAM;IAP1B,KAAK,EAAE,CAAC,KAAK,EAAE,aAAa,CAAC,EAAE,CAAM;IAErC,QAAQ,UAAS;IAGjB,OAAO,EAAE,MAAM,EAAE,CAAM;gBAEH,MAAM,EAAE,UAAU;IAEhC,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAmBvC,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAWnD,KAAK,CAAC,MAAM,EAAE,UAAU;IAQxB,IAAI,CAAC,KAAK,EAAE,KAAK;IAOjB,aAAa,CAAC,OAAO,EAAE,MAAM;IAavB,KAAK,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,aAAa,CAAC,GAAG,SAAS;IAgE9C,KAAK;CAgBZ"}
@@ -36,7 +36,7 @@ var Timeline = /** @class */ (function () {
36
36
  });
37
37
  });
38
38
  };
39
- Timeline.prototype.deregister = function (pluginName) {
39
+ Timeline.prototype.deregister = function (pluginName, config) {
40
40
  var _a;
41
41
  return __awaiter(this, void 0, void 0, function () {
42
42
  var index, plugin;
@@ -44,6 +44,10 @@ var Timeline = /** @class */ (function () {
44
44
  switch (_b.label) {
45
45
  case 0:
46
46
  index = this.plugins.findIndex(function (plugin) { return plugin.name === pluginName; });
47
+ if (index === -1) {
48
+ config.loggerProvider.warn("Plugin with name ".concat(pluginName, " does not exist, skipping deregistration"));
49
+ return [2 /*return*/];
50
+ }
47
51
  plugin = this.plugins[index];
48
52
  this.plugins.splice(index, 1);
49
53
  return [4 /*yield*/, ((_a = plugin.teardown) === null || _a === void 0 ? void 0 : _a.call(plugin))];
@@ -1 +1 @@
1
- {"version":3,"file":"timeline.js","sourceRoot":"","sources":["../../src/timeline.ts"],"names":[],"mappings":";AAWA,OAAO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AK,OAAA,KAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAhB,CAAgB,CAAC,CAAC,EAAA;;wBAAxD,SAAwD,CAAC;wBAEnD,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CACrC,UAAC,MAAc,IAAkC,OAAA,MAAM,CAAC,IAAI,KAAK,aAAa,EAA7B,CAA6B,CAC/E,CAAC;wBAEI,mBAAmB,GAAG,WAAW,CAAC,GAAG,CAAC,UAAC,MAAM;4BACjD,OAAO,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;wBACxC,CAAC,CAAC,CAAC;wBAEH,qBAAM,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,EAAA;;wBAAtC,SAAsC,CAAC;;;;;KACxC;IACH,eAAC;AAAD,CAAC,AAhJD,IAgJC","sourcesContent":["import {\n BeforePlugin,\n Config,\n CoreClient,\n DestinationPlugin,\n EnrichmentPlugin,\n Event,\n EventCallback,\n Plugin,\n Result,\n} from '@amplitude/analytics-types';\nimport { buildResult } from './utils/result-builder';\nimport { UUID } from './utils/uuid';\n\nexport class Timeline {\n queue: [Event, EventCallback][] = [];\n // Flag to guarantee one schedule apply is running\n applying = false;\n // Flag indicates whether timeline is ready to process event\n // Events collected before timeline is ready will stay in the queue to be processed later\n plugins: Plugin[] = [];\n\n constructor(private client: CoreClient) {}\n\n async register(plugin: Plugin, config: Config) {\n if (this.plugins.some((existingPlugin) => existingPlugin.name === plugin.name)) {\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n config.loggerProvider.warn(`Plugin with name ${plugin.name} already exists, skipping registration`);\n return;\n }\n\n if (plugin.name === undefined) {\n plugin.name = UUID();\n config.loggerProvider.warn(`Plugin name is undefined. \n Generating a random UUID for plugin name: ${plugin.name}. \n Set a name for the plugin to prevent it from being added multiple times.`);\n }\n\n plugin.type = plugin.type ?? 'enrichment';\n await plugin.setup?.(config, this.client);\n this.plugins.push(plugin);\n }\n\n async deregister(pluginName: string) {\n const index = this.plugins.findIndex((plugin) => plugin.name === pluginName);\n const plugin = this.plugins[index];\n this.plugins.splice(index, 1);\n await plugin.teardown?.();\n }\n\n reset(client: CoreClient) {\n this.applying = false;\n const plugins = this.plugins;\n plugins.map((plugin) => plugin.teardown?.());\n this.plugins = [];\n this.client = client;\n }\n\n push(event: Event) {\n return new Promise<Result>((resolve) => {\n this.queue.push([event, resolve]);\n this.scheduleApply(0);\n });\n }\n\n scheduleApply(timeout: number) {\n if (this.applying) return;\n this.applying = true;\n setTimeout(() => {\n void this.apply(this.queue.shift()).then(() => {\n this.applying = false;\n if (this.queue.length > 0) {\n this.scheduleApply(0);\n }\n });\n }, timeout);\n }\n\n async apply(item: [Event, EventCallback] | undefined) {\n if (!item) {\n return;\n }\n\n let [event] = item;\n const [, resolve] = item;\n\n const before = this.plugins.filter<BeforePlugin>(\n (plugin: Plugin): plugin is BeforePlugin => plugin.type === 'before',\n );\n\n for (const plugin of before) {\n /* istanbul ignore if */\n if (!plugin.execute) {\n // do nothing\n continue;\n }\n const e = await plugin.execute({ ...event });\n if (e === null) {\n resolve({ event, code: 0, message: '' });\n return;\n } else {\n event = e;\n }\n }\n\n const enrichment = this.plugins.filter<EnrichmentPlugin>(\n (plugin: Plugin): plugin is EnrichmentPlugin => plugin.type === 'enrichment' || plugin.type === undefined,\n );\n\n for (const plugin of enrichment) {\n /* istanbul ignore if */\n if (!plugin.execute) {\n // do nothing\n continue;\n }\n const e = await plugin.execute({ ...event });\n if (e === null) {\n resolve({ event, code: 0, message: '' });\n return;\n } else {\n event = e;\n }\n }\n\n const destination = this.plugins.filter<DestinationPlugin>(\n (plugin: Plugin): plugin is DestinationPlugin => plugin.type === 'destination',\n );\n\n const executeDestinations = destination.map((plugin) => {\n const eventClone = { ...event };\n return plugin.execute(eventClone).catch((e) => buildResult(eventClone, 0, String(e)));\n });\n\n void Promise.all(executeDestinations).then(([result]) => {\n const resolveResult =\n result || buildResult(event, 100, 'Event not tracked, no destination plugins on the instance');\n resolve(resolveResult);\n });\n\n return;\n }\n\n async flush() {\n const queue = this.queue;\n this.queue = [];\n\n await Promise.all(queue.map((item) => this.apply(item)));\n\n const destination = this.plugins.filter<DestinationPlugin>(\n (plugin: Plugin): plugin is DestinationPlugin => plugin.type === 'destination',\n );\n\n const executeDestinations = destination.map((plugin) => {\n return plugin.flush && plugin.flush();\n });\n\n await Promise.all(executeDestinations);\n }\n}\n"]}
1
+ {"version":3,"file":"timeline.js","sourceRoot":"","sources":["../../src/timeline.ts"],"names":[],"mappings":";AAWA,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC;IAQE,kBAAoB,MAAkB;QAAlB,WAAM,GAAN,MAAM,CAAY;QAPtC,UAAK,GAA6B,EAAE,CAAC;QACrC,kDAAkD;QAClD,aAAQ,GAAG,KAAK,CAAC;QACjB,4DAA4D;QAC5D,yFAAyF;QACzF,YAAO,GAAa,EAAE,CAAC;IAEkB,CAAC;IAEpC,2BAAQ,GAAd,UAAe,MAAc,EAAE,MAAc;;;;;;wBAC3C,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,UAAC,cAAc,IAAK,OAAA,cAAc,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI,EAAnC,CAAmC,CAAC,EAAE;4BAC9E,4EAA4E;4BAC5E,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,2BAAoB,MAAM,CAAC,IAAI,2CAAwC,CAAC,CAAC;4BACpG,sBAAO;yBACR;wBAED,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS,EAAE;4BAC7B,MAAM,CAAC,IAAI,GAAG,IAAI,EAAE,CAAC;4BACrB,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,sFACiB,MAAM,CAAC,IAAI,uFACkB,CAAC,CAAC;yBAC5E;wBAED,MAAM,CAAC,IAAI,GAAG,MAAA,MAAM,CAAC,IAAI,mCAAI,YAAY,C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,CAAC;wBAEH,KAAK,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC,IAAI,CAAC,UAAC,EAAQ;gCAAR,KAAA,aAAQ,EAAP,MAAM,QAAA;4BACjD,IAAM,aAAa,GACjB,MAAM,IAAI,WAAW,CAAC,KAAK,EAAE,GAAG,EAAE,2DAA2D,CAAC,CAAC;4BACjG,OAAO,CAAC,aAAa,CAAC,CAAC;wBACzB,CAAC,CAAC,CAAC;wBAEH,sBAAO;;;;KACR;IAEK,wBAAK,GAAX;;;;;;;wBACQ,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;wBACzB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;wBAEhB,qBAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,UAAC,IAAI,IAAK,OAAA,KAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAhB,CAAgB,CAAC,CAAC,EAAA;;wBAAxD,SAAwD,CAAC;wBAEnD,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CACrC,UAAC,MAAc,IAAkC,OAAA,MAAM,CAAC,IAAI,KAAK,aAAa,EAA7B,CAA6B,CAC/E,CAAC;wBAEI,mBAAmB,GAAG,WAAW,CAAC,GAAG,CAAC,UAAC,MAAM;4BACjD,OAAO,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;wBACxC,CAAC,CAAC,CAAC;wBAEH,qBAAM,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,EAAA;;wBAAtC,SAAsC,CAAC;;;;;KACxC;IACH,eAAC;AAAD,CAAC,AApJD,IAoJC","sourcesContent":["import {\n BeforePlugin,\n Config,\n CoreClient,\n DestinationPlugin,\n EnrichmentPlugin,\n Event,\n EventCallback,\n Plugin,\n Result,\n} from '@amplitude/analytics-types';\nimport { buildResult } from './utils/result-builder';\nimport { UUID } from './utils/uuid';\n\nexport class Timeline {\n queue: [Event, EventCallback][] = [];\n // Flag to guarantee one schedule apply is running\n applying = false;\n // Flag indicates whether timeline is ready to process event\n // Events collected before timeline is ready will stay in the queue to be processed later\n plugins: Plugin[] = [];\n\n constructor(private client: CoreClient) {}\n\n async register(plugin: Plugin, config: Config) {\n if (this.plugins.some((existingPlugin) => existingPlugin.name === plugin.name)) {\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n config.loggerProvider.warn(`Plugin with name ${plugin.name} already exists, skipping registration`);\n return;\n }\n\n if (plugin.name === undefined) {\n plugin.name = UUID();\n config.loggerProvider.warn(`Plugin name is undefined. \n Generating a random UUID for plugin name: ${plugin.name}. \n Set a name for the plugin to prevent it from being added multiple times.`);\n }\n\n plugin.type = plugin.type ?? 'enrichment';\n await plugin.setup?.(config, this.client);\n this.plugins.push(plugin);\n }\n\n async deregister(pluginName: string, config: Config) {\n const index = this.plugins.findIndex((plugin) => plugin.name === pluginName);\n if (index === -1) {\n config.loggerProvider.warn(`Plugin with name ${pluginName} does not exist, skipping deregistration`);\n return;\n }\n const plugin = this.plugins[index];\n this.plugins.splice(index, 1);\n await plugin.teardown?.();\n }\n\n reset(client: CoreClient) {\n this.applying = false;\n const plugins = this.plugins;\n plugins.map((plugin) => plugin.teardown?.());\n this.plugins = [];\n this.client = client;\n }\n\n push(event: Event) {\n return new Promise<Result>((resolve) => {\n this.queue.push([event, resolve]);\n this.scheduleApply(0);\n });\n }\n\n scheduleApply(timeout: number) {\n if (this.applying) return;\n this.applying = true;\n setTimeout(() => {\n void this.apply(this.queue.shift()).then(() => {\n this.applying = false;\n if (this.queue.length > 0) {\n this.scheduleApply(0);\n }\n });\n }, timeout);\n }\n\n async apply(item: [Event, EventCallback] | undefined) {\n if (!item) {\n return;\n }\n\n let [event] = item;\n const [, resolve] = item;\n\n const before = this.plugins.filter<BeforePlugin>(\n (plugin: Plugin): plugin is BeforePlugin => plugin.type === 'before',\n );\n\n for (const plugin of before) {\n /* istanbul ignore if */\n if (!plugin.execute) {\n // do nothing\n continue;\n }\n const e = await plugin.execute({ ...event });\n if (e === null) {\n resolve({ event, code: 0, message: '' });\n return;\n } else {\n event = e;\n }\n }\n\n const enrichment = this.plugins.filter<EnrichmentPlugin>(\n (plugin: Plugin): plugin is EnrichmentPlugin => plugin.type === 'enrichment' || plugin.type === undefined,\n );\n\n for (const plugin of enrichment) {\n /* istanbul ignore if */\n if (!plugin.execute) {\n // do nothing\n continue;\n }\n const e = await plugin.execute({ ...event });\n if (e === null) {\n resolve({ event, code: 0, message: '' });\n return;\n } else {\n event = e;\n }\n }\n\n const destination = this.plugins.filter<DestinationPlugin>(\n (plugin: Plugin): plugin is DestinationPlugin => plugin.type === 'destination',\n );\n\n const executeDestinations = destination.map((plugin) => {\n const eventClone = { ...event };\n return plugin.execute(eventClone).catch((e) => buildResult(eventClone, 0, String(e)));\n });\n\n void Promise.all(executeDestinations).then(([result]) => {\n const resolveResult =\n result || buildResult(event, 100, 'Event not tracked, no destination plugins on the instance');\n resolve(resolveResult);\n });\n\n return;\n }\n\n async flush() {\n const queue = this.queue;\n this.queue = [];\n\n await Promise.all(queue.map((item) => this.apply(item)));\n\n const destination = this.plugins.filter<DestinationPlugin>(\n (plugin: Plugin): plugin is DestinationPlugin => plugin.type === 'destination',\n );\n\n const executeDestinations = destination.map((plugin) => {\n return plugin.flush && plugin.flush();\n });\n\n await Promise.all(executeDestinations);\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amplitude/analytics-core",
3
- "version": "2.5.4",
3
+ "version": "2.5.6",
4
4
  "description": "",
5
5
  "author": "Amplitude Inc",
6
6
  "homepage": "https://github.com/amplitude/Amplitude-TypeScript",
@@ -35,11 +35,11 @@
35
35
  "url": "https://github.com/amplitude/Amplitude-TypeScript/issues"
36
36
  },
37
37
  "dependencies": {
38
- "@amplitude/analytics-types": "^2.8.4",
38
+ "@amplitude/analytics-types": "^2.9.0",
39
39
  "tslib": "^2.4.1"
40
40
  },
41
41
  "files": [
42
42
  "lib"
43
43
  ],
44
- "gitHead": "368f7d32ddf547a14395cebcc2bbc4dee477acdb"
44
+ "gitHead": "e9b1fd973f86c5f262465f2d033b73a742697219"
45
45
  }