@aws-amplify/analytics 7.0.1-api-v6-models.8035dfc.0 → 7.0.1-api-v6-models.3eb7748.0
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/lib/errors/validation.d.ts +2 -0
- package/lib/errors/validation.js +8 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +2 -1
- package/lib/providers/kinesis/apis/flushEvents.js +2 -1
- package/lib/providers/kinesis/apis/record.js +2 -1
- package/lib/providers/kinesis/types/buffer.d.ts +2 -2
- package/lib/providers/kinesis-firehose/apis/flushEvents.js +2 -1
- package/lib/providers/kinesis-firehose/apis/record.js +2 -1
- package/lib/providers/kinesis-firehose/types/buffer.d.ts +2 -2
- package/lib/providers/personalize/apis/flushEvents.js +2 -1
- package/lib/providers/personalize/apis/record.js +61 -46
- package/lib/providers/personalize/types/buffer.d.ts +2 -2
- package/lib/providers/personalize/utils/autoTrackMedia.js +2 -1
- package/lib/providers/personalize/utils/cachedSession.d.ts +2 -2
- package/lib/providers/personalize/utils/cachedSession.js +23 -14
- package/lib/providers/pinpoint/apis/configureAutoTrack.d.ts +15 -0
- package/lib/providers/pinpoint/apis/configureAutoTrack.js +35 -0
- package/lib/providers/pinpoint/apis/flushEvents.js +2 -1
- package/lib/providers/pinpoint/apis/identifyUser.js +4 -5
- package/lib/providers/pinpoint/apis/index.d.ts +1 -0
- package/lib/providers/pinpoint/apis/index.js +3 -1
- package/lib/providers/pinpoint/apis/record.js +1 -1
- package/lib/providers/pinpoint/index.d.ts +2 -2
- package/lib/providers/pinpoint/index.js +2 -1
- package/lib/providers/pinpoint/types/index.d.ts +1 -2
- package/lib/providers/pinpoint/types/index.js +0 -3
- package/lib/providers/pinpoint/types/inputs.d.ts +5 -1
- package/lib/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
- package/lib/trackers/EventTracker.d.ts +10 -0
- package/lib/trackers/EventTracker.js +104 -0
- package/lib/trackers/PageViewTracker.d.ts +18 -0
- package/lib/trackers/PageViewTracker.js +116 -0
- package/lib/trackers/SessionTracker.d.ts +13 -0
- package/lib/trackers/SessionTracker.js +73 -0
- package/lib/trackers/SessionTracker.native.d.ts +13 -0
- package/lib/trackers/SessionTracker.native.js +73 -0
- package/lib/trackers/index.d.ts +3 -0
- package/lib/trackers/index.js +11 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/index.d.ts +1 -2
- package/lib/types/inputs.d.ts +17 -3
- package/lib/types/options.d.ts +1 -1
- package/lib/types/trackers.d.ts +22 -0
- package/lib/utils/eventBuffer/EventBuffer.js +2 -2
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +5 -1
- package/lib/utils/resolveCredentials.d.ts +1 -6
- package/lib/utils/trackerConfigHelpers.d.ts +5 -0
- package/lib/utils/trackerConfigHelpers.js +15 -0
- package/lib/utils/trackerConfigHelpers.native.d.ts +5 -0
- package/lib/utils/trackerConfigHelpers.native.js +14 -0
- package/lib/utils/trackerHelpers.d.ts +9 -0
- package/lib/utils/trackerHelpers.js +44 -0
- package/lib-esm/errors/validation.d.ts +2 -0
- package/lib-esm/errors/validation.js +8 -0
- package/lib-esm/index.d.ts +1 -1
- package/lib-esm/index.js +1 -1
- package/lib-esm/providers/kinesis/apis/flushEvents.js +2 -1
- package/lib-esm/providers/kinesis/apis/record.js +2 -1
- package/lib-esm/providers/kinesis/types/buffer.d.ts +2 -2
- package/lib-esm/providers/kinesis-firehose/apis/flushEvents.js +2 -1
- package/lib-esm/providers/kinesis-firehose/apis/record.js +3 -2
- package/lib-esm/providers/kinesis-firehose/types/buffer.d.ts +2 -2
- package/lib-esm/providers/personalize/apis/flushEvents.js +2 -1
- package/lib-esm/providers/personalize/apis/record.js +61 -46
- package/lib-esm/providers/personalize/types/buffer.d.ts +2 -2
- package/lib-esm/providers/personalize/utils/autoTrackMedia.js +2 -1
- package/lib-esm/providers/personalize/utils/cachedSession.d.ts +2 -2
- package/lib-esm/providers/personalize/utils/cachedSession.js +24 -15
- package/lib-esm/providers/pinpoint/apis/configureAutoTrack.d.ts +15 -0
- package/lib-esm/providers/pinpoint/apis/configureAutoTrack.js +31 -0
- package/lib-esm/providers/pinpoint/apis/flushEvents.js +2 -1
- package/lib-esm/providers/pinpoint/apis/identifyUser.js +5 -6
- package/lib-esm/providers/pinpoint/apis/index.d.ts +1 -0
- package/lib-esm/providers/pinpoint/apis/index.js +1 -0
- package/lib-esm/providers/pinpoint/apis/record.js +3 -3
- package/lib-esm/providers/pinpoint/index.d.ts +2 -2
- package/lib-esm/providers/pinpoint/index.js +1 -1
- package/lib-esm/providers/pinpoint/types/index.d.ts +1 -2
- package/lib-esm/providers/pinpoint/types/index.js +1 -1
- package/lib-esm/providers/pinpoint/types/inputs.d.ts +5 -1
- package/lib-esm/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
- package/lib-esm/trackers/EventTracker.d.ts +10 -0
- package/lib-esm/trackers/EventTracker.js +101 -0
- package/lib-esm/trackers/PageViewTracker.d.ts +18 -0
- package/lib-esm/trackers/PageViewTracker.js +113 -0
- package/lib-esm/trackers/SessionTracker.d.ts +13 -0
- package/lib-esm/trackers/SessionTracker.js +70 -0
- package/lib-esm/trackers/SessionTracker.native.d.ts +13 -0
- package/lib-esm/trackers/SessionTracker.native.js +70 -0
- package/lib-esm/trackers/index.d.ts +3 -0
- package/lib-esm/trackers/index.js +5 -0
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/index.d.ts +1 -2
- package/lib-esm/types/inputs.d.ts +17 -3
- package/lib-esm/types/options.d.ts +1 -1
- package/lib-esm/types/trackers.d.ts +22 -0
- package/lib-esm/utils/eventBuffer/EventBuffer.js +1 -1
- package/lib-esm/utils/index.d.ts +2 -0
- package/lib-esm/utils/index.js +2 -0
- package/lib-esm/utils/resolveCredentials.d.ts +1 -6
- package/lib-esm/utils/trackerConfigHelpers.d.ts +5 -0
- package/lib-esm/utils/trackerConfigHelpers.js +11 -0
- package/lib-esm/utils/trackerConfigHelpers.native.d.ts +5 -0
- package/lib-esm/utils/trackerConfigHelpers.native.js +10 -0
- package/lib-esm/utils/trackerHelpers.d.ts +9 -0
- package/lib-esm/utils/trackerHelpers.js +40 -0
- package/package.json +6 -7
- package/src/errors/validation.ts +8 -0
- package/src/index.ts +3 -1
- package/src/providers/kinesis/apis/flushEvents.ts +2 -4
- package/src/providers/kinesis/apis/record.ts +2 -4
- package/src/providers/kinesis/types/buffer.ts +2 -2
- package/src/providers/kinesis-firehose/apis/flushEvents.ts +2 -4
- package/src/providers/kinesis-firehose/apis/record.ts +3 -5
- package/src/providers/kinesis-firehose/types/buffer.ts +2 -2
- package/src/providers/personalize/apis/flushEvents.ts +2 -4
- package/src/providers/personalize/apis/record.ts +5 -7
- package/src/providers/personalize/types/buffer.ts +2 -2
- package/src/providers/personalize/utils/autoTrackMedia.ts +2 -1
- package/src/providers/personalize/utils/cachedSession.ts +8 -7
- package/src/providers/pinpoint/apis/configureAutoTrack.ts +49 -0
- package/src/providers/pinpoint/apis/flushEvents.ts +2 -4
- package/src/providers/pinpoint/apis/identifyUser.ts +6 -3
- package/src/providers/pinpoint/apis/index.ts +1 -0
- package/src/providers/pinpoint/apis/record.ts +2 -3
- package/src/providers/pinpoint/index.ts +6 -2
- package/src/providers/pinpoint/types/index.ts +5 -2
- package/src/providers/pinpoint/types/inputs.ts +9 -1
- package/src/trackers/EventTracker.ts +135 -0
- package/src/trackers/PageViewTracker.ts +160 -0
- package/src/trackers/SessionTracker.native.ts +106 -0
- package/src/trackers/SessionTracker.ts +106 -0
- package/src/trackers/index.ts +6 -0
- package/src/types/index.ts +4 -13
- package/src/types/inputs.ts +26 -3
- package/src/types/options.ts +1 -1
- package/src/types/trackers.ts +35 -0
- package/src/utils/eventBuffer/EventBuffer.ts +1 -1
- package/src/utils/index.ts +2 -0
- package/src/utils/trackerConfigHelpers.native.ts +18 -0
- package/src/utils/trackerConfigHelpers.ts +19 -0
- package/src/utils/trackerHelpers.ts +57 -0
- package/lib/providers/pinpoint/types/errors.d.ts +0 -9
- package/lib/providers/pinpoint/types/errors.js +0 -15
- package/lib/types/analytics.d.ts +0 -50
- package/lib-esm/providers/pinpoint/types/errors.d.ts +0 -9
- package/lib-esm/providers/pinpoint/types/errors.js +0 -12
- package/lib-esm/types/analytics.d.ts +0 -50
- package/src/providers/pinpoint/types/errors.ts +0 -12
- package/src/types/analytics.ts +0 -70
- /package/lib/types/{analytics.js → trackers.js} +0 -0
- /package/lib-esm/types/{analytics.js → trackers.js} +0 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
EventTrackingOptions,
|
|
6
|
+
DOMEvent,
|
|
7
|
+
TrackerEventRecorder,
|
|
8
|
+
TrackerInterface,
|
|
9
|
+
} from '../types/trackers';
|
|
10
|
+
import { ConsoleLogger } from '@aws-amplify/core';
|
|
11
|
+
import { isBrowser } from '@aws-amplify/core/internals/utils';
|
|
12
|
+
|
|
13
|
+
const DEFAULT_EVENTS = ['click'] as DOMEvent[];
|
|
14
|
+
const DEFAULT_SELECTOR_PREFIX = 'data-amplify-analytics-';
|
|
15
|
+
const DEFAULT_EVENT_NAME = 'event'; // Default event name as sent to the analytics provider
|
|
16
|
+
|
|
17
|
+
const logger = new ConsoleLogger('EventTracker');
|
|
18
|
+
|
|
19
|
+
export class EventTracker implements TrackerInterface {
|
|
20
|
+
private trackerActive: boolean;
|
|
21
|
+
private options: EventTrackingOptions;
|
|
22
|
+
private eventRecorder: TrackerEventRecorder;
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
eventRecorder: TrackerEventRecorder,
|
|
26
|
+
options?: EventTrackingOptions
|
|
27
|
+
) {
|
|
28
|
+
this.options = {};
|
|
29
|
+
this.trackerActive = false;
|
|
30
|
+
this.eventRecorder = eventRecorder;
|
|
31
|
+
this.handleDocEvent = this.handleDocEvent.bind(this);
|
|
32
|
+
|
|
33
|
+
this.configure(eventRecorder, options);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public configure(
|
|
37
|
+
eventRecorder: TrackerEventRecorder,
|
|
38
|
+
options?: EventTrackingOptions
|
|
39
|
+
) {
|
|
40
|
+
this.eventRecorder = eventRecorder;
|
|
41
|
+
|
|
42
|
+
// Clean up any existing listeners
|
|
43
|
+
this.cleanup();
|
|
44
|
+
|
|
45
|
+
// Apply defaults
|
|
46
|
+
this.options = {
|
|
47
|
+
attributes: options?.attributes ?? undefined,
|
|
48
|
+
events: options?.events ?? DEFAULT_EVENTS,
|
|
49
|
+
selectorPrefix: options?.selectorPrefix ?? DEFAULT_SELECTOR_PREFIX,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Register event listeners
|
|
53
|
+
if (isBrowser()) {
|
|
54
|
+
this.options.events?.forEach(targetEvent => {
|
|
55
|
+
document.addEventListener(targetEvent, this.handleDocEvent, {
|
|
56
|
+
capture: true,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
this.trackerActive = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public cleanup() {
|
|
65
|
+
// No-op if document listener is not active
|
|
66
|
+
if (!this.trackerActive) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Clean up event listeners
|
|
71
|
+
this.options.events?.forEach(targetEvent => {
|
|
72
|
+
document.removeEventListener(targetEvent, this.handleDocEvent, {
|
|
73
|
+
capture: true,
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private handleDocEvent(event: Event) {
|
|
79
|
+
/**
|
|
80
|
+
* Example DOM element:
|
|
81
|
+
*
|
|
82
|
+
* ```
|
|
83
|
+
* <button
|
|
84
|
+
* data-amplify-analytics-on="click"
|
|
85
|
+
* data-amplify-analytics-name="click"
|
|
86
|
+
* data-amplify-analytics-attrs="attr1:attr1_value,attr2:attr2_value"
|
|
87
|
+
* />
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
const triggerSelector = `[${this.options.selectorPrefix}on]`;
|
|
91
|
+
const attrSelector = `${this.options.selectorPrefix}attrs`;
|
|
92
|
+
const eventNameSelector = `${this.options.selectorPrefix}name`;
|
|
93
|
+
const eventSource = event.target;
|
|
94
|
+
|
|
95
|
+
// Validate that the triggering event type is being tracked
|
|
96
|
+
if (!this.options.events?.includes(event.type as DOMEvent)) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (eventSource instanceof HTMLElement) {
|
|
101
|
+
const target = eventSource.closest(triggerSelector);
|
|
102
|
+
|
|
103
|
+
if (target) {
|
|
104
|
+
// Parse event name from the element
|
|
105
|
+
const eventName =
|
|
106
|
+
target.getAttribute(eventNameSelector) || DEFAULT_EVENT_NAME;
|
|
107
|
+
|
|
108
|
+
// Parse attributes from the element
|
|
109
|
+
const elementAttributes: Record<string, string> = {};
|
|
110
|
+
const rawElementAttributes = target.getAttribute(attrSelector);
|
|
111
|
+
rawElementAttributes?.split(/\s*,\s*/).forEach(attr => {
|
|
112
|
+
const tmp = attr.trim().split(/\s*:\s*/);
|
|
113
|
+
elementAttributes[tmp[0]] = tmp[1];
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Assemble final list of attributes
|
|
117
|
+
const attributes = Object.assign(
|
|
118
|
+
{
|
|
119
|
+
type: event.type,
|
|
120
|
+
target: `${target.localName} with id ${target.id}`,
|
|
121
|
+
},
|
|
122
|
+
this.options.attributes,
|
|
123
|
+
elementAttributes
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
logger.debug('Recording automatically tracked DOM event', {
|
|
127
|
+
eventName,
|
|
128
|
+
attributes,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
this.eventRecorder(eventName, attributes);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
PageViewTrackingOptions,
|
|
6
|
+
TrackerEventRecorder,
|
|
7
|
+
TrackerInterface,
|
|
8
|
+
} from '../types/trackers';
|
|
9
|
+
import { ConsoleLogger } from '@aws-amplify/core';
|
|
10
|
+
import { isBrowser } from '@aws-amplify/core/internals/utils';
|
|
11
|
+
|
|
12
|
+
const logger = new ConsoleLogger('PageViewTracker');
|
|
13
|
+
|
|
14
|
+
const DEFAULT_EVENT_NAME = 'pageView';
|
|
15
|
+
const DEFAULT_APP_TYPE = 'singlePage';
|
|
16
|
+
const DEFAULT_URL_PROVIDER = () => {
|
|
17
|
+
return window.location.origin + window.location.pathname;
|
|
18
|
+
};
|
|
19
|
+
const PREV_URL_STORAGE_KEY = 'aws-amplify-analytics-prevUrl';
|
|
20
|
+
|
|
21
|
+
export class PageViewTracker implements TrackerInterface {
|
|
22
|
+
private trackerActive: boolean;
|
|
23
|
+
private options: PageViewTrackingOptions;
|
|
24
|
+
private eventRecorder: TrackerEventRecorder;
|
|
25
|
+
|
|
26
|
+
// SPA tracking helpers
|
|
27
|
+
private spaTrackingActive: boolean;
|
|
28
|
+
private pushStateProxy?: any;
|
|
29
|
+
private replaceStateProxy?: any;
|
|
30
|
+
private originalPushState: any;
|
|
31
|
+
private originalReplaceState: any;
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
eventRecorder: TrackerEventRecorder,
|
|
35
|
+
options?: PageViewTrackingOptions
|
|
36
|
+
) {
|
|
37
|
+
this.options = {};
|
|
38
|
+
this.trackerActive = true;
|
|
39
|
+
this.eventRecorder = eventRecorder;
|
|
40
|
+
this.spaTrackingActive = false;
|
|
41
|
+
this.handleLocationChange = this.handleLocationChange.bind(this);
|
|
42
|
+
|
|
43
|
+
this.configure(eventRecorder, options);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public configure(
|
|
47
|
+
eventRecorder: TrackerEventRecorder,
|
|
48
|
+
options?: PageViewTrackingOptions
|
|
49
|
+
) {
|
|
50
|
+
this.eventRecorder = eventRecorder;
|
|
51
|
+
|
|
52
|
+
// Clean up any existing listeners
|
|
53
|
+
this.cleanup();
|
|
54
|
+
|
|
55
|
+
// Apply defaults
|
|
56
|
+
this.options = {
|
|
57
|
+
appType: options?.appType ?? DEFAULT_APP_TYPE,
|
|
58
|
+
attributes: options?.attributes ?? undefined,
|
|
59
|
+
eventName: this.options?.eventName ?? DEFAULT_EVENT_NAME,
|
|
60
|
+
urlProvider: this.options?.urlProvider ?? DEFAULT_URL_PROVIDER,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Configure SPA or MPA page view tracking
|
|
64
|
+
if (isBrowser()) {
|
|
65
|
+
if (this.options.appType === 'singlePage') {
|
|
66
|
+
this.setupSPATracking();
|
|
67
|
+
} else {
|
|
68
|
+
this.setupMPATracking();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.trackerActive = true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public cleanup() {
|
|
76
|
+
// No-op if document listener is not active
|
|
77
|
+
if (!this.trackerActive) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Clean up SPA page view listeners
|
|
82
|
+
if (this.spaTrackingActive) {
|
|
83
|
+
window.history.pushState = this.originalPushState;
|
|
84
|
+
window.history.replaceState = this.originalReplaceState;
|
|
85
|
+
this.pushStateProxy?.revoke();
|
|
86
|
+
this.replaceStateProxy?.revoke();
|
|
87
|
+
window.removeEventListener('popstate', this.handleLocationChange);
|
|
88
|
+
|
|
89
|
+
this.spaTrackingActive = false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private setupSPATracking() {
|
|
94
|
+
if (!this.spaTrackingActive) {
|
|
95
|
+
// Configure proxies on History APIs
|
|
96
|
+
this.pushStateProxy = Proxy.revocable(window.history.pushState, {
|
|
97
|
+
apply: (target, thisArg, args) => {
|
|
98
|
+
const proxiedResult = target.apply(thisArg, args as any);
|
|
99
|
+
|
|
100
|
+
this.handleLocationChange();
|
|
101
|
+
|
|
102
|
+
return proxiedResult;
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
this.replaceStateProxy = Proxy.revocable(window.history.replaceState, {
|
|
106
|
+
apply: (target, thisArg, args) => {
|
|
107
|
+
const proxiedResult = target.apply(thisArg, args as any);
|
|
108
|
+
|
|
109
|
+
this.handleLocationChange();
|
|
110
|
+
|
|
111
|
+
return proxiedResult;
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
this.originalPushState = window.history.pushState;
|
|
116
|
+
this.originalReplaceState = window.history.replaceState;
|
|
117
|
+
window.history.pushState = this.pushStateProxy.proxy;
|
|
118
|
+
window.history.replaceState = this.replaceStateProxy.proxy;
|
|
119
|
+
window.addEventListener('popstate', this.handleLocationChange);
|
|
120
|
+
sessionStorage.removeItem(PREV_URL_STORAGE_KEY);
|
|
121
|
+
|
|
122
|
+
this.spaTrackingActive = true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private setupMPATracking() {
|
|
127
|
+
this.handleLocationChange();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private handleLocationChange() {
|
|
131
|
+
const currentUrl = this.options.urlProvider!();
|
|
132
|
+
const eventName = this.options.eventName || DEFAULT_EVENT_NAME;
|
|
133
|
+
|
|
134
|
+
if (this.urlHasChanged()) {
|
|
135
|
+
sessionStorage.setItem(PREV_URL_STORAGE_KEY, currentUrl);
|
|
136
|
+
|
|
137
|
+
// Assemble attribute list
|
|
138
|
+
const attributes = Object.assign(
|
|
139
|
+
{
|
|
140
|
+
url: currentUrl,
|
|
141
|
+
},
|
|
142
|
+
this.options.attributes
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
logger.debug('Recording automatically tracked page view event', {
|
|
146
|
+
eventName,
|
|
147
|
+
attributes,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
this.eventRecorder(eventName, attributes);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private urlHasChanged() {
|
|
155
|
+
const prevUrl = sessionStorage.getItem(PREV_URL_STORAGE_KEY);
|
|
156
|
+
const currUrl = this.options.urlProvider!();
|
|
157
|
+
|
|
158
|
+
return currUrl !== prevUrl;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
sessionListener,
|
|
6
|
+
SessionState,
|
|
7
|
+
} from '@aws-amplify/core/internals/utils';
|
|
8
|
+
import { ConsoleLogger } from '@aws-amplify/core';
|
|
9
|
+
import {
|
|
10
|
+
SessionTrackingOptions,
|
|
11
|
+
TrackerEventRecorder,
|
|
12
|
+
TrackerInterface,
|
|
13
|
+
} from '../types/trackers';
|
|
14
|
+
|
|
15
|
+
const SESSION_START_EVENT = '_session.start';
|
|
16
|
+
const SESSION_STOP_EVENT = '_session.stop';
|
|
17
|
+
|
|
18
|
+
const logger = new ConsoleLogger('SessionTracker');
|
|
19
|
+
|
|
20
|
+
export class SessionTracker implements TrackerInterface {
|
|
21
|
+
private options: SessionTrackingOptions;
|
|
22
|
+
private eventRecorder: TrackerEventRecorder;
|
|
23
|
+
private initialEventSent: boolean;
|
|
24
|
+
private sessionTrackingActive: boolean;
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
eventRecorder: TrackerEventRecorder,
|
|
28
|
+
options?: SessionTrackingOptions
|
|
29
|
+
) {
|
|
30
|
+
this.options = {};
|
|
31
|
+
this.eventRecorder = eventRecorder;
|
|
32
|
+
this.initialEventSent = false;
|
|
33
|
+
this.sessionTrackingActive = false;
|
|
34
|
+
this.handleStateChange = this.handleStateChange.bind(this);
|
|
35
|
+
|
|
36
|
+
this.configure(eventRecorder, options);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public configure(
|
|
40
|
+
eventRecorder: TrackerEventRecorder,
|
|
41
|
+
options?: SessionTrackingOptions
|
|
42
|
+
) {
|
|
43
|
+
this.eventRecorder = eventRecorder;
|
|
44
|
+
|
|
45
|
+
// Clean up any existing listeners
|
|
46
|
+
this.cleanup();
|
|
47
|
+
|
|
48
|
+
// Apply defaults
|
|
49
|
+
this.options = {
|
|
50
|
+
attributes: options?.attributes ?? {},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Setup state listeners
|
|
54
|
+
if (!this.sessionTrackingActive) {
|
|
55
|
+
sessionListener.addStateChangeListener(
|
|
56
|
+
this.handleStateChange,
|
|
57
|
+
!this.initialEventSent
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
this.sessionTrackingActive = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public cleanup() {
|
|
65
|
+
if (this.sessionTrackingActive) {
|
|
66
|
+
sessionListener.removeStateChangeListener(this.handleStateChange);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this.sessionTrackingActive = false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private handleStateChange(state: SessionState) {
|
|
73
|
+
if (state === 'started') {
|
|
74
|
+
this.sessionStarted();
|
|
75
|
+
} else {
|
|
76
|
+
this.sessionStopped();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private sessionStarted() {
|
|
81
|
+
const attributes = this.options.attributes ?? {};
|
|
82
|
+
|
|
83
|
+
logger.debug('Recording automatically tracked page view event', {
|
|
84
|
+
SESSION_START_EVENT,
|
|
85
|
+
attributes,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
this.eventRecorder(SESSION_START_EVENT, attributes);
|
|
89
|
+
|
|
90
|
+
// NOTE: The initial event will not be re-sent on re-configuration (e.g. to add additional custom attributes)
|
|
91
|
+
if (!this.initialEventSent) {
|
|
92
|
+
this.initialEventSent = true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private sessionStopped() {
|
|
97
|
+
const attributes = this.options.attributes ?? {};
|
|
98
|
+
|
|
99
|
+
logger.debug('Recording automatically tracked page view event', {
|
|
100
|
+
SESSION_STOP_EVENT,
|
|
101
|
+
attributes,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
this.eventRecorder(SESSION_STOP_EVENT, attributes);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
sessionListener,
|
|
6
|
+
SessionState,
|
|
7
|
+
} from '@aws-amplify/core/internals/utils';
|
|
8
|
+
import { ConsoleLogger } from '@aws-amplify/core';
|
|
9
|
+
import {
|
|
10
|
+
SessionTrackingOptions,
|
|
11
|
+
TrackerEventRecorder,
|
|
12
|
+
TrackerInterface,
|
|
13
|
+
} from '../types/trackers';
|
|
14
|
+
|
|
15
|
+
const SESSION_START_EVENT = '_session.start';
|
|
16
|
+
const SESSION_STOP_EVENT = '_session.stop';
|
|
17
|
+
|
|
18
|
+
const logger = new ConsoleLogger('SessionTracker');
|
|
19
|
+
|
|
20
|
+
export class SessionTracker implements TrackerInterface {
|
|
21
|
+
private options: SessionTrackingOptions;
|
|
22
|
+
private eventRecorder: TrackerEventRecorder;
|
|
23
|
+
private sessionTrackingActive: boolean;
|
|
24
|
+
private initialEventSent: boolean;
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
eventRecorder: TrackerEventRecorder,
|
|
28
|
+
options?: SessionTrackingOptions
|
|
29
|
+
) {
|
|
30
|
+
this.options = {};
|
|
31
|
+
this.eventRecorder = eventRecorder;
|
|
32
|
+
this.sessionTrackingActive = false;
|
|
33
|
+
this.initialEventSent = false;
|
|
34
|
+
this.handleStateChange = this.handleStateChange.bind(this);
|
|
35
|
+
|
|
36
|
+
this.configure(eventRecorder, options);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public configure(
|
|
40
|
+
eventRecorder: TrackerEventRecorder,
|
|
41
|
+
options?: SessionTrackingOptions
|
|
42
|
+
) {
|
|
43
|
+
this.eventRecorder = eventRecorder;
|
|
44
|
+
|
|
45
|
+
// Clean up any existing listeners
|
|
46
|
+
this.cleanup();
|
|
47
|
+
|
|
48
|
+
// Apply defaults
|
|
49
|
+
this.options = {
|
|
50
|
+
attributes: options?.attributes ?? {},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Setup state listeners
|
|
54
|
+
if (!this.sessionTrackingActive) {
|
|
55
|
+
sessionListener.addStateChangeListener(
|
|
56
|
+
this.handleStateChange,
|
|
57
|
+
!this.initialEventSent
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
this.sessionTrackingActive = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public cleanup() {
|
|
65
|
+
if (this.sessionTrackingActive) {
|
|
66
|
+
sessionListener.removeStateChangeListener(this.handleStateChange);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this.sessionTrackingActive = false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private handleStateChange(state: SessionState) {
|
|
73
|
+
if (state === 'started') {
|
|
74
|
+
this.sessionStarted();
|
|
75
|
+
} else {
|
|
76
|
+
this.sessionStopped();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private sessionStarted() {
|
|
81
|
+
const attributes = this.options.attributes ?? {};
|
|
82
|
+
|
|
83
|
+
logger.debug('Recording automatically tracked page view event', {
|
|
84
|
+
SESSION_START_EVENT,
|
|
85
|
+
attributes,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
this.eventRecorder(SESSION_START_EVENT, attributes);
|
|
89
|
+
|
|
90
|
+
// NOTE: The initial event will not be re-sent on re-configuration (e.g. to add additional custom attributes)
|
|
91
|
+
if (!this.initialEventSent) {
|
|
92
|
+
this.initialEventSent = true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private sessionStopped() {
|
|
97
|
+
const attributes = this.options.attributes ?? {};
|
|
98
|
+
|
|
99
|
+
logger.debug('Recording automatically tracked page view event', {
|
|
100
|
+
SESSION_STOP_EVENT,
|
|
101
|
+
attributes,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
this.eventRecorder(SESSION_STOP_EVENT, attributes);
|
|
105
|
+
}
|
|
106
|
+
}
|
package/src/types/index.ts
CHANGED
|
@@ -1,20 +1,11 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
export {
|
|
5
|
-
AutoTrackAttributes,
|
|
6
|
-
AutoTrackEventOpts,
|
|
7
|
-
AutoTrackPageViewOpts,
|
|
8
|
-
AutoTrackSessionOpts,
|
|
9
|
-
EventAttributes,
|
|
10
|
-
EventMetrics,
|
|
11
|
-
EventTrackOpts,
|
|
12
|
-
PageViewTrackOpts,
|
|
13
|
-
SessionTrackOpts,
|
|
14
|
-
} from './analytics';
|
|
15
|
-
|
|
16
4
|
export { AnalyticsServiceOptions } from './options';
|
|
17
5
|
|
|
18
|
-
export {
|
|
6
|
+
export {
|
|
7
|
+
AnalyticsConfigureAutoTrackInput,
|
|
8
|
+
AnalyticsIdentifyUserInput,
|
|
9
|
+
} from './inputs';
|
|
19
10
|
|
|
20
11
|
export { KinesisStream, KinesisShard, KinesisEventData } from './kinesis';
|
package/src/types/inputs.ts
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
import { UserProfile } from '@aws-amplify/core';
|
|
5
5
|
import { AnalyticsServiceOptions } from '.';
|
|
6
|
+
import {
|
|
7
|
+
SessionTrackingOptions,
|
|
8
|
+
PageViewTrackingOptions,
|
|
9
|
+
EventTrackingOptions,
|
|
10
|
+
} from './trackers';
|
|
6
11
|
|
|
7
12
|
/**
|
|
8
13
|
* Input type for `identifyUser`.
|
|
@@ -23,7 +28,25 @@ export type AnalyticsIdentifyUserInput<
|
|
|
23
28
|
/**
|
|
24
29
|
* Options to be passed to the API.
|
|
25
30
|
*/
|
|
26
|
-
options?:
|
|
27
|
-
serviceOptions?: ServiceOptions;
|
|
28
|
-
};
|
|
31
|
+
options?: ServiceOptions;
|
|
29
32
|
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Input type for `configureAutoTrack`.
|
|
36
|
+
*/
|
|
37
|
+
export type AnalyticsConfigureAutoTrackInput = {
|
|
38
|
+
enable: boolean;
|
|
39
|
+
} & (
|
|
40
|
+
| {
|
|
41
|
+
type: 'session';
|
|
42
|
+
options?: SessionTrackingOptions;
|
|
43
|
+
}
|
|
44
|
+
| {
|
|
45
|
+
type: 'pageView';
|
|
46
|
+
options?: PageViewTrackingOptions;
|
|
47
|
+
}
|
|
48
|
+
| {
|
|
49
|
+
type: 'event';
|
|
50
|
+
options?: EventTrackingOptions;
|
|
51
|
+
}
|
|
52
|
+
);
|
package/src/types/options.ts
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export type SessionTrackingOptions = {
|
|
5
|
+
attributes?: TrackerAttributes;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type PageViewTrackingOptions = {
|
|
9
|
+
attributes?: TrackerAttributes;
|
|
10
|
+
eventName?: string;
|
|
11
|
+
urlProvider?: () => string;
|
|
12
|
+
appType?: 'multiPage' | 'singlePage';
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type EventTrackingOptions = {
|
|
16
|
+
attributes?: TrackerAttributes;
|
|
17
|
+
events?: DOMEvent[];
|
|
18
|
+
selectorPrefix?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type TrackerType = 'event' | 'pageView' | 'session';
|
|
22
|
+
|
|
23
|
+
export type TrackerAttributes = Record<string, string>;
|
|
24
|
+
|
|
25
|
+
export type TrackerEventRecorder = (
|
|
26
|
+
eventName: string,
|
|
27
|
+
attributes: TrackerAttributes
|
|
28
|
+
) => void;
|
|
29
|
+
|
|
30
|
+
export type DOMEvent = keyof GlobalEventHandlersEventMap;
|
|
31
|
+
|
|
32
|
+
export interface TrackerInterface {
|
|
33
|
+
configure(eventRecorder: TrackerEventRecorder, options?: object): void;
|
|
34
|
+
cleanup(): void;
|
|
35
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { ConsoleLogger } from '@aws-amplify/core
|
|
4
|
+
import { ConsoleLogger } from '@aws-amplify/core';
|
|
5
5
|
import { EventBufferConfig, IAnalyticsClient } from './';
|
|
6
6
|
|
|
7
7
|
const logger = new ConsoleLogger('EventBuffer');
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { AnalyticsValidationErrorCode, assertValidationError } from '../errors';
|
|
5
|
+
import { AnalyticsConfigureAutoTrackInput } from '../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Validates tracker configuration.
|
|
9
|
+
*/
|
|
10
|
+
export const validateTrackerConfiguration = (
|
|
11
|
+
input: AnalyticsConfigureAutoTrackInput
|
|
12
|
+
) => {
|
|
13
|
+
// React Native only supports session tracking
|
|
14
|
+
assertValidationError(
|
|
15
|
+
input.type === 'session',
|
|
16
|
+
AnalyticsValidationErrorCode.UnsupportedPlatform
|
|
17
|
+
);
|
|
18
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { AnalyticsValidationErrorCode, assertValidationError } from '../errors';
|
|
5
|
+
import { AnalyticsConfigureAutoTrackInput } from '../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Validates tracker configuration.
|
|
9
|
+
*/
|
|
10
|
+
export const validateTrackerConfiguration = (
|
|
11
|
+
input: AnalyticsConfigureAutoTrackInput
|
|
12
|
+
) => {
|
|
13
|
+
assertValidationError(
|
|
14
|
+
input.type === 'event' ||
|
|
15
|
+
input.type === 'pageView' ||
|
|
16
|
+
input.type === 'session',
|
|
17
|
+
AnalyticsValidationErrorCode.InvalidTracker
|
|
18
|
+
);
|
|
19
|
+
};
|