@aws-amplify/analytics 7.0.1-console-preview.8f82e46.0 → 7.0.1-console-preview.b278dcb.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.
Files changed (136) hide show
  1. package/lib/errors/validation.d.ts +2 -0
  2. package/lib/errors/validation.js +8 -0
  3. package/lib/index.d.ts +1 -1
  4. package/lib/index.js +2 -1
  5. package/lib/providers/kinesis/apis/flushEvents.js +2 -1
  6. package/lib/providers/kinesis/apis/record.js +2 -1
  7. package/lib/providers/kinesis-firehose/apis/flushEvents.js +2 -1
  8. package/lib/providers/kinesis-firehose/apis/record.js +2 -1
  9. package/lib/providers/personalize/apis/flushEvents.js +2 -1
  10. package/lib/providers/personalize/apis/record.js +2 -1
  11. package/lib/providers/personalize/utils/autoTrackMedia.js +2 -1
  12. package/lib/providers/pinpoint/apis/configureAutoTrack.d.ts +15 -0
  13. package/lib/providers/pinpoint/apis/configureAutoTrack.js +35 -0
  14. package/lib/providers/pinpoint/apis/flushEvents.js +2 -1
  15. package/lib/providers/pinpoint/apis/identifyUser.js +4 -5
  16. package/lib/providers/pinpoint/apis/index.d.ts +1 -0
  17. package/lib/providers/pinpoint/apis/index.js +3 -1
  18. package/lib/providers/pinpoint/apis/record.js +1 -1
  19. package/lib/providers/pinpoint/index.d.ts +2 -2
  20. package/lib/providers/pinpoint/index.js +2 -1
  21. package/lib/providers/pinpoint/types/index.d.ts +1 -2
  22. package/lib/providers/pinpoint/types/index.js +0 -3
  23. package/lib/providers/pinpoint/types/inputs.d.ts +5 -1
  24. package/lib/trackers/EventTracker.d.ts +10 -0
  25. package/lib/trackers/EventTracker.js +104 -0
  26. package/lib/trackers/PageViewTracker.d.ts +18 -0
  27. package/lib/trackers/PageViewTracker.js +116 -0
  28. package/lib/trackers/SessionTracker.d.ts +13 -0
  29. package/lib/trackers/SessionTracker.js +73 -0
  30. package/lib/trackers/SessionTracker.native.d.ts +13 -0
  31. package/lib/trackers/SessionTracker.native.js +73 -0
  32. package/lib/trackers/index.d.ts +3 -0
  33. package/lib/trackers/index.js +11 -0
  34. package/lib/tsconfig.tsbuildinfo +1 -1
  35. package/lib/types/index.d.ts +1 -2
  36. package/lib/types/inputs.d.ts +17 -3
  37. package/lib/types/options.d.ts +1 -1
  38. package/lib/types/trackers.d.ts +22 -0
  39. package/lib/utils/eventBuffer/EventBuffer.js +2 -2
  40. package/lib/utils/index.d.ts +2 -0
  41. package/lib/utils/index.js +5 -1
  42. package/lib/utils/trackerConfigHelpers.d.ts +5 -0
  43. package/lib/utils/trackerConfigHelpers.js +15 -0
  44. package/lib/utils/trackerConfigHelpers.native.d.ts +5 -0
  45. package/lib/utils/trackerConfigHelpers.native.js +14 -0
  46. package/lib/utils/trackerHelpers.d.ts +9 -0
  47. package/lib/utils/trackerHelpers.js +44 -0
  48. package/lib-esm/errors/validation.d.ts +2 -0
  49. package/lib-esm/errors/validation.js +8 -0
  50. package/lib-esm/index.d.ts +1 -1
  51. package/lib-esm/index.js +1 -1
  52. package/lib-esm/providers/kinesis/apis/flushEvents.js +2 -1
  53. package/lib-esm/providers/kinesis/apis/record.js +2 -1
  54. package/lib-esm/providers/kinesis-firehose/apis/flushEvents.js +2 -1
  55. package/lib-esm/providers/kinesis-firehose/apis/record.js +3 -2
  56. package/lib-esm/providers/personalize/apis/flushEvents.js +2 -1
  57. package/lib-esm/providers/personalize/apis/record.js +2 -1
  58. package/lib-esm/providers/personalize/utils/autoTrackMedia.js +2 -1
  59. package/lib-esm/providers/pinpoint/apis/configureAutoTrack.d.ts +15 -0
  60. package/lib-esm/providers/pinpoint/apis/configureAutoTrack.js +31 -0
  61. package/lib-esm/providers/pinpoint/apis/flushEvents.js +2 -1
  62. package/lib-esm/providers/pinpoint/apis/identifyUser.js +5 -6
  63. package/lib-esm/providers/pinpoint/apis/index.d.ts +1 -0
  64. package/lib-esm/providers/pinpoint/apis/index.js +1 -0
  65. package/lib-esm/providers/pinpoint/apis/record.js +3 -3
  66. package/lib-esm/providers/pinpoint/index.d.ts +2 -2
  67. package/lib-esm/providers/pinpoint/index.js +1 -1
  68. package/lib-esm/providers/pinpoint/types/index.d.ts +1 -2
  69. package/lib-esm/providers/pinpoint/types/index.js +1 -1
  70. package/lib-esm/providers/pinpoint/types/inputs.d.ts +5 -1
  71. package/lib-esm/trackers/EventTracker.d.ts +10 -0
  72. package/lib-esm/trackers/EventTracker.js +101 -0
  73. package/lib-esm/trackers/PageViewTracker.d.ts +18 -0
  74. package/lib-esm/trackers/PageViewTracker.js +113 -0
  75. package/lib-esm/trackers/SessionTracker.d.ts +13 -0
  76. package/lib-esm/trackers/SessionTracker.js +70 -0
  77. package/lib-esm/trackers/SessionTracker.native.d.ts +13 -0
  78. package/lib-esm/trackers/SessionTracker.native.js +70 -0
  79. package/lib-esm/trackers/index.d.ts +3 -0
  80. package/lib-esm/trackers/index.js +5 -0
  81. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  82. package/lib-esm/types/index.d.ts +1 -2
  83. package/lib-esm/types/inputs.d.ts +17 -3
  84. package/lib-esm/types/options.d.ts +1 -1
  85. package/lib-esm/types/trackers.d.ts +22 -0
  86. package/lib-esm/utils/eventBuffer/EventBuffer.js +1 -1
  87. package/lib-esm/utils/index.d.ts +2 -0
  88. package/lib-esm/utils/index.js +2 -0
  89. package/lib-esm/utils/trackerConfigHelpers.d.ts +5 -0
  90. package/lib-esm/utils/trackerConfigHelpers.js +11 -0
  91. package/lib-esm/utils/trackerConfigHelpers.native.d.ts +5 -0
  92. package/lib-esm/utils/trackerConfigHelpers.native.js +10 -0
  93. package/lib-esm/utils/trackerHelpers.d.ts +9 -0
  94. package/lib-esm/utils/trackerHelpers.js +40 -0
  95. package/package.json +5 -4
  96. package/src/errors/validation.ts +8 -0
  97. package/src/index.ts +3 -1
  98. package/src/providers/kinesis/apis/flushEvents.ts +2 -4
  99. package/src/providers/kinesis/apis/record.ts +2 -4
  100. package/src/providers/kinesis-firehose/apis/flushEvents.ts +2 -4
  101. package/src/providers/kinesis-firehose/apis/record.ts +3 -5
  102. package/src/providers/personalize/apis/flushEvents.ts +2 -4
  103. package/src/providers/personalize/apis/record.ts +2 -4
  104. package/src/providers/personalize/utils/autoTrackMedia.ts +2 -1
  105. package/src/providers/pinpoint/apis/configureAutoTrack.ts +49 -0
  106. package/src/providers/pinpoint/apis/flushEvents.ts +2 -4
  107. package/src/providers/pinpoint/apis/identifyUser.ts +6 -3
  108. package/src/providers/pinpoint/apis/index.ts +1 -0
  109. package/src/providers/pinpoint/apis/record.ts +2 -3
  110. package/src/providers/pinpoint/index.ts +6 -2
  111. package/src/providers/pinpoint/types/index.ts +5 -2
  112. package/src/providers/pinpoint/types/inputs.ts +9 -1
  113. package/src/trackers/EventTracker.ts +135 -0
  114. package/src/trackers/PageViewTracker.ts +160 -0
  115. package/src/trackers/SessionTracker.native.ts +106 -0
  116. package/src/trackers/SessionTracker.ts +106 -0
  117. package/src/trackers/index.ts +6 -0
  118. package/src/types/index.ts +4 -13
  119. package/src/types/inputs.ts +26 -3
  120. package/src/types/options.ts +1 -1
  121. package/src/types/trackers.ts +35 -0
  122. package/src/utils/eventBuffer/EventBuffer.ts +1 -1
  123. package/src/utils/index.ts +2 -0
  124. package/src/utils/trackerConfigHelpers.native.ts +18 -0
  125. package/src/utils/trackerConfigHelpers.ts +19 -0
  126. package/src/utils/trackerHelpers.ts +57 -0
  127. package/lib/providers/pinpoint/types/errors.d.ts +0 -9
  128. package/lib/providers/pinpoint/types/errors.js +0 -15
  129. package/lib/types/analytics.d.ts +0 -50
  130. package/lib-esm/providers/pinpoint/types/errors.d.ts +0 -9
  131. package/lib-esm/providers/pinpoint/types/errors.js +0 -12
  132. package/lib-esm/types/analytics.d.ts +0 -50
  133. package/src/providers/pinpoint/types/errors.ts +0 -12
  134. package/src/types/analytics.ts +0 -70
  135. /package/lib/types/{analytics.js → trackers.js} +0 -0
  136. /package/lib-esm/types/{analytics.js → trackers.js} +0 -0
@@ -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
+ }
@@ -0,0 +1,6 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { EventTracker } from './EventTracker';
5
+ export { PageViewTracker } from './PageViewTracker';
6
+ export { SessionTracker } from './SessionTracker';
@@ -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 { AnalyticsIdentifyUserInput } from './inputs';
6
+ export {
7
+ AnalyticsConfigureAutoTrackInput,
8
+ AnalyticsIdentifyUserInput,
9
+ } from './inputs';
19
10
 
20
11
  export { KinesisStream, KinesisShard, KinesisEventData } from './kinesis';
@@ -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
+ );
@@ -4,4 +4,4 @@
4
4
  /**
5
5
  * Base type for service options.
6
6
  */
7
- export type AnalyticsServiceOptions = any;
7
+ export type AnalyticsServiceOptions = Record<string, unknown>;
@@ -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/internals/utils';
4
+ import { ConsoleLogger } from '@aws-amplify/core';
5
5
  import { EventBufferConfig, IAnalyticsClient } from './';
6
6
 
7
7
  const logger = new ConsoleLogger('EventBuffer');
@@ -17,3 +17,5 @@ export {
17
17
  getAnalyticsUserAgent,
18
18
  getAnalyticsUserAgentString,
19
19
  } from './userAgent';
20
+ export { updateProviderTrackers } from './trackerHelpers';
21
+ export { validateTrackerConfiguration } from './trackerConfigHelpers';
@@ -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
+ };
@@ -0,0 +1,57 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ TrackerAttributes,
6
+ TrackerEventRecorder,
7
+ TrackerInterface,
8
+ TrackerType,
9
+ } from '../types/trackers';
10
+ import { EventTracker, PageViewTracker, SessionTracker } from '../trackers';
11
+ import { ConfigureAutoTrackInput } from '../providers/pinpoint';
12
+
13
+ /**
14
+ * Updates a provider's trackers as appropriate for the provided auto-track configuration.
15
+ *
16
+ * @remark
17
+ * This utility will mutate the provider's configured trackers via `providerTrackers`.
18
+ */
19
+ export const updateProviderTrackers = (
20
+ input: ConfigureAutoTrackInput,
21
+ providerEventRecorder: TrackerEventRecorder,
22
+ providerTrackers: Partial<Record<TrackerType, TrackerInterface>>
23
+ ) => {
24
+ let trackerInstance;
25
+ const trackerType = input.type;
26
+ const currentTracker = providerTrackers[trackerType];
27
+
28
+ // Check if the tracker was disabled & should be cleaned up
29
+ if (!input.enable) {
30
+ if (currentTracker) {
31
+ currentTracker.cleanup();
32
+ delete providerTrackers[trackerType];
33
+ }
34
+
35
+ return;
36
+ }
37
+
38
+ // Re-configure the existing tracker, or create & configure an instance if it doesn't exist yet
39
+ if (currentTracker) {
40
+ currentTracker.configure(providerEventRecorder, input.options);
41
+
42
+ return;
43
+ }
44
+
45
+ // Create a new tracker instance for the type
46
+ if (trackerType === 'event') {
47
+ trackerInstance = new EventTracker(providerEventRecorder, input.options);
48
+ } else if (trackerType === 'pageView') {
49
+ trackerInstance = new PageViewTracker(providerEventRecorder, input.options);
50
+ } else if (trackerType === 'session') {
51
+ trackerInstance = new SessionTracker(providerEventRecorder, input.options);
52
+ }
53
+
54
+ if (trackerInstance) {
55
+ providerTrackers[trackerType] = trackerInstance;
56
+ }
57
+ };
@@ -1,9 +0,0 @@
1
- export declare enum UpdateEndpointException {
2
- BadRequestException = "BadRequestException",
3
- ForbiddenException = "ForbiddenException",
4
- InternalServerErrorException = "InternalServerErrorException",
5
- MethodNotAllowedException = "MethodNotAllowedException",
6
- NotFoundException = "NotFoundException",
7
- PayloadTooLargeException = "PayloadTooLargeException",
8
- TooManyRequestsException = "TooManyRequestsException"
9
- }
@@ -1,15 +0,0 @@
1
- "use strict";
2
- // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- // SPDX-License-Identifier: Apache-2.0
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.UpdateEndpointException = void 0;
6
- var UpdateEndpointException;
7
- (function (UpdateEndpointException) {
8
- UpdateEndpointException["BadRequestException"] = "BadRequestException";
9
- UpdateEndpointException["ForbiddenException"] = "ForbiddenException";
10
- UpdateEndpointException["InternalServerErrorException"] = "InternalServerErrorException";
11
- UpdateEndpointException["MethodNotAllowedException"] = "MethodNotAllowedException";
12
- UpdateEndpointException["NotFoundException"] = "NotFoundException";
13
- UpdateEndpointException["PayloadTooLargeException"] = "PayloadTooLargeException";
14
- UpdateEndpointException["TooManyRequestsException"] = "TooManyRequestsException";
15
- })(UpdateEndpointException = exports.UpdateEndpointException || (exports.UpdateEndpointException = {}));
@@ -1,50 +0,0 @@
1
- /**
2
- * Analytics instance options
3
- */
4
- export interface EventAttributes {
5
- [key: string]: string;
6
- }
7
- export interface EventMetrics {
8
- [key: string]: number;
9
- }
10
- export interface PageViewTrackOpts {
11
- enable: boolean;
12
- type?: string;
13
- eventName?: string;
14
- provider?: string;
15
- attributes?: EventAttributes | (() => EventAttributes | Promise<EventAttributes>);
16
- getUrl?: () => string;
17
- }
18
- export interface EventTrackOpts {
19
- enable: boolean;
20
- events?: Array<string>;
21
- selectorPrefix?: string;
22
- provider?: string;
23
- attributes?: EventAttributes | (() => EventAttributes | Promise<EventAttributes>);
24
- }
25
- export interface SessionTrackOpts {
26
- enable: boolean;
27
- attributes?: EventAttributes | (() => EventAttributes | Promise<EventAttributes>);
28
- provider?: string;
29
- }
30
- export type AutoTrackAttributes = (() => EventAttributes | Promise<EventAttributes>) | EventAttributes;
31
- export interface AutoTrackSessionOpts {
32
- enable: boolean;
33
- attributes?: AutoTrackAttributes;
34
- provider?: string;
35
- }
36
- export interface AutoTrackPageViewOpts {
37
- enable: boolean;
38
- eventName?: string;
39
- attributes?: AutoTrackAttributes;
40
- type?: 'SPA' | 'multiPageApp';
41
- provider?: string;
42
- getUrl?: () => string;
43
- }
44
- export interface AutoTrackEventOpts {
45
- enable: boolean;
46
- events?: string[];
47
- selectorPrefix?: string;
48
- provider?: string;
49
- attributes?: AutoTrackAttributes;
50
- }
@@ -1,9 +0,0 @@
1
- export declare enum UpdateEndpointException {
2
- BadRequestException = "BadRequestException",
3
- ForbiddenException = "ForbiddenException",
4
- InternalServerErrorException = "InternalServerErrorException",
5
- MethodNotAllowedException = "MethodNotAllowedException",
6
- NotFoundException = "NotFoundException",
7
- PayloadTooLargeException = "PayloadTooLargeException",
8
- TooManyRequestsException = "TooManyRequestsException"
9
- }