From 2309e6e6e8731ad1b5b850d3342cd298765ec2d7 Mon Sep 17 00:00:00 2001 From: elorzafe Date: Tue, 22 Jan 2019 10:48:49 -0800 Subject: [PATCH] fix(@aws-amplify/analytics)Update EventAttributes type (#2581) * Update EventAttributes type Replacing #735 (inactive) * Added comment and updated type to allow promises --- .../analytics/src/trackers/SessionTracker.ts | 32 +++++++++++-------- packages/analytics/src/types/Analytics.ts | 8 ++--- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/packages/analytics/src/trackers/SessionTracker.ts b/packages/analytics/src/trackers/SessionTracker.ts index 2ab7cba95da..3a6a2b0e0a4 100644 --- a/packages/analytics/src/trackers/SessionTracker.ts +++ b/packages/analytics/src/trackers/SessionTracker.ts @@ -11,7 +11,7 @@ * and limitations under the License. */ - // the session tracker for web +// the session tracker for web import { ConsoleLogger as Logger, Hub, JS } from '@aws-amplify/core'; import { SessionTrackOpts } from '../types'; @@ -36,7 +36,7 @@ export default class SessionTracker { constructor(tracker, opts) { this._config = Object.assign({}, defaultOpts, opts); this._tracker = tracker; - + this._hasEnabled = false; this._trackFunc = this._trackFunc.bind(this); this._trackBeforeUnload = this._trackBeforeUnload.bind(this); @@ -71,8 +71,9 @@ export default class SessionTracker { } private async _trackFunc() { - const customAttrs = typeof this._config.attributes === 'function'? + const customAttrs = typeof this._config.attributes === 'function' ? await this._config.attributes() : this._config.attributes; + const attributes = Object.assign( {}, customAttrs @@ -80,7 +81,7 @@ export default class SessionTracker { if (document[this._hidden]) { this._tracker( - { + { name: '_session.stop', attributes }, @@ -90,7 +91,7 @@ export default class SessionTracker { }); } else { this._tracker( - { + { name: '_session.start', attributes }, @@ -102,16 +103,18 @@ export default class SessionTracker { } private _trackBeforeUnload() { - const getCustomAttrs = typeof this._config.attributes === 'function'? - this._config.attributes() : Promise.resolve(this._config.attributes); - - getCustomAttrs.then((customAttrs) => { + // before unload callback cannot be async => https://github.com/aws-amplify/amplify-js/issues/2088 + const customAttrs = typeof this._config.attributes === 'function' ? + Promise.resolve(this._config.attributes()) : Promise.resolve(this._config.attributes); + + customAttrs.then(custom => { const attributes = Object.assign( {}, - customAttrs + custom ); + this._tracker( - { + { name: '_session.stop', attributes, immediate: true @@ -121,6 +124,7 @@ export default class SessionTracker { logger.debug('record session stop event failed.', e); }); }); + } // to keep configure a synchronized function @@ -131,8 +135,8 @@ export default class SessionTracker { } else { initialEventSent = true; } - - const customAttrs = typeof this._config.attributes === 'function'? + + const customAttrs = typeof this._config.attributes === 'function' ? await this._config.attributes() : this._config.attributes; const attributes = Object.assign( {}, @@ -140,7 +144,7 @@ export default class SessionTracker { ); this._tracker( - { + { name: '_session.start', attributes }, diff --git a/packages/analytics/src/types/Analytics.ts b/packages/analytics/src/types/Analytics.ts index 887f0168a32..bacae23a1cc 100644 --- a/packages/analytics/src/types/Analytics.ts +++ b/packages/analytics/src/types/Analytics.ts @@ -23,7 +23,7 @@ export interface AnalyticsOptions { } export interface EventAttributes { - [key: string]: any; + [key: string]: string; } export interface EventMetrics { @@ -35,7 +35,7 @@ export interface pageViewTrackOpts { type?: string eventName?: string, provider?: string, - attributes?: EventAttributes | (()=> EventAttributes), + attributes?: EventAttributes | (()=> EventAttributes | Promise), getUrl?: (() => string) } @@ -44,11 +44,11 @@ export interface EventTrackOpts { events?: Array, selectorPrefix?: string, provider?: string, - attributes?: EventAttributes | (()=> EventAttributes) + attributes?: EventAttributes | (()=> EventAttributes | Promise) } export interface SessionTrackOpts { enable: boolean, - attributes?: EventAttributes | (()=> EventAttributes), + attributes?: EventAttributes | (()=> EventAttributes | Promise), provider?: string, }