@aws-amplify/analytics 5.1.12 → 5.1.13-cloud-logging.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,13 @@ 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
+ AutoTrackOpts,
28
+ } from './types';
23
29
  import { PageViewTracker, EventTracker, SessionTracker } from './trackers';
24
30
 
25
31
  const logger = new Logger('AnalyticsClass');
@@ -45,6 +51,8 @@ const trackers = {
45
51
  session: SessionTracker,
46
52
  };
47
53
 
54
+ type TrackerTypes = keyof typeof trackers;
55
+ type Trackers = typeof trackers[TrackerTypes];
48
56
  let _instance = null;
49
57
 
50
58
  /**
@@ -53,8 +61,8 @@ let _instance = null;
53
61
  export class AnalyticsClass {
54
62
  private _config;
55
63
  private _pluggables: AnalyticsProvider[];
56
- private _disabled;
57
- private _trackers;
64
+ private _disabled: boolean;
65
+ private _trackers: Trackers;
58
66
 
59
67
  /**
60
68
  * Initialize Analtyics
@@ -64,7 +72,6 @@ export class AnalyticsClass {
64
72
  this._config = {};
65
73
  this._pluggables = [];
66
74
  this._disabled = false;
67
- this._trackers = {};
68
75
  _instance = this;
69
76
 
70
77
  this.record = this.record.bind(this);
@@ -100,7 +107,7 @@ export class AnalyticsClass {
100
107
  this._config['autoSessionRecord'] = true;
101
108
  }
102
109
 
103
- this._pluggables.forEach((pluggable) => {
110
+ this._pluggables.forEach(pluggable => {
104
111
  // for backward compatibility
105
112
  const providerConfig =
106
113
  pluggable.getProviderName() === 'AWSPinpoint' &&
@@ -130,7 +137,7 @@ export class AnalyticsClass {
130
137
 
131
138
  /**
132
139
  * add plugin into Analytics category
133
- * @param {Object} pluggable - an instance of the plugin
140
+ * @param pluggable - an instance of the plugin
134
141
  */
135
142
  public addPluggable(pluggable: AnalyticsProvider) {
136
143
  if (pluggable && pluggable.getCategory() === 'Analytics') {
@@ -149,9 +156,9 @@ export class AnalyticsClass {
149
156
 
150
157
  /**
151
158
  * Get the plugin object
152
- * @param providerName - the name of the plugin
159
+ * @param providerName - the name of the provider to be removed
153
160
  */
154
- public getPluggable(providerName) {
161
+ public getPluggable(providerName: string): AnalyticsProvider {
155
162
  for (let i = 0; i < this._pluggables.length; i += 1) {
156
163
  const pluggable = this._pluggables[i];
157
164
  if (pluggable.getProviderName() === providerName) {
@@ -165,9 +172,9 @@ export class AnalyticsClass {
165
172
 
166
173
  /**
167
174
  * Remove the plugin object
168
- * @param providerName - the name of the plugin
175
+ * @param providerName - the name of the provider to be removed
169
176
  */
170
- public removePluggable(providerName) {
177
+ public removePluggable(providerName: string): void {
171
178
  let idx = 0;
172
179
  while (idx < this._pluggables.length) {
173
180
  if (this._pluggables[idx].getProviderName() === providerName) {
@@ -201,6 +208,7 @@ export class AnalyticsClass {
201
208
 
202
209
  /**
203
210
  * Record Session start
211
+ * @param [provider] - name of the provider.
204
212
  * @return - A promise which resolves if buffer doesn't overflow
205
213
  */
206
214
  public async startSession(provider?: string) {
@@ -210,6 +218,7 @@ export class AnalyticsClass {
210
218
 
211
219
  /**
212
220
  * Record Session stop
221
+ * @param [provider] - name of the provider.
213
222
  * @return - A promise which resolves if buffer doesn't overflow
214
223
  */
215
224
  public async stopSession(provider?: string) {
@@ -219,14 +228,26 @@ export class AnalyticsClass {
219
228
 
220
229
  /**
221
230
  * 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
231
+ * @param event - An object with the name of the event, attributes of the event and event metrics.
232
+ * @param [provider] - name of the provider.
233
+ */
234
+ public async record(event: AnalyticsEvent, provider?: string);
235
+ /**
236
+ * Record one analytic event and send it to Pinpoint
237
+ * @deprecated Use the new syntax and pass in the event as an object instead.
238
+ * @param eventName - The name of the event
239
+ * @param [attributes] - Attributes of the event
240
+ * @param [metrics] - Event metrics
225
241
  * @return - A promise which resolves if buffer doesn't overflow
226
242
  */
227
243
  public async record(
228
- event: string | object,
229
- provider?,
244
+ eventName: string,
245
+ attributes?: EventAttributes,
246
+ metrics?: EventMetrics
247
+ );
248
+ public async record(
249
+ event: string | AnalyticsEvent,
250
+ providerOrAttributes?: string | EventAttributes,
230
251
  metrics?: EventMetrics
231
252
  ) {
232
253
  let params = null;
@@ -235,24 +256,27 @@ export class AnalyticsClass {
235
256
  params = {
236
257
  event: {
237
258
  name: event,
238
- attributes: provider,
259
+ attributes: providerOrAttributes,
239
260
  metrics,
240
261
  },
241
262
  provider: 'AWSPinpoint',
242
263
  };
243
264
  } else {
244
- params = { event, provider };
265
+ params = { event, provider: providerOrAttributes };
245
266
  }
246
267
  return this._sendEvent(params);
247
268
  }
248
269
 
249
- public async updateEndpoint(attrs, provider?) {
270
+ public async updateEndpoint(
271
+ attrs: { [key: string]: any },
272
+ provider?: string
273
+ ) {
250
274
  const event = { ...attrs, name: '_update_endpoint' };
251
275
 
252
276
  return this.record(event, provider);
253
277
  }
254
278
 
255
- private _sendEvent(params) {
279
+ private _sendEvent(params: { event: AnalyticsEvent; provider?: string }) {
256
280
  if (this._disabled) {
257
281
  logger.debug('Analytics has been disabled');
258
282
  return Promise.resolve();
@@ -261,7 +285,7 @@ export class AnalyticsClass {
261
285
  const provider = params.provider ? params.provider : 'AWSPinpoint';
262
286
 
263
287
  return new Promise((resolve, reject) => {
264
- this._pluggables.forEach((pluggable) => {
288
+ this._pluggables.forEach(pluggable => {
265
289
  if (pluggable.getProviderName() === provider) {
266
290
  pluggable.record(params, { resolve, reject });
267
291
  }
@@ -269,7 +293,12 @@ export class AnalyticsClass {
269
293
  });
270
294
  }
271
295
 
272
- public autoTrack(trackerType, opts) {
296
+ /**
297
+ * Enable or disable auto tracking
298
+ * @param trackerType - The type of tracker to activate.
299
+ * @param [opts] - Auto tracking options.
300
+ */
301
+ public autoTrack(trackerType: TrackerTypes, opts: AutoTrackOpts) {
273
302
  if (!trackers[trackerType]) {
274
303
  logger.debug('invalid tracker type');
275
304
  return;
@@ -295,7 +324,7 @@ export class AnalyticsClass {
295
324
  let endpointUpdated = false;
296
325
  let authConfigured = false;
297
326
  let analyticsConfigured = false;
298
- const listener = (capsule) => {
327
+ const listener = capsule => {
299
328
  const { channel, payload } = capsule;
300
329
  logger.debug('on hub capsule ' + channel, payload);
301
330
 
@@ -314,7 +343,7 @@ const listener = (capsule) => {
314
343
  }
315
344
  };
316
345
 
317
- const storageEvent = (payload) => {
346
+ const storageEvent = payload => {
318
347
  const {
319
348
  data: { attrs, metrics },
320
349
  } = payload;
@@ -327,19 +356,19 @@ const storageEvent = (payload) => {
327
356
  attributes: attrs,
328
357
  metrics,
329
358
  })
330
- .catch((e) => {
359
+ .catch(e => {
331
360
  logger.debug('Failed to send the storage event automatically', e);
332
361
  });
333
362
  }
334
363
  };
335
364
 
336
- const authEvent = (payload) => {
365
+ const authEvent = payload => {
337
366
  const { event } = payload;
338
367
  if (!event) {
339
368
  return;
340
369
  }
341
370
 
342
- const recordAuthEvent = async (eventName) => {
371
+ const recordAuthEvent = async eventName => {
343
372
  if (authConfigured && analyticsConfigured) {
344
373
  try {
345
374
  return await _instance.record({ name: `_userauth.${eventName}` });
@@ -370,7 +399,7 @@ const authEvent = (payload) => {
370
399
  }
371
400
  };
372
401
 
373
- const analyticsEvent = (payload) => {
402
+ const analyticsEvent = payload => {
374
403
  const { event } = payload;
375
404
  if (!event) return;
376
405
 
@@ -387,7 +416,7 @@ const analyticsEvent = (payload) => {
387
416
  const sendEvents = () => {
388
417
  const config = _instance.configure();
389
418
  if (!endpointUpdated && config['autoSessionRecord']) {
390
- _instance.updateEndpoint({ immediate: true }).catch((e) => {
419
+ _instance.updateEndpoint({ immediate: true }).catch(e => {
391
420
  logger.debug('Failed to update the endpoint', e);
392
421
  });
393
422
  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,16 @@ export interface SessionTrackOpts {
59
58
  | (() => EventAttributes | Promise<EventAttributes>);
60
59
  provider?: string;
61
60
  }
61
+
62
+ export interface AutoTrackOpts {
63
+ enable: boolean;
64
+ attributes?: EventAttributes;
65
+ provider?: string;
66
+ }
67
+
68
+ export interface AnalyticsEvent {
69
+ name: string;
70
+ attributes?: EventAttributes;
71
+ metrics?: EventMetrics;
72
+ immediate?: boolean;
73
+ }