@aws-amplify/analytics 5.1.13-unstable.1 → 5.1.13-unstable.10

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/src/Analytics.ts CHANGED
@@ -19,7 +19,15 @@ 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
+ } from './types';
23
31
  import { PageViewTracker, EventTracker, SessionTracker } from './trackers';
24
32
 
25
33
  const logger = new Logger('AnalyticsClass');
@@ -45,6 +53,8 @@ const trackers = {
45
53
  session: SessionTracker,
46
54
  };
47
55
 
56
+ type TrackerTypes = keyof typeof trackers;
57
+ type Trackers = typeof trackers[TrackerTypes];
48
58
  let _instance = null;
49
59
 
50
60
  /**
@@ -53,8 +63,8 @@ let _instance = null;
53
63
  export class AnalyticsClass {
54
64
  private _config;
55
65
  private _pluggables: AnalyticsProvider[];
56
- private _disabled;
57
- private _trackers;
66
+ private _disabled: boolean;
67
+ private _trackers: Trackers | {};
58
68
 
59
69
  /**
60
70
  * Initialize Analtyics
@@ -100,7 +110,7 @@ export class AnalyticsClass {
100
110
  this._config['autoSessionRecord'] = true;
101
111
  }
102
112
 
103
- this._pluggables.forEach((pluggable) => {
113
+ this._pluggables.forEach(pluggable => {
104
114
  // for backward compatibility
105
115
  const providerConfig =
106
116
  pluggable.getProviderName() === 'AWSPinpoint' &&
@@ -130,7 +140,7 @@ export class AnalyticsClass {
130
140
 
131
141
  /**
132
142
  * add plugin into Analytics category
133
- * @param {Object} pluggable - an instance of the plugin
143
+ * @param pluggable - an instance of the plugin
134
144
  */
135
145
  public addPluggable(pluggable: AnalyticsProvider) {
136
146
  if (pluggable && pluggable.getCategory() === 'Analytics') {
@@ -149,9 +159,9 @@ export class AnalyticsClass {
149
159
 
150
160
  /**
151
161
  * Get the plugin object
152
- * @param providerName - the name of the plugin
162
+ * @param providerName - the name of the provider to be removed
153
163
  */
154
- public getPluggable(providerName) {
164
+ public getPluggable(providerName: string): AnalyticsProvider {
155
165
  for (let i = 0; i < this._pluggables.length; i += 1) {
156
166
  const pluggable = this._pluggables[i];
157
167
  if (pluggable.getProviderName() === providerName) {
@@ -165,9 +175,9 @@ export class AnalyticsClass {
165
175
 
166
176
  /**
167
177
  * Remove the plugin object
168
- * @param providerName - the name of the plugin
178
+ * @param providerName - the name of the provider to be removed
169
179
  */
170
- public removePluggable(providerName) {
180
+ public removePluggable(providerName: string): void {
171
181
  let idx = 0;
172
182
  while (idx < this._pluggables.length) {
173
183
  if (this._pluggables[idx].getProviderName() === providerName) {
@@ -201,6 +211,7 @@ export class AnalyticsClass {
201
211
 
202
212
  /**
203
213
  * Record Session start
214
+ * @param [provider] - name of the provider.
204
215
  * @return - A promise which resolves if buffer doesn't overflow
205
216
  */
206
217
  public async startSession(provider?: string) {
@@ -210,6 +221,7 @@ export class AnalyticsClass {
210
221
 
211
222
  /**
212
223
  * Record Session stop
224
+ * @param [provider] - name of the provider.
213
225
  * @return - A promise which resolves if buffer doesn't overflow
214
226
  */
215
227
  public async stopSession(provider?: string) {
@@ -219,14 +231,26 @@ export class AnalyticsClass {
219
231
 
220
232
  /**
221
233
  * 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
234
+ * @param event - An object with the name of the event, attributes of the event and event metrics.
235
+ * @param [provider] - name of the provider.
236
+ */
237
+ public async record(event: AnalyticsEvent, provider?: string);
238
+ /**
239
+ * Record one analytic event and send it to Pinpoint
240
+ * @deprecated Use the new syntax and pass in the event as an object instead.
241
+ * @param eventName - The name of the event
242
+ * @param [attributes] - Attributes of the event
243
+ * @param [metrics] - Event metrics
225
244
  * @return - A promise which resolves if buffer doesn't overflow
226
245
  */
227
246
  public async record(
228
- event: string | object,
229
- provider?,
247
+ eventName: string,
248
+ attributes?: EventAttributes,
249
+ metrics?: EventMetrics
250
+ );
251
+ public async record(
252
+ event: string | AnalyticsEvent,
253
+ providerOrAttributes?: string | EventAttributes,
230
254
  metrics?: EventMetrics
231
255
  ) {
232
256
  let params = null;
@@ -235,24 +259,27 @@ export class AnalyticsClass {
235
259
  params = {
236
260
  event: {
237
261
  name: event,
238
- attributes: provider,
262
+ attributes: providerOrAttributes,
239
263
  metrics,
240
264
  },
241
265
  provider: 'AWSPinpoint',
242
266
  };
243
267
  } else {
244
- params = { event, provider };
268
+ params = { event, provider: providerOrAttributes };
245
269
  }
246
270
  return this._sendEvent(params);
247
271
  }
248
272
 
249
- public async updateEndpoint(attrs, provider?) {
273
+ public async updateEndpoint(
274
+ attrs: { [key: string]: any },
275
+ provider?: string
276
+ ) {
250
277
  const event = { ...attrs, name: '_update_endpoint' };
251
278
 
252
279
  return this.record(event, provider);
253
280
  }
254
281
 
255
- private _sendEvent(params) {
282
+ private _sendEvent(params: { event: AnalyticsEvent; provider?: string }) {
256
283
  if (this._disabled) {
257
284
  logger.debug('Analytics has been disabled');
258
285
  return Promise.resolve();
@@ -261,7 +288,7 @@ export class AnalyticsClass {
261
288
  const provider = params.provider ? params.provider : 'AWSPinpoint';
262
289
 
263
290
  return new Promise((resolve, reject) => {
264
- this._pluggables.forEach((pluggable) => {
291
+ this._pluggables.forEach(pluggable => {
265
292
  if (pluggable.getProviderName() === provider) {
266
293
  pluggable.record(params, { resolve, reject });
267
294
  }
@@ -269,7 +296,20 @@ export class AnalyticsClass {
269
296
  });
270
297
  }
271
298
 
272
- public autoTrack(trackerType, opts) {
299
+ /**
300
+ * Enable or disable auto tracking
301
+ * @param trackerType - The type of tracker to activate.
302
+ * @param [opts] - Auto tracking options.
303
+ */
304
+ public autoTrack(trackerType: 'session', opts: AutoTrackSessionOpts);
305
+ public autoTrack(trackerType: 'pageView', opts: AutoTrackPageViewOpts);
306
+ public autoTrack(trackerType: 'event', opts: AutoTrackEventOpts);
307
+ // ensures backwards compatibility for non-pinpoint provider users
308
+ public autoTrack(
309
+ trackerType: TrackerTypes,
310
+ opts: { provider: string; [key: string]: any }
311
+ );
312
+ public autoTrack(trackerType: TrackerTypes, opts: { [key: string]: any }) {
273
313
  if (!trackers[trackerType]) {
274
314
  logger.debug('invalid tracker type');
275
315
  return;
@@ -295,7 +335,7 @@ export class AnalyticsClass {
295
335
  let endpointUpdated = false;
296
336
  let authConfigured = false;
297
337
  let analyticsConfigured = false;
298
- const listener = (capsule) => {
338
+ const listener = capsule => {
299
339
  const { channel, payload } = capsule;
300
340
  logger.debug('on hub capsule ' + channel, payload);
301
341
 
@@ -314,7 +354,7 @@ const listener = (capsule) => {
314
354
  }
315
355
  };
316
356
 
317
- const storageEvent = (payload) => {
357
+ const storageEvent = payload => {
318
358
  const {
319
359
  data: { attrs, metrics },
320
360
  } = payload;
@@ -327,19 +367,19 @@ const storageEvent = (payload) => {
327
367
  attributes: attrs,
328
368
  metrics,
329
369
  })
330
- .catch((e) => {
370
+ .catch(e => {
331
371
  logger.debug('Failed to send the storage event automatically', e);
332
372
  });
333
373
  }
334
374
  };
335
375
 
336
- const authEvent = (payload) => {
376
+ const authEvent = payload => {
337
377
  const { event } = payload;
338
378
  if (!event) {
339
379
  return;
340
380
  }
341
381
 
342
- const recordAuthEvent = async (eventName) => {
382
+ const recordAuthEvent = async eventName => {
343
383
  if (authConfigured && analyticsConfigured) {
344
384
  try {
345
385
  return await _instance.record({ name: `_userauth.${eventName}` });
@@ -370,7 +410,7 @@ const authEvent = (payload) => {
370
410
  }
371
411
  };
372
412
 
373
- const analyticsEvent = (payload) => {
413
+ const analyticsEvent = payload => {
374
414
  const { event } = payload;
375
415
  if (!event) return;
376
416
 
@@ -387,7 +427,7 @@ const analyticsEvent = (payload) => {
387
427
  const sendEvents = () => {
388
428
  const config = _instance.configure();
389
429
  if (!endpointUpdated && config['autoSessionRecord']) {
390
- _instance.updateEndpoint({ immediate: true }).catch((e) => {
430
+ _instance.updateEndpoint({ immediate: true }).catch(e => {
391
431
  logger.debug('Failed to update the endpoint', e);
392
432
  });
393
433
  endpointUpdated = true;
@@ -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,37 @@ 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
+ }