Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EventAttributes type #2581

Merged
merged 7 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}