@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.
Files changed (154) 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/types/buffer.d.ts +2 -2
  8. package/lib/providers/kinesis-firehose/apis/flushEvents.js +2 -1
  9. package/lib/providers/kinesis-firehose/apis/record.js +2 -1
  10. package/lib/providers/kinesis-firehose/types/buffer.d.ts +2 -2
  11. package/lib/providers/personalize/apis/flushEvents.js +2 -1
  12. package/lib/providers/personalize/apis/record.js +61 -46
  13. package/lib/providers/personalize/types/buffer.d.ts +2 -2
  14. package/lib/providers/personalize/utils/autoTrackMedia.js +2 -1
  15. package/lib/providers/personalize/utils/cachedSession.d.ts +2 -2
  16. package/lib/providers/personalize/utils/cachedSession.js +23 -14
  17. package/lib/providers/pinpoint/apis/configureAutoTrack.d.ts +15 -0
  18. package/lib/providers/pinpoint/apis/configureAutoTrack.js +35 -0
  19. package/lib/providers/pinpoint/apis/flushEvents.js +2 -1
  20. package/lib/providers/pinpoint/apis/identifyUser.js +4 -5
  21. package/lib/providers/pinpoint/apis/index.d.ts +1 -0
  22. package/lib/providers/pinpoint/apis/index.js +3 -1
  23. package/lib/providers/pinpoint/apis/record.js +1 -1
  24. package/lib/providers/pinpoint/index.d.ts +2 -2
  25. package/lib/providers/pinpoint/index.js +2 -1
  26. package/lib/providers/pinpoint/types/index.d.ts +1 -2
  27. package/lib/providers/pinpoint/types/index.js +0 -3
  28. package/lib/providers/pinpoint/types/inputs.d.ts +5 -1
  29. package/lib/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
  30. package/lib/trackers/EventTracker.d.ts +10 -0
  31. package/lib/trackers/EventTracker.js +104 -0
  32. package/lib/trackers/PageViewTracker.d.ts +18 -0
  33. package/lib/trackers/PageViewTracker.js +116 -0
  34. package/lib/trackers/SessionTracker.d.ts +13 -0
  35. package/lib/trackers/SessionTracker.js +73 -0
  36. package/lib/trackers/SessionTracker.native.d.ts +13 -0
  37. package/lib/trackers/SessionTracker.native.js +73 -0
  38. package/lib/trackers/index.d.ts +3 -0
  39. package/lib/trackers/index.js +11 -0
  40. package/lib/tsconfig.tsbuildinfo +1 -1
  41. package/lib/types/index.d.ts +1 -2
  42. package/lib/types/inputs.d.ts +17 -3
  43. package/lib/types/options.d.ts +1 -1
  44. package/lib/types/trackers.d.ts +22 -0
  45. package/lib/utils/eventBuffer/EventBuffer.js +2 -2
  46. package/lib/utils/index.d.ts +2 -0
  47. package/lib/utils/index.js +5 -1
  48. package/lib/utils/resolveCredentials.d.ts +1 -6
  49. package/lib/utils/trackerConfigHelpers.d.ts +5 -0
  50. package/lib/utils/trackerConfigHelpers.js +15 -0
  51. package/lib/utils/trackerConfigHelpers.native.d.ts +5 -0
  52. package/lib/utils/trackerConfigHelpers.native.js +14 -0
  53. package/lib/utils/trackerHelpers.d.ts +9 -0
  54. package/lib/utils/trackerHelpers.js +44 -0
  55. package/lib-esm/errors/validation.d.ts +2 -0
  56. package/lib-esm/errors/validation.js +8 -0
  57. package/lib-esm/index.d.ts +1 -1
  58. package/lib-esm/index.js +1 -1
  59. package/lib-esm/providers/kinesis/apis/flushEvents.js +2 -1
  60. package/lib-esm/providers/kinesis/apis/record.js +2 -1
  61. package/lib-esm/providers/kinesis/types/buffer.d.ts +2 -2
  62. package/lib-esm/providers/kinesis-firehose/apis/flushEvents.js +2 -1
  63. package/lib-esm/providers/kinesis-firehose/apis/record.js +3 -2
  64. package/lib-esm/providers/kinesis-firehose/types/buffer.d.ts +2 -2
  65. package/lib-esm/providers/personalize/apis/flushEvents.js +2 -1
  66. package/lib-esm/providers/personalize/apis/record.js +61 -46
  67. package/lib-esm/providers/personalize/types/buffer.d.ts +2 -2
  68. package/lib-esm/providers/personalize/utils/autoTrackMedia.js +2 -1
  69. package/lib-esm/providers/personalize/utils/cachedSession.d.ts +2 -2
  70. package/lib-esm/providers/personalize/utils/cachedSession.js +24 -15
  71. package/lib-esm/providers/pinpoint/apis/configureAutoTrack.d.ts +15 -0
  72. package/lib-esm/providers/pinpoint/apis/configureAutoTrack.js +31 -0
  73. package/lib-esm/providers/pinpoint/apis/flushEvents.js +2 -1
  74. package/lib-esm/providers/pinpoint/apis/identifyUser.js +5 -6
  75. package/lib-esm/providers/pinpoint/apis/index.d.ts +1 -0
  76. package/lib-esm/providers/pinpoint/apis/index.js +1 -0
  77. package/lib-esm/providers/pinpoint/apis/record.js +3 -3
  78. package/lib-esm/providers/pinpoint/index.d.ts +2 -2
  79. package/lib-esm/providers/pinpoint/index.js +1 -1
  80. package/lib-esm/providers/pinpoint/types/index.d.ts +1 -2
  81. package/lib-esm/providers/pinpoint/types/index.js +1 -1
  82. package/lib-esm/providers/pinpoint/types/inputs.d.ts +5 -1
  83. package/lib-esm/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
  84. package/lib-esm/trackers/EventTracker.d.ts +10 -0
  85. package/lib-esm/trackers/EventTracker.js +101 -0
  86. package/lib-esm/trackers/PageViewTracker.d.ts +18 -0
  87. package/lib-esm/trackers/PageViewTracker.js +113 -0
  88. package/lib-esm/trackers/SessionTracker.d.ts +13 -0
  89. package/lib-esm/trackers/SessionTracker.js +70 -0
  90. package/lib-esm/trackers/SessionTracker.native.d.ts +13 -0
  91. package/lib-esm/trackers/SessionTracker.native.js +70 -0
  92. package/lib-esm/trackers/index.d.ts +3 -0
  93. package/lib-esm/trackers/index.js +5 -0
  94. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  95. package/lib-esm/types/index.d.ts +1 -2
  96. package/lib-esm/types/inputs.d.ts +17 -3
  97. package/lib-esm/types/options.d.ts +1 -1
  98. package/lib-esm/types/trackers.d.ts +22 -0
  99. package/lib-esm/utils/eventBuffer/EventBuffer.js +1 -1
  100. package/lib-esm/utils/index.d.ts +2 -0
  101. package/lib-esm/utils/index.js +2 -0
  102. package/lib-esm/utils/resolveCredentials.d.ts +1 -6
  103. package/lib-esm/utils/trackerConfigHelpers.d.ts +5 -0
  104. package/lib-esm/utils/trackerConfigHelpers.js +11 -0
  105. package/lib-esm/utils/trackerConfigHelpers.native.d.ts +5 -0
  106. package/lib-esm/utils/trackerConfigHelpers.native.js +10 -0
  107. package/lib-esm/utils/trackerHelpers.d.ts +9 -0
  108. package/lib-esm/utils/trackerHelpers.js +40 -0
  109. package/package.json +6 -7
  110. package/src/errors/validation.ts +8 -0
  111. package/src/index.ts +3 -1
  112. package/src/providers/kinesis/apis/flushEvents.ts +2 -4
  113. package/src/providers/kinesis/apis/record.ts +2 -4
  114. package/src/providers/kinesis/types/buffer.ts +2 -2
  115. package/src/providers/kinesis-firehose/apis/flushEvents.ts +2 -4
  116. package/src/providers/kinesis-firehose/apis/record.ts +3 -5
  117. package/src/providers/kinesis-firehose/types/buffer.ts +2 -2
  118. package/src/providers/personalize/apis/flushEvents.ts +2 -4
  119. package/src/providers/personalize/apis/record.ts +5 -7
  120. package/src/providers/personalize/types/buffer.ts +2 -2
  121. package/src/providers/personalize/utils/autoTrackMedia.ts +2 -1
  122. package/src/providers/personalize/utils/cachedSession.ts +8 -7
  123. package/src/providers/pinpoint/apis/configureAutoTrack.ts +49 -0
  124. package/src/providers/pinpoint/apis/flushEvents.ts +2 -4
  125. package/src/providers/pinpoint/apis/identifyUser.ts +6 -3
  126. package/src/providers/pinpoint/apis/index.ts +1 -0
  127. package/src/providers/pinpoint/apis/record.ts +2 -3
  128. package/src/providers/pinpoint/index.ts +6 -2
  129. package/src/providers/pinpoint/types/index.ts +5 -2
  130. package/src/providers/pinpoint/types/inputs.ts +9 -1
  131. package/src/trackers/EventTracker.ts +135 -0
  132. package/src/trackers/PageViewTracker.ts +160 -0
  133. package/src/trackers/SessionTracker.native.ts +106 -0
  134. package/src/trackers/SessionTracker.ts +106 -0
  135. package/src/trackers/index.ts +6 -0
  136. package/src/types/index.ts +4 -13
  137. package/src/types/inputs.ts +26 -3
  138. package/src/types/options.ts +1 -1
  139. package/src/types/trackers.ts +35 -0
  140. package/src/utils/eventBuffer/EventBuffer.ts +1 -1
  141. package/src/utils/index.ts +2 -0
  142. package/src/utils/trackerConfigHelpers.native.ts +18 -0
  143. package/src/utils/trackerConfigHelpers.ts +19 -0
  144. package/src/utils/trackerHelpers.ts +57 -0
  145. package/lib/providers/pinpoint/types/errors.d.ts +0 -9
  146. package/lib/providers/pinpoint/types/errors.js +0 -15
  147. package/lib/types/analytics.d.ts +0 -50
  148. package/lib-esm/providers/pinpoint/types/errors.d.ts +0 -9
  149. package/lib-esm/providers/pinpoint/types/errors.js +0 -12
  150. package/lib-esm/types/analytics.d.ts +0 -50
  151. package/src/providers/pinpoint/types/errors.ts +0 -12
  152. package/src/types/analytics.ts +0 -70
  153. /package/lib/types/{analytics.js → trackers.js} +0 -0
  154. /package/lib-esm/types/{analytics.js → trackers.js} +0 -0
@@ -2,7 +2,8 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { resolveConfig, resolveCredentials } from '../utils';
4
4
  import { flushEvents as flushEventsCore } from '@aws-amplify/core/internals/providers/pinpoint';
5
- import { AnalyticsAction, ConsoleLogger, } from '@aws-amplify/core/internals/utils';
5
+ import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
6
+ import { ConsoleLogger } from '@aws-amplify/core';
6
7
  import { getAnalyticsUserAgentString } from '../../../utils';
7
8
  var logger = new ConsoleLogger('Analytics');
8
9
  /**
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { __awaiter, __generator } from "tslib";
4
4
  import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
5
- import { updateEndpoint } from '@aws-amplify/core/internals/providers/pinpoint';
5
+ import { updateEndpoint, } from '@aws-amplify/core/internals/providers/pinpoint';
6
6
  import { getAnalyticsUserAgentString } from '../../../utils';
7
7
  import { resolveConfig, resolveCredentials } from '../utils';
8
8
  /**
@@ -54,14 +54,13 @@ export var identifyUser = function (_a) {
54
54
  var userId = _a.userId, userProfile = _a.userProfile, options = _a.options;
55
55
  return __awaiter(void 0, void 0, void 0, function () {
56
56
  var _b, credentials, identityId, _c, appId, region, userAttributes;
57
- var _d;
58
- return __generator(this, function (_e) {
59
- switch (_e.label) {
57
+ return __generator(this, function (_d) {
58
+ switch (_d.label) {
60
59
  case 0: return [4 /*yield*/, resolveCredentials()];
61
60
  case 1:
62
- _b = _e.sent(), credentials = _b.credentials, identityId = _b.identityId;
61
+ _b = _d.sent(), credentials = _b.credentials, identityId = _b.identityId;
63
62
  _c = resolveConfig(), appId = _c.appId, region = _c.region;
64
- userAttributes = ((_d = options === null || options === void 0 ? void 0 : options.serviceOptions) !== null && _d !== void 0 ? _d : {}).userAttributes;
63
+ userAttributes = (options !== null && options !== void 0 ? options : {}).userAttributes;
65
64
  updateEndpoint({
66
65
  appId: appId,
67
66
  category: 'Analytics',
@@ -1,3 +1,4 @@
1
1
  export { record } from './record';
2
2
  export { identifyUser } from './identifyUser';
3
+ export { configureAutoTrack } from './configureAutoTrack';
3
4
  export { flushEvents } from './flushEvents';
@@ -2,4 +2,5 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  export { record } from './record';
4
4
  export { identifyUser } from './identifyUser';
5
+ export { configureAutoTrack } from './configureAutoTrack';
5
6
  export { flushEvents } from './flushEvents';
@@ -1,12 +1,12 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { Hub } from '@aws-amplify/core';
4
- import { AMPLIFY_SYMBOL, AnalyticsAction, ConsoleLogger as Logger, } from '@aws-amplify/core/internals/utils';
3
+ import { Hub, ConsoleLogger } from '@aws-amplify/core';
4
+ import { AMPLIFY_SYMBOL, AnalyticsAction, } from '@aws-amplify/core/internals/utils';
5
5
  import { record as recordCore } from '@aws-amplify/core/internals/providers/pinpoint';
6
6
  import { AnalyticsValidationErrorCode, assertValidationError, } from '../../../errors';
7
7
  import { getAnalyticsUserAgentString, isAnalyticsEnabled, } from '../../../utils';
8
8
  import { resolveConfig, resolveCredentials } from '../utils';
9
- var logger = new Logger('Analytics');
9
+ var logger = new ConsoleLogger('Analytics');
10
10
  /**
11
11
  * Records an Analytic event to Pinpoint. Events will be buffered and periodically sent to Pinpoint.
12
12
  *
@@ -1,2 +1,2 @@
1
- export { record, identifyUser, flushEvents } from './apis';
2
- export { RecordInput, IdentifyUserInput } from './types/inputs';
1
+ export { record, identifyUser, flushEvents, configureAutoTrack } from './apis';
2
+ export { RecordInput, IdentifyUserInput, ConfigureAutoTrackInput, } from './types/inputs';
@@ -1,3 +1,3 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- export { record, identifyUser, flushEvents } from './apis';
3
+ export { record, identifyUser, flushEvents, configureAutoTrack } from './apis';
@@ -1,3 +1,2 @@
1
- export { UpdateEndpointException } from './errors';
2
- export { RecordInput, IdentifyUserInput } from './inputs';
1
+ export { RecordInput, IdentifyUserInput, ConfigureAutoTrackInput, } from './inputs';
3
2
  export { IdentifyUserOptions } from './options';
@@ -1,3 +1,3 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- export { UpdateEndpointException } from './errors';
3
+ export {};
@@ -1,6 +1,6 @@
1
1
  import { PinpointAnalyticsEvent } from '@aws-amplify/core/internals/providers/pinpoint';
2
2
  import { IdentifyUserOptions } from '.';
3
- import { AnalyticsIdentifyUserInput } from '../../../types';
3
+ import { AnalyticsConfigureAutoTrackInput, AnalyticsIdentifyUserInput } from '../../../types';
4
4
  /**
5
5
  * Input type for Pinpoint record API.
6
6
  */
@@ -9,3 +9,7 @@ export type RecordInput = PinpointAnalyticsEvent;
9
9
  * Input type for Pinpoint identifyUser API.
10
10
  */
11
11
  export type IdentifyUserInput = AnalyticsIdentifyUserInput<IdentifyUserOptions>;
12
+ /**
13
+ * Input type for Pinpoint configureAutoTrack API.
14
+ */
15
+ export type ConfigureAutoTrackInput = AnalyticsConfigureAutoTrackInput;
@@ -2,11 +2,6 @@
2
2
  * @internal
3
3
  */
4
4
  export declare const resolveCredentials: () => Promise<{
5
- credentials: {
6
- accessKeyId: string;
7
- secretAccessKey: string;
8
- sessionToken?: string | undefined;
9
- expiration?: Date | undefined;
10
- };
5
+ credentials: import("@aws-amplify/core/lib-esm/libraryUtils").AWSCredentials;
11
6
  identityId: string | undefined;
12
7
  }>;
@@ -0,0 +1,10 @@
1
+ import { EventTrackingOptions, TrackerEventRecorder, TrackerInterface } from '../types/trackers';
2
+ export declare class EventTracker implements TrackerInterface {
3
+ private trackerActive;
4
+ private options;
5
+ private eventRecorder;
6
+ constructor(eventRecorder: TrackerEventRecorder, options?: EventTrackingOptions);
7
+ configure(eventRecorder: TrackerEventRecorder, options?: EventTrackingOptions): void;
8
+ cleanup(): void;
9
+ private handleDocEvent;
10
+ }
@@ -0,0 +1,101 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { ConsoleLogger } from '@aws-amplify/core';
4
+ import { isBrowser } from '@aws-amplify/core/internals/utils';
5
+ var DEFAULT_EVENTS = ['click'];
6
+ var DEFAULT_SELECTOR_PREFIX = 'data-amplify-analytics-';
7
+ var DEFAULT_EVENT_NAME = 'event'; // Default event name as sent to the analytics provider
8
+ var logger = new ConsoleLogger('EventTracker');
9
+ var EventTracker = /** @class */ (function () {
10
+ function EventTracker(eventRecorder, options) {
11
+ this.options = {};
12
+ this.trackerActive = false;
13
+ this.eventRecorder = eventRecorder;
14
+ this.handleDocEvent = this.handleDocEvent.bind(this);
15
+ this.configure(eventRecorder, options);
16
+ }
17
+ EventTracker.prototype.configure = function (eventRecorder, options) {
18
+ var _this = this;
19
+ var _a, _b, _c, _d;
20
+ this.eventRecorder = eventRecorder;
21
+ // Clean up any existing listeners
22
+ this.cleanup();
23
+ // Apply defaults
24
+ this.options = {
25
+ attributes: (_a = options === null || options === void 0 ? void 0 : options.attributes) !== null && _a !== void 0 ? _a : undefined,
26
+ events: (_b = options === null || options === void 0 ? void 0 : options.events) !== null && _b !== void 0 ? _b : DEFAULT_EVENTS,
27
+ selectorPrefix: (_c = options === null || options === void 0 ? void 0 : options.selectorPrefix) !== null && _c !== void 0 ? _c : DEFAULT_SELECTOR_PREFIX,
28
+ };
29
+ // Register event listeners
30
+ if (isBrowser()) {
31
+ (_d = this.options.events) === null || _d === void 0 ? void 0 : _d.forEach(function (targetEvent) {
32
+ document.addEventListener(targetEvent, _this.handleDocEvent, {
33
+ capture: true,
34
+ });
35
+ });
36
+ this.trackerActive = true;
37
+ }
38
+ };
39
+ EventTracker.prototype.cleanup = function () {
40
+ var _this = this;
41
+ var _a;
42
+ // No-op if document listener is not active
43
+ if (!this.trackerActive) {
44
+ return;
45
+ }
46
+ // Clean up event listeners
47
+ (_a = this.options.events) === null || _a === void 0 ? void 0 : _a.forEach(function (targetEvent) {
48
+ document.removeEventListener(targetEvent, _this.handleDocEvent, {
49
+ capture: true,
50
+ });
51
+ });
52
+ };
53
+ EventTracker.prototype.handleDocEvent = function (event) {
54
+ var _a;
55
+ /**
56
+ * Example DOM element:
57
+ *
58
+ * ```
59
+ * <button
60
+ * data-amplify-analytics-on="click"
61
+ * data-amplify-analytics-name="click"
62
+ * data-amplify-analytics-attrs="attr1:attr1_value,attr2:attr2_value"
63
+ * />
64
+ * ```
65
+ */
66
+ var triggerSelector = "[".concat(this.options.selectorPrefix, "on]");
67
+ var attrSelector = "".concat(this.options.selectorPrefix, "attrs");
68
+ var eventNameSelector = "".concat(this.options.selectorPrefix, "name");
69
+ var eventSource = event.target;
70
+ // Validate that the triggering event type is being tracked
71
+ if (!((_a = this.options.events) === null || _a === void 0 ? void 0 : _a.includes(event.type))) {
72
+ return;
73
+ }
74
+ if (eventSource instanceof HTMLElement) {
75
+ var target = eventSource.closest(triggerSelector);
76
+ if (target) {
77
+ // Parse event name from the element
78
+ var eventName = target.getAttribute(eventNameSelector) || DEFAULT_EVENT_NAME;
79
+ // Parse attributes from the element
80
+ var elementAttributes_1 = {};
81
+ var rawElementAttributes = target.getAttribute(attrSelector);
82
+ rawElementAttributes === null || rawElementAttributes === void 0 ? void 0 : rawElementAttributes.split(/\s*,\s*/).forEach(function (attr) {
83
+ var tmp = attr.trim().split(/\s*:\s*/);
84
+ elementAttributes_1[tmp[0]] = tmp[1];
85
+ });
86
+ // Assemble final list of attributes
87
+ var attributes = Object.assign({
88
+ type: event.type,
89
+ target: "".concat(target.localName, " with id ").concat(target.id),
90
+ }, this.options.attributes, elementAttributes_1);
91
+ logger.debug('Recording automatically tracked DOM event', {
92
+ eventName: eventName,
93
+ attributes: attributes,
94
+ });
95
+ this.eventRecorder(eventName, attributes);
96
+ }
97
+ }
98
+ };
99
+ return EventTracker;
100
+ }());
101
+ export { EventTracker };
@@ -0,0 +1,18 @@
1
+ import { PageViewTrackingOptions, TrackerEventRecorder, TrackerInterface } from '../types/trackers';
2
+ export declare class PageViewTracker implements TrackerInterface {
3
+ private trackerActive;
4
+ private options;
5
+ private eventRecorder;
6
+ private spaTrackingActive;
7
+ private pushStateProxy?;
8
+ private replaceStateProxy?;
9
+ private originalPushState;
10
+ private originalReplaceState;
11
+ constructor(eventRecorder: TrackerEventRecorder, options?: PageViewTrackingOptions);
12
+ configure(eventRecorder: TrackerEventRecorder, options?: PageViewTrackingOptions): void;
13
+ cleanup(): void;
14
+ private setupSPATracking;
15
+ private setupMPATracking;
16
+ private handleLocationChange;
17
+ private urlHasChanged;
18
+ }
@@ -0,0 +1,113 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { ConsoleLogger } from '@aws-amplify/core';
4
+ import { isBrowser } from '@aws-amplify/core/internals/utils';
5
+ var logger = new ConsoleLogger('PageViewTracker');
6
+ var DEFAULT_EVENT_NAME = 'pageView';
7
+ var DEFAULT_APP_TYPE = 'singlePage';
8
+ var DEFAULT_URL_PROVIDER = function () {
9
+ return window.location.origin + window.location.pathname;
10
+ };
11
+ var PREV_URL_STORAGE_KEY = 'aws-amplify-analytics-prevUrl';
12
+ var PageViewTracker = /** @class */ (function () {
13
+ function PageViewTracker(eventRecorder, options) {
14
+ this.options = {};
15
+ this.trackerActive = true;
16
+ this.eventRecorder = eventRecorder;
17
+ this.spaTrackingActive = false;
18
+ this.handleLocationChange = this.handleLocationChange.bind(this);
19
+ this.configure(eventRecorder, options);
20
+ }
21
+ PageViewTracker.prototype.configure = function (eventRecorder, options) {
22
+ var _a, _b, _c, _d, _e, _f;
23
+ this.eventRecorder = eventRecorder;
24
+ // Clean up any existing listeners
25
+ this.cleanup();
26
+ // Apply defaults
27
+ this.options = {
28
+ appType: (_a = options === null || options === void 0 ? void 0 : options.appType) !== null && _a !== void 0 ? _a : DEFAULT_APP_TYPE,
29
+ attributes: (_b = options === null || options === void 0 ? void 0 : options.attributes) !== null && _b !== void 0 ? _b : undefined,
30
+ eventName: (_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.eventName) !== null && _d !== void 0 ? _d : DEFAULT_EVENT_NAME,
31
+ urlProvider: (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.urlProvider) !== null && _f !== void 0 ? _f : DEFAULT_URL_PROVIDER,
32
+ };
33
+ // Configure SPA or MPA page view tracking
34
+ if (isBrowser()) {
35
+ if (this.options.appType === 'singlePage') {
36
+ this.setupSPATracking();
37
+ }
38
+ else {
39
+ this.setupMPATracking();
40
+ }
41
+ this.trackerActive = true;
42
+ }
43
+ };
44
+ PageViewTracker.prototype.cleanup = function () {
45
+ var _a, _b;
46
+ // No-op if document listener is not active
47
+ if (!this.trackerActive) {
48
+ return;
49
+ }
50
+ // Clean up SPA page view listeners
51
+ if (this.spaTrackingActive) {
52
+ window.history.pushState = this.originalPushState;
53
+ window.history.replaceState = this.originalReplaceState;
54
+ (_a = this.pushStateProxy) === null || _a === void 0 ? void 0 : _a.revoke();
55
+ (_b = this.replaceStateProxy) === null || _b === void 0 ? void 0 : _b.revoke();
56
+ window.removeEventListener('popstate', this.handleLocationChange);
57
+ this.spaTrackingActive = false;
58
+ }
59
+ };
60
+ PageViewTracker.prototype.setupSPATracking = function () {
61
+ var _this = this;
62
+ if (!this.spaTrackingActive) {
63
+ // Configure proxies on History APIs
64
+ this.pushStateProxy = Proxy.revocable(window.history.pushState, {
65
+ apply: function (target, thisArg, args) {
66
+ var proxiedResult = target.apply(thisArg, args);
67
+ _this.handleLocationChange();
68
+ return proxiedResult;
69
+ },
70
+ });
71
+ this.replaceStateProxy = Proxy.revocable(window.history.replaceState, {
72
+ apply: function (target, thisArg, args) {
73
+ var proxiedResult = target.apply(thisArg, args);
74
+ _this.handleLocationChange();
75
+ return proxiedResult;
76
+ },
77
+ });
78
+ this.originalPushState = window.history.pushState;
79
+ this.originalReplaceState = window.history.replaceState;
80
+ window.history.pushState = this.pushStateProxy.proxy;
81
+ window.history.replaceState = this.replaceStateProxy.proxy;
82
+ window.addEventListener('popstate', this.handleLocationChange);
83
+ sessionStorage.removeItem(PREV_URL_STORAGE_KEY);
84
+ this.spaTrackingActive = true;
85
+ }
86
+ };
87
+ PageViewTracker.prototype.setupMPATracking = function () {
88
+ this.handleLocationChange();
89
+ };
90
+ PageViewTracker.prototype.handleLocationChange = function () {
91
+ var currentUrl = this.options.urlProvider();
92
+ var eventName = this.options.eventName || DEFAULT_EVENT_NAME;
93
+ if (this.urlHasChanged()) {
94
+ sessionStorage.setItem(PREV_URL_STORAGE_KEY, currentUrl);
95
+ // Assemble attribute list
96
+ var attributes = Object.assign({
97
+ url: currentUrl,
98
+ }, this.options.attributes);
99
+ logger.debug('Recording automatically tracked page view event', {
100
+ eventName: eventName,
101
+ attributes: attributes,
102
+ });
103
+ this.eventRecorder(eventName, attributes);
104
+ }
105
+ };
106
+ PageViewTracker.prototype.urlHasChanged = function () {
107
+ var prevUrl = sessionStorage.getItem(PREV_URL_STORAGE_KEY);
108
+ var currUrl = this.options.urlProvider();
109
+ return currUrl !== prevUrl;
110
+ };
111
+ return PageViewTracker;
112
+ }());
113
+ export { PageViewTracker };
@@ -0,0 +1,13 @@
1
+ import { SessionTrackingOptions, TrackerEventRecorder, TrackerInterface } from '../types/trackers';
2
+ export declare class SessionTracker implements TrackerInterface {
3
+ private options;
4
+ private eventRecorder;
5
+ private sessionTrackingActive;
6
+ private initialEventSent;
7
+ constructor(eventRecorder: TrackerEventRecorder, options?: SessionTrackingOptions);
8
+ configure(eventRecorder: TrackerEventRecorder, options?: SessionTrackingOptions): void;
9
+ cleanup(): void;
10
+ private handleStateChange;
11
+ private sessionStarted;
12
+ private sessionStopped;
13
+ }
@@ -0,0 +1,70 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { sessionListener, } from '@aws-amplify/core/internals/utils';
4
+ import { ConsoleLogger } from '@aws-amplify/core';
5
+ var SESSION_START_EVENT = '_session.start';
6
+ var SESSION_STOP_EVENT = '_session.stop';
7
+ var logger = new ConsoleLogger('SessionTracker');
8
+ var SessionTracker = /** @class */ (function () {
9
+ function SessionTracker(eventRecorder, options) {
10
+ this.options = {};
11
+ this.eventRecorder = eventRecorder;
12
+ this.sessionTrackingActive = false;
13
+ this.initialEventSent = false;
14
+ this.handleStateChange = this.handleStateChange.bind(this);
15
+ this.configure(eventRecorder, options);
16
+ }
17
+ SessionTracker.prototype.configure = function (eventRecorder, options) {
18
+ var _a;
19
+ this.eventRecorder = eventRecorder;
20
+ // Clean up any existing listeners
21
+ this.cleanup();
22
+ // Apply defaults
23
+ this.options = {
24
+ attributes: (_a = options === null || options === void 0 ? void 0 : options.attributes) !== null && _a !== void 0 ? _a : {},
25
+ };
26
+ // Setup state listeners
27
+ if (!this.sessionTrackingActive) {
28
+ sessionListener.addStateChangeListener(this.handleStateChange, !this.initialEventSent);
29
+ this.sessionTrackingActive = true;
30
+ }
31
+ };
32
+ SessionTracker.prototype.cleanup = function () {
33
+ if (this.sessionTrackingActive) {
34
+ sessionListener.removeStateChangeListener(this.handleStateChange);
35
+ }
36
+ this.sessionTrackingActive = false;
37
+ };
38
+ SessionTracker.prototype.handleStateChange = function (state) {
39
+ if (state === 'started') {
40
+ this.sessionStarted();
41
+ }
42
+ else {
43
+ this.sessionStopped();
44
+ }
45
+ };
46
+ SessionTracker.prototype.sessionStarted = function () {
47
+ var _a;
48
+ var attributes = (_a = this.options.attributes) !== null && _a !== void 0 ? _a : {};
49
+ logger.debug('Recording automatically tracked page view event', {
50
+ SESSION_START_EVENT: SESSION_START_EVENT,
51
+ attributes: attributes,
52
+ });
53
+ this.eventRecorder(SESSION_START_EVENT, attributes);
54
+ // NOTE: The initial event will not be re-sent on re-configuration (e.g. to add additional custom attributes)
55
+ if (!this.initialEventSent) {
56
+ this.initialEventSent = true;
57
+ }
58
+ };
59
+ SessionTracker.prototype.sessionStopped = function () {
60
+ var _a;
61
+ var attributes = (_a = this.options.attributes) !== null && _a !== void 0 ? _a : {};
62
+ logger.debug('Recording automatically tracked page view event', {
63
+ SESSION_STOP_EVENT: SESSION_STOP_EVENT,
64
+ attributes: attributes,
65
+ });
66
+ this.eventRecorder(SESSION_STOP_EVENT, attributes);
67
+ };
68
+ return SessionTracker;
69
+ }());
70
+ export { SessionTracker };
@@ -0,0 +1,13 @@
1
+ import { SessionTrackingOptions, TrackerEventRecorder, TrackerInterface } from '../types/trackers';
2
+ export declare class SessionTracker implements TrackerInterface {
3
+ private options;
4
+ private eventRecorder;
5
+ private initialEventSent;
6
+ private sessionTrackingActive;
7
+ constructor(eventRecorder: TrackerEventRecorder, options?: SessionTrackingOptions);
8
+ configure(eventRecorder: TrackerEventRecorder, options?: SessionTrackingOptions): void;
9
+ cleanup(): void;
10
+ private handleStateChange;
11
+ private sessionStarted;
12
+ private sessionStopped;
13
+ }
@@ -0,0 +1,70 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { sessionListener, } from '@aws-amplify/core/internals/utils';
4
+ import { ConsoleLogger } from '@aws-amplify/core';
5
+ var SESSION_START_EVENT = '_session.start';
6
+ var SESSION_STOP_EVENT = '_session.stop';
7
+ var logger = new ConsoleLogger('SessionTracker');
8
+ var SessionTracker = /** @class */ (function () {
9
+ function SessionTracker(eventRecorder, options) {
10
+ this.options = {};
11
+ this.eventRecorder = eventRecorder;
12
+ this.initialEventSent = false;
13
+ this.sessionTrackingActive = false;
14
+ this.handleStateChange = this.handleStateChange.bind(this);
15
+ this.configure(eventRecorder, options);
16
+ }
17
+ SessionTracker.prototype.configure = function (eventRecorder, options) {
18
+ var _a;
19
+ this.eventRecorder = eventRecorder;
20
+ // Clean up any existing listeners
21
+ this.cleanup();
22
+ // Apply defaults
23
+ this.options = {
24
+ attributes: (_a = options === null || options === void 0 ? void 0 : options.attributes) !== null && _a !== void 0 ? _a : {},
25
+ };
26
+ // Setup state listeners
27
+ if (!this.sessionTrackingActive) {
28
+ sessionListener.addStateChangeListener(this.handleStateChange, !this.initialEventSent);
29
+ this.sessionTrackingActive = true;
30
+ }
31
+ };
32
+ SessionTracker.prototype.cleanup = function () {
33
+ if (this.sessionTrackingActive) {
34
+ sessionListener.removeStateChangeListener(this.handleStateChange);
35
+ }
36
+ this.sessionTrackingActive = false;
37
+ };
38
+ SessionTracker.prototype.handleStateChange = function (state) {
39
+ if (state === 'started') {
40
+ this.sessionStarted();
41
+ }
42
+ else {
43
+ this.sessionStopped();
44
+ }
45
+ };
46
+ SessionTracker.prototype.sessionStarted = function () {
47
+ var _a;
48
+ var attributes = (_a = this.options.attributes) !== null && _a !== void 0 ? _a : {};
49
+ logger.debug('Recording automatically tracked page view event', {
50
+ SESSION_START_EVENT: SESSION_START_EVENT,
51
+ attributes: attributes,
52
+ });
53
+ this.eventRecorder(SESSION_START_EVENT, attributes);
54
+ // NOTE: The initial event will not be re-sent on re-configuration (e.g. to add additional custom attributes)
55
+ if (!this.initialEventSent) {
56
+ this.initialEventSent = true;
57
+ }
58
+ };
59
+ SessionTracker.prototype.sessionStopped = function () {
60
+ var _a;
61
+ var attributes = (_a = this.options.attributes) !== null && _a !== void 0 ? _a : {};
62
+ logger.debug('Recording automatically tracked page view event', {
63
+ SESSION_STOP_EVENT: SESSION_STOP_EVENT,
64
+ attributes: attributes,
65
+ });
66
+ this.eventRecorder(SESSION_STOP_EVENT, attributes);
67
+ };
68
+ return SessionTracker;
69
+ }());
70
+ export { SessionTracker };
@@ -0,0 +1,3 @@
1
+ export { EventTracker } from './EventTracker';
2
+ export { PageViewTracker } from './PageViewTracker';
3
+ export { SessionTracker } from './SessionTracker';
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { EventTracker } from './EventTracker';
4
+ export { PageViewTracker } from './PageViewTracker';
5
+ export { SessionTracker } from './SessionTracker';