@aws-amplify/analytics 5.1.9 → 5.1.10-custom-pk.86

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/analytics",
3
- "version": "5.1.9",
3
+ "version": "5.1.10-custom-pk.86+e6bcf1cd8",
4
4
  "description": "Analytics category of aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -43,8 +43,8 @@
43
43
  },
44
44
  "homepage": "https://aws-amplify.github.io/",
45
45
  "dependencies": {
46
- "@aws-amplify/cache": "4.0.31",
47
- "@aws-amplify/core": "4.3.11",
46
+ "@aws-amplify/cache": "4.0.32-custom-pk.86+e6bcf1cd8",
47
+ "@aws-amplify/core": "4.3.12-custom-pk.86+e6bcf1cd8",
48
48
  "@aws-sdk/client-firehose": "3.6.1",
49
49
  "@aws-sdk/client-kinesis": "3.6.1",
50
50
  "@aws-sdk/client-personalize-events": "3.6.1",
@@ -101,5 +101,5 @@
101
101
  "<rootDir>/src/setupTests.ts"
102
102
  ]
103
103
  },
104
- "gitHead": "b567fc3b9ad28043fd07ce93e78ec5958414e82f"
104
+ "gitHead": "e6bcf1cd8128b132b9bc06f98bb6694cf23e41b2"
105
105
  }
package/src/Analytics.ts CHANGED
@@ -19,7 +19,16 @@ import {
19
19
  } from '@aws-amplify/core';
20
20
  import { AWSPinpointProvider } from './Providers/AWSPinpointProvider';
21
21
 
22
- import { AnalyticsProvider, EventMetrics } from './types';
22
+ import {
23
+ AnalyticsProvider,
24
+ EventAttributes,
25
+ EventMetrics,
26
+ AnalyticsEvent,
27
+ AutoTrackSessionOpts,
28
+ AutoTrackPageViewOpts,
29
+ AutoTrackEventOpts,
30
+ PersonalizeAnalyticsEvent,
31
+ } from './types';
23
32
  import { PageViewTracker, EventTracker, SessionTracker } from './trackers';
24
33
 
25
34
  const logger = new Logger('AnalyticsClass');
@@ -45,6 +54,8 @@ const trackers = {
45
54
  session: SessionTracker,
46
55
  };
47
56
 
57
+ type TrackerTypes = keyof typeof trackers;
58
+ type Trackers = typeof trackers[TrackerTypes];
48
59
  let _instance = null;
49
60
 
50
61
  /**
@@ -53,8 +64,8 @@ let _instance = null;
53
64
  export class AnalyticsClass {
54
65
  private _config;
55
66
  private _pluggables: AnalyticsProvider[];
56
- private _disabled;
57
- private _trackers;
67
+ private _disabled: boolean;
68
+ private _trackers: Trackers | {};
58
69
 
59
70
  /**
60
71
  * Initialize Analtyics
@@ -100,7 +111,7 @@ export class AnalyticsClass {
100
111
  this._config['autoSessionRecord'] = true;
101
112
  }
102
113
 
103
- this._pluggables.forEach((pluggable) => {
114
+ this._pluggables.forEach(pluggable => {
104
115
  // for backward compatibility
105
116
  const providerConfig =
106
117
  pluggable.getProviderName() === 'AWSPinpoint' &&
@@ -130,7 +141,7 @@ export class AnalyticsClass {
130
141
 
131
142
  /**
132
143
  * add plugin into Analytics category
133
- * @param {Object} pluggable - an instance of the plugin
144
+ * @param pluggable - an instance of the plugin
134
145
  */
135
146
  public addPluggable(pluggable: AnalyticsProvider) {
136
147
  if (pluggable && pluggable.getCategory() === 'Analytics') {
@@ -149,9 +160,9 @@ export class AnalyticsClass {
149
160
 
150
161
  /**
151
162
  * Get the plugin object
152
- * @param providerName - the name of the plugin
163
+ * @param providerName - the name of the provider to be removed
153
164
  */
154
- public getPluggable(providerName) {
165
+ public getPluggable(providerName: string): AnalyticsProvider {
155
166
  for (let i = 0; i < this._pluggables.length; i += 1) {
156
167
  const pluggable = this._pluggables[i];
157
168
  if (pluggable.getProviderName() === providerName) {
@@ -165,9 +176,9 @@ export class AnalyticsClass {
165
176
 
166
177
  /**
167
178
  * Remove the plugin object
168
- * @param providerName - the name of the plugin
179
+ * @param providerName - the name of the provider to be removed
169
180
  */
170
- public removePluggable(providerName) {
181
+ public removePluggable(providerName: string): void {
171
182
  let idx = 0;
172
183
  while (idx < this._pluggables.length) {
173
184
  if (this._pluggables[idx].getProviderName() === providerName) {
@@ -201,6 +212,7 @@ export class AnalyticsClass {
201
212
 
202
213
  /**
203
214
  * Record Session start
215
+ * @param [provider] - name of the provider.
204
216
  * @return - A promise which resolves if buffer doesn't overflow
205
217
  */
206
218
  public async startSession(provider?: string) {
@@ -210,6 +222,7 @@ export class AnalyticsClass {
210
222
 
211
223
  /**
212
224
  * Record Session stop
225
+ * @param [provider] - name of the provider.
213
226
  * @return - A promise which resolves if buffer doesn't overflow
214
227
  */
215
228
  public async stopSession(provider?: string) {
@@ -219,14 +232,29 @@ export class AnalyticsClass {
219
232
 
220
233
  /**
221
234
  * Record one analytic event and send it to Pinpoint
222
- * @param {String} name - The name of the event
223
- * @param {Object} [attributes] - Attributes of the event
224
- * @param {Object} [metrics] - Event metrics
235
+ * @param event - An object with the name of the event, attributes of the event and event metrics.
236
+ * @param [provider] - name of the provider.
237
+ */
238
+ public async record(
239
+ event: AnalyticsEvent | PersonalizeAnalyticsEvent,
240
+ provider?: string
241
+ );
242
+ /**
243
+ * Record one analytic event and send it to Pinpoint
244
+ * @deprecated Use the new syntax and pass in the event as an object instead.
245
+ * @param eventName - The name of the event
246
+ * @param [attributes] - Attributes of the event
247
+ * @param [metrics] - Event metrics
225
248
  * @return - A promise which resolves if buffer doesn't overflow
226
249
  */
227
250
  public async record(
228
- event: string | object,
229
- provider?,
251
+ eventName: string,
252
+ attributes?: EventAttributes,
253
+ metrics?: EventMetrics
254
+ );
255
+ public async record(
256
+ event: string | AnalyticsEvent | PersonalizeAnalyticsEvent,
257
+ providerOrAttributes?: string | EventAttributes,
230
258
  metrics?: EventMetrics
231
259
  ) {
232
260
  let params = null;
@@ -235,24 +263,27 @@ export class AnalyticsClass {
235
263
  params = {
236
264
  event: {
237
265
  name: event,
238
- attributes: provider,
266
+ attributes: providerOrAttributes,
239
267
  metrics,
240
268
  },
241
269
  provider: 'AWSPinpoint',
242
270
  };
243
271
  } else {
244
- params = { event, provider };
272
+ params = { event, provider: providerOrAttributes };
245
273
  }
246
274
  return this._sendEvent(params);
247
275
  }
248
276
 
249
- public async updateEndpoint(attrs, provider?) {
277
+ public async updateEndpoint(
278
+ attrs: { [key: string]: any },
279
+ provider?: string
280
+ ) {
250
281
  const event = { ...attrs, name: '_update_endpoint' };
251
282
 
252
283
  return this.record(event, provider);
253
284
  }
254
285
 
255
- private _sendEvent(params) {
286
+ private _sendEvent(params: { event: AnalyticsEvent; provider?: string }) {
256
287
  if (this._disabled) {
257
288
  logger.debug('Analytics has been disabled');
258
289
  return Promise.resolve();
@@ -261,7 +292,7 @@ export class AnalyticsClass {
261
292
  const provider = params.provider ? params.provider : 'AWSPinpoint';
262
293
 
263
294
  return new Promise((resolve, reject) => {
264
- this._pluggables.forEach((pluggable) => {
295
+ this._pluggables.forEach(pluggable => {
265
296
  if (pluggable.getProviderName() === provider) {
266
297
  pluggable.record(params, { resolve, reject });
267
298
  }
@@ -269,7 +300,20 @@ export class AnalyticsClass {
269
300
  });
270
301
  }
271
302
 
272
- public autoTrack(trackerType, opts) {
303
+ /**
304
+ * Enable or disable auto tracking
305
+ * @param trackerType - The type of tracker to activate.
306
+ * @param [opts] - Auto tracking options.
307
+ */
308
+ public autoTrack(trackerType: 'session', opts: AutoTrackSessionOpts);
309
+ public autoTrack(trackerType: 'pageView', opts: AutoTrackPageViewOpts);
310
+ public autoTrack(trackerType: 'event', opts: AutoTrackEventOpts);
311
+ // ensures backwards compatibility for non-pinpoint provider users
312
+ public autoTrack(
313
+ trackerType: TrackerTypes,
314
+ opts: { provider: string; [key: string]: any }
315
+ );
316
+ public autoTrack(trackerType: TrackerTypes, opts: { [key: string]: any }) {
273
317
  if (!trackers[trackerType]) {
274
318
  logger.debug('invalid tracker type');
275
319
  return;
@@ -295,7 +339,7 @@ export class AnalyticsClass {
295
339
  let endpointUpdated = false;
296
340
  let authConfigured = false;
297
341
  let analyticsConfigured = false;
298
- const listener = (capsule) => {
342
+ const listener = capsule => {
299
343
  const { channel, payload } = capsule;
300
344
  logger.debug('on hub capsule ' + channel, payload);
301
345
 
@@ -314,7 +358,7 @@ const listener = (capsule) => {
314
358
  }
315
359
  };
316
360
 
317
- const storageEvent = (payload) => {
361
+ const storageEvent = payload => {
318
362
  const {
319
363
  data: { attrs, metrics },
320
364
  } = payload;
@@ -327,19 +371,19 @@ const storageEvent = (payload) => {
327
371
  attributes: attrs,
328
372
  metrics,
329
373
  })
330
- .catch((e) => {
374
+ .catch(e => {
331
375
  logger.debug('Failed to send the storage event automatically', e);
332
376
  });
333
377
  }
334
378
  };
335
379
 
336
- const authEvent = (payload) => {
380
+ const authEvent = payload => {
337
381
  const { event } = payload;
338
382
  if (!event) {
339
383
  return;
340
384
  }
341
385
 
342
- const recordAuthEvent = async (eventName) => {
386
+ const recordAuthEvent = async eventName => {
343
387
  if (authConfigured && analyticsConfigured) {
344
388
  try {
345
389
  return await _instance.record({ name: `_userauth.${eventName}` });
@@ -370,7 +414,7 @@ const authEvent = (payload) => {
370
414
  }
371
415
  };
372
416
 
373
- const analyticsEvent = (payload) => {
417
+ const analyticsEvent = payload => {
374
418
  const { event } = payload;
375
419
  if (!event) return;
376
420
 
@@ -387,7 +431,7 @@ const analyticsEvent = (payload) => {
387
431
  const sendEvents = () => {
388
432
  const config = _instance.configure();
389
433
  if (!endpointUpdated && config['autoSessionRecord']) {
390
- _instance.updateEndpoint({ immediate: true }).catch((e) => {
434
+ _instance.updateEndpoint({ immediate: true }).catch(e => {
391
435
  logger.debug('Failed to update the endpoint', e);
392
436
  });
393
437
  endpointUpdated = true;
@@ -43,7 +43,8 @@ export default class EventsBuffer {
43
43
  }
44
44
 
45
45
  public push(event: EventObject) {
46
- if (this._buffer > this._config.bufferSize) {
46
+ // if the buffer is currently at the configured limit, pushing would exceed it
47
+ if (this._buffer.length >= this._config.bufferSize) {
47
48
  logger.debug('Exceeded analytics events buffer size');
48
49
  return event.handlers.reject(
49
50
  new Error('Exceeded the size of analytics events buffer')
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ * Copyright 2017-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
5
  * the License. A copy of the License is located at
@@ -11,7 +11,6 @@
11
11
  * and limitations under the License.
12
12
  */
13
13
  import { ICredentials } from '@aws-amplify/core';
14
-
15
14
  /**
16
15
  * Analytics instance options
17
16
  */
@@ -59,3 +58,39 @@ export interface SessionTrackOpts {
59
58
  | (() => EventAttributes | Promise<EventAttributes>);
60
59
  provider?: string;
61
60
  }
61
+
62
+ export type AutoTrackAttributes =
63
+ | (() => EventAttributes | Promise<EventAttributes>)
64
+ | EventAttributes;
65
+
66
+ export interface AutoTrackSessionOpts {
67
+ enable: boolean;
68
+ attributes?: AutoTrackAttributes;
69
+ provider?: string;
70
+ }
71
+
72
+ export interface AutoTrackPageViewOpts {
73
+ enable: boolean;
74
+ eventName?: string;
75
+ attributes?: AutoTrackAttributes;
76
+ type?: 'SPA' | 'multiPageApp';
77
+ provider?: string;
78
+ getUrl?: () => string;
79
+ }
80
+
81
+ export interface AutoTrackEventOpts {
82
+ enable: boolean;
83
+ events?: string[];
84
+ selectorPrefix?: string;
85
+ provider?: string;
86
+ attributes?: AutoTrackAttributes;
87
+ }
88
+
89
+ export interface AnalyticsEvent {
90
+ name: string;
91
+ attributes?: EventAttributes;
92
+ metrics?: EventMetrics;
93
+ immediate?: boolean;
94
+ }
95
+
96
+ export { PersonalizeAnalyticsEvent } from './Providers/AmazonPersonalizeProvider';
@@ -0,0 +1,7 @@
1
+ export interface PersonalizeAnalyticsEvent {
2
+ eventType?: 'Identify' | 'MediaAutoTrack';
3
+ userId?: string;
4
+ properties?: {
5
+ [key: string]: string;
6
+ };
7
+ }