Skip to content

Commit

Permalink
fix(@aws-amplify/analytics)Update EventAttributes type (#2581)
Browse files Browse the repository at this point in the history
* Update EventAttributes type

Replacing #735 (inactive)

* Added comment and updated type to allow promises
  • Loading branch information
elorzafe authored Jan 22, 2019
1 parent c306043 commit 2309e6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
32 changes: 18 additions & 14 deletions packages/analytics/src/trackers/SessionTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -71,16 +71,17 @@ 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
);

if (document[this._hidden]) {
this._tracker(
{
{
name: '_session.stop',
attributes
},
Expand All @@ -90,7 +91,7 @@ export default class SessionTracker {
});
} else {
this._tracker(
{
{
name: '_session.start',
attributes
},
Expand All @@ -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
Expand All @@ -121,6 +124,7 @@ export default class SessionTracker {
logger.debug('record session stop event failed.', e);
});
});

}

// to keep configure a synchronized function
Expand All @@ -131,16 +135,16 @@ 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(
{},
customAttrs
);

this._tracker(
{
{
name: '_session.start',
attributes
},
Expand Down
8 changes: 4 additions & 4 deletions packages/analytics/src/types/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface AnalyticsOptions {
}

export interface EventAttributes {
[key: string]: any;
[key: string]: string;
}

export interface EventMetrics {
Expand All @@ -35,7 +35,7 @@ export interface pageViewTrackOpts {
type?: string
eventName?: string,
provider?: string,
attributes?: EventAttributes | (()=> EventAttributes),
attributes?: EventAttributes | (()=> EventAttributes | Promise<EventAttributes>),
getUrl?: (() => string)
}

Expand All @@ -44,11 +44,11 @@ export interface EventTrackOpts {
events?: Array<string>,
selectorPrefix?: string,
provider?: string,
attributes?: EventAttributes | (()=> EventAttributes)
attributes?: EventAttributes | (()=> EventAttributes | Promise<EventAttributes>)
}

export interface SessionTrackOpts {
enable: boolean,
attributes?: EventAttributes | (()=> EventAttributes),
attributes?: EventAttributes | (()=> EventAttributes | Promise<EventAttributes>),
provider?: string,
}

0 comments on commit 2309e6e

Please sign in to comment.