@aws-amplify/analytics 7.0.1-console-preview.8d88eef.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 (95) 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/pinpoint/apis/configureAutoTrack.d.ts +15 -0
  6. package/lib/providers/pinpoint/apis/configureAutoTrack.js +35 -0
  7. package/lib/providers/pinpoint/apis/index.d.ts +1 -0
  8. package/lib/providers/pinpoint/apis/index.js +3 -1
  9. package/lib/providers/pinpoint/index.d.ts +2 -2
  10. package/lib/providers/pinpoint/index.js +2 -1
  11. package/lib/providers/pinpoint/types/index.d.ts +1 -1
  12. package/lib/providers/pinpoint/types/inputs.d.ts +5 -1
  13. package/lib/trackers/EventTracker.d.ts +10 -0
  14. package/lib/trackers/EventTracker.js +104 -0
  15. package/lib/trackers/PageViewTracker.d.ts +18 -0
  16. package/lib/trackers/PageViewTracker.js +116 -0
  17. package/lib/trackers/SessionTracker.d.ts +13 -0
  18. package/lib/trackers/SessionTracker.js +73 -0
  19. package/lib/trackers/SessionTracker.native.d.ts +13 -0
  20. package/lib/trackers/SessionTracker.native.js +73 -0
  21. package/lib/trackers/index.d.ts +3 -0
  22. package/lib/trackers/index.js +11 -0
  23. package/lib/tsconfig.tsbuildinfo +1 -1
  24. package/lib/types/index.d.ts +1 -2
  25. package/lib/types/inputs.d.ts +16 -0
  26. package/lib/types/trackers.d.ts +22 -0
  27. package/lib/utils/index.d.ts +2 -0
  28. package/lib/utils/index.js +5 -1
  29. package/lib/utils/trackerConfigHelpers.d.ts +5 -0
  30. package/lib/utils/trackerConfigHelpers.js +15 -0
  31. package/lib/utils/trackerConfigHelpers.native.d.ts +5 -0
  32. package/lib/utils/trackerConfigHelpers.native.js +14 -0
  33. package/lib/utils/trackerHelpers.d.ts +9 -0
  34. package/lib/utils/trackerHelpers.js +44 -0
  35. package/lib-esm/errors/validation.d.ts +2 -0
  36. package/lib-esm/errors/validation.js +8 -0
  37. package/lib-esm/index.d.ts +1 -1
  38. package/lib-esm/index.js +1 -1
  39. package/lib-esm/providers/pinpoint/apis/configureAutoTrack.d.ts +15 -0
  40. package/lib-esm/providers/pinpoint/apis/configureAutoTrack.js +31 -0
  41. package/lib-esm/providers/pinpoint/apis/index.d.ts +1 -0
  42. package/lib-esm/providers/pinpoint/apis/index.js +1 -0
  43. package/lib-esm/providers/pinpoint/apis/record.js +1 -1
  44. package/lib-esm/providers/pinpoint/index.d.ts +2 -2
  45. package/lib-esm/providers/pinpoint/index.js +1 -1
  46. package/lib-esm/providers/pinpoint/types/index.d.ts +1 -1
  47. package/lib-esm/providers/pinpoint/types/inputs.d.ts +5 -1
  48. package/lib-esm/trackers/EventTracker.d.ts +10 -0
  49. package/lib-esm/trackers/EventTracker.js +101 -0
  50. package/lib-esm/trackers/PageViewTracker.d.ts +18 -0
  51. package/lib-esm/trackers/PageViewTracker.js +113 -0
  52. package/lib-esm/trackers/SessionTracker.d.ts +13 -0
  53. package/lib-esm/trackers/SessionTracker.js +70 -0
  54. package/lib-esm/trackers/SessionTracker.native.d.ts +13 -0
  55. package/lib-esm/trackers/SessionTracker.native.js +70 -0
  56. package/lib-esm/trackers/index.d.ts +3 -0
  57. package/lib-esm/trackers/index.js +5 -0
  58. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  59. package/lib-esm/types/index.d.ts +1 -2
  60. package/lib-esm/types/inputs.d.ts +16 -0
  61. package/lib-esm/types/trackers.d.ts +22 -0
  62. package/lib-esm/utils/index.d.ts +2 -0
  63. package/lib-esm/utils/index.js +2 -0
  64. package/lib-esm/utils/trackerConfigHelpers.d.ts +5 -0
  65. package/lib-esm/utils/trackerConfigHelpers.js +11 -0
  66. package/lib-esm/utils/trackerConfigHelpers.native.d.ts +5 -0
  67. package/lib-esm/utils/trackerConfigHelpers.native.js +10 -0
  68. package/lib-esm/utils/trackerHelpers.d.ts +9 -0
  69. package/lib-esm/utils/trackerHelpers.js +40 -0
  70. package/package.json +5 -4
  71. package/src/errors/validation.ts +8 -0
  72. package/src/index.ts +3 -1
  73. package/src/providers/pinpoint/apis/configureAutoTrack.ts +49 -0
  74. package/src/providers/pinpoint/apis/index.ts +1 -0
  75. package/src/providers/pinpoint/apis/record.ts +1 -1
  76. package/src/providers/pinpoint/index.ts +6 -2
  77. package/src/providers/pinpoint/types/index.ts +5 -1
  78. package/src/providers/pinpoint/types/inputs.ts +9 -1
  79. package/src/trackers/EventTracker.ts +135 -0
  80. package/src/trackers/PageViewTracker.ts +160 -0
  81. package/src/trackers/SessionTracker.native.ts +106 -0
  82. package/src/trackers/SessionTracker.ts +106 -0
  83. package/src/trackers/index.ts +6 -0
  84. package/src/types/index.ts +4 -13
  85. package/src/types/inputs.ts +25 -0
  86. package/src/types/trackers.ts +35 -0
  87. package/src/utils/index.ts +2 -0
  88. package/src/utils/trackerConfigHelpers.native.ts +18 -0
  89. package/src/utils/trackerConfigHelpers.ts +19 -0
  90. package/src/utils/trackerHelpers.ts +57 -0
  91. package/lib/types/analytics.d.ts +0 -50
  92. package/lib-esm/types/analytics.d.ts +0 -50
  93. package/src/types/analytics.ts +0 -70
  94. /package/lib/types/{analytics.js → trackers.js} +0 -0
  95. /package/lib-esm/types/{analytics.js → trackers.js} +0 -0
@@ -4,6 +4,8 @@ export declare enum AnalyticsValidationErrorCode {
4
4
  NoCredentials = "NoCredentials",
5
5
  NoEventName = "NoEventName",
6
6
  NoRegion = "NoRegion",
7
+ InvalidTracker = "InvalidTracker",
8
+ UnsupportedPlatform = "UnsupportedPlatform",
7
9
  NoTrackingId = "NoTrackingId",
8
10
  InvalidFlushSize = "InvalidFlushSize"
9
11
  }
@@ -10,6 +10,8 @@ var AnalyticsValidationErrorCode;
10
10
  AnalyticsValidationErrorCode["NoCredentials"] = "NoCredentials";
11
11
  AnalyticsValidationErrorCode["NoEventName"] = "NoEventName";
12
12
  AnalyticsValidationErrorCode["NoRegion"] = "NoRegion";
13
+ AnalyticsValidationErrorCode["InvalidTracker"] = "InvalidTracker";
14
+ AnalyticsValidationErrorCode["UnsupportedPlatform"] = "UnsupportedPlatform";
13
15
  AnalyticsValidationErrorCode["NoTrackingId"] = "NoTrackingId";
14
16
  AnalyticsValidationErrorCode["InvalidFlushSize"] = "InvalidFlushSize";
15
17
  })(AnalyticsValidationErrorCode = exports.AnalyticsValidationErrorCode || (exports.AnalyticsValidationErrorCode = {}));
@@ -26,6 +28,12 @@ exports.validationErrorMap = (_a = {},
26
28
  _a[AnalyticsValidationErrorCode.NoRegion] = {
27
29
  message: 'Missing region.',
28
30
  },
31
+ _a[AnalyticsValidationErrorCode.InvalidTracker] = {
32
+ message: 'Invalid tracker type specified.',
33
+ },
34
+ _a[AnalyticsValidationErrorCode.UnsupportedPlatform] = {
35
+ message: 'Only session tracking is supported on React Native.',
36
+ },
29
37
  _a[AnalyticsValidationErrorCode.InvalidFlushSize] = {
30
38
  message: 'Invalid FlushSize, it should be smaller than BufferSize',
31
39
  },
package/lib/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { record, identifyUser, RecordInput, IdentifyUserInput, flushEvents, } from './providers/pinpoint';
1
+ export { record, identifyUser, configureAutoTrack, flushEvents, RecordInput, IdentifyUserInput, ConfigureAutoTrackInput, } from './providers/pinpoint';
2
2
  export { enable, disable } from './apis';
3
3
  export { AnalyticsError } from './errors';
package/lib/index.js CHANGED
@@ -2,10 +2,11 @@
2
2
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.AnalyticsError = exports.disable = exports.enable = exports.flushEvents = exports.identifyUser = exports.record = void 0;
5
+ exports.AnalyticsError = exports.disable = exports.enable = exports.flushEvents = exports.configureAutoTrack = exports.identifyUser = exports.record = void 0;
6
6
  var pinpoint_1 = require("./providers/pinpoint");
7
7
  Object.defineProperty(exports, "record", { enumerable: true, get: function () { return pinpoint_1.record; } });
8
8
  Object.defineProperty(exports, "identifyUser", { enumerable: true, get: function () { return pinpoint_1.identifyUser; } });
9
+ Object.defineProperty(exports, "configureAutoTrack", { enumerable: true, get: function () { return pinpoint_1.configureAutoTrack; } });
9
10
  Object.defineProperty(exports, "flushEvents", { enumerable: true, get: function () { return pinpoint_1.flushEvents; } });
10
11
  var apis_1 = require("./apis");
11
12
  Object.defineProperty(exports, "enable", { enumerable: true, get: function () { return apis_1.enable; } });
@@ -0,0 +1,15 @@
1
+ import { ConfigureAutoTrackInput } from '../types';
2
+ /**
3
+ * Configures automatic event tracking for Pinpoint. This API will automatically transmit an analytic event when
4
+ * configured events are detected within your application. This can include: DOM element events (via the `event`
5
+ * tracker), session events (via the `session` tracker), and page view events (via the `pageView` tracker).
6
+ *
7
+ * @remark Only session tracking is currently supported on React Native.
8
+ *
9
+ * @param {ConfigureAutoTrackInput} params The input object to configure auto track behavior.
10
+ *
11
+ * @throws service: {@link UpdateEndpointException} - Thrown when the underlying Pinpoint service returns an error.
12
+ * @throws validation: {@link AnalyticsValidationErrorCode} - Thrown when the provided parameters or library
13
+ * configuration is incorrect.
14
+ */
15
+ export declare const configureAutoTrack: (input: ConfigureAutoTrackInput) => void;
@@ -0,0 +1,35 @@
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.configureAutoTrack = void 0;
6
+ var utils_1 = require("../../../utils");
7
+ var record_1 = require("./record");
8
+ // Configured Tracker instances for Pinpoint
9
+ var configuredTrackers = {};
10
+ // Callback that will emit an appropriate event to Pinpoint when required by the Tracker
11
+ var emitTrackingEvent = function (eventName, attributes) {
12
+ (0, record_1.record)({
13
+ name: eventName,
14
+ attributes: attributes,
15
+ });
16
+ };
17
+ /**
18
+ * Configures automatic event tracking for Pinpoint. This API will automatically transmit an analytic event when
19
+ * configured events are detected within your application. This can include: DOM element events (via the `event`
20
+ * tracker), session events (via the `session` tracker), and page view events (via the `pageView` tracker).
21
+ *
22
+ * @remark Only session tracking is currently supported on React Native.
23
+ *
24
+ * @param {ConfigureAutoTrackInput} params The input object to configure auto track behavior.
25
+ *
26
+ * @throws service: {@link UpdateEndpointException} - Thrown when the underlying Pinpoint service returns an error.
27
+ * @throws validation: {@link AnalyticsValidationErrorCode} - Thrown when the provided parameters or library
28
+ * configuration is incorrect.
29
+ */
30
+ var configureAutoTrack = function (input) {
31
+ (0, utils_1.validateTrackerConfiguration)(input);
32
+ // Initialize or update this provider's trackers
33
+ (0, utils_1.updateProviderTrackers)(input, emitTrackingEvent, configuredTrackers);
34
+ };
35
+ exports.configureAutoTrack = configureAutoTrack;
@@ -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,10 +2,12 @@
2
2
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.flushEvents = exports.identifyUser = exports.record = void 0;
5
+ exports.flushEvents = exports.configureAutoTrack = exports.identifyUser = exports.record = void 0;
6
6
  var record_1 = require("./record");
7
7
  Object.defineProperty(exports, "record", { enumerable: true, get: function () { return record_1.record; } });
8
8
  var identifyUser_1 = require("./identifyUser");
9
9
  Object.defineProperty(exports, "identifyUser", { enumerable: true, get: function () { return identifyUser_1.identifyUser; } });
10
+ var configureAutoTrack_1 = require("./configureAutoTrack");
11
+ Object.defineProperty(exports, "configureAutoTrack", { enumerable: true, get: function () { return configureAutoTrack_1.configureAutoTrack; } });
10
12
  var flushEvents_1 = require("./flushEvents");
11
13
  Object.defineProperty(exports, "flushEvents", { enumerable: true, get: function () { return flushEvents_1.flushEvents; } });
@@ -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';
@@ -2,8 +2,9 @@
2
2
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.flushEvents = exports.identifyUser = exports.record = void 0;
5
+ exports.configureAutoTrack = exports.flushEvents = exports.identifyUser = exports.record = void 0;
6
6
  var apis_1 = require("./apis");
7
7
  Object.defineProperty(exports, "record", { enumerable: true, get: function () { return apis_1.record; } });
8
8
  Object.defineProperty(exports, "identifyUser", { enumerable: true, get: function () { return apis_1.identifyUser; } });
9
9
  Object.defineProperty(exports, "flushEvents", { enumerable: true, get: function () { return apis_1.flushEvents; } });
10
+ Object.defineProperty(exports, "configureAutoTrack", { enumerable: true, get: function () { return apis_1.configureAutoTrack; } });
@@ -1,2 +1,2 @@
1
- export { RecordInput, IdentifyUserInput } from './inputs';
1
+ export { RecordInput, IdentifyUserInput, ConfigureAutoTrackInput, } from './inputs';
2
2
  export { IdentifyUserOptions } from './options';
@@ -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;
@@ -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,104 @@
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.EventTracker = void 0;
6
+ var core_1 = require("@aws-amplify/core");
7
+ var utils_1 = require("@aws-amplify/core/internals/utils");
8
+ var DEFAULT_EVENTS = ['click'];
9
+ var DEFAULT_SELECTOR_PREFIX = 'data-amplify-analytics-';
10
+ var DEFAULT_EVENT_NAME = 'event'; // Default event name as sent to the analytics provider
11
+ var logger = new core_1.ConsoleLogger('EventTracker');
12
+ var EventTracker = /** @class */ (function () {
13
+ function EventTracker(eventRecorder, options) {
14
+ this.options = {};
15
+ this.trackerActive = false;
16
+ this.eventRecorder = eventRecorder;
17
+ this.handleDocEvent = this.handleDocEvent.bind(this);
18
+ this.configure(eventRecorder, options);
19
+ }
20
+ EventTracker.prototype.configure = function (eventRecorder, options) {
21
+ var _this = this;
22
+ var _a, _b, _c, _d;
23
+ this.eventRecorder = eventRecorder;
24
+ // Clean up any existing listeners
25
+ this.cleanup();
26
+ // Apply defaults
27
+ this.options = {
28
+ attributes: (_a = options === null || options === void 0 ? void 0 : options.attributes) !== null && _a !== void 0 ? _a : undefined,
29
+ events: (_b = options === null || options === void 0 ? void 0 : options.events) !== null && _b !== void 0 ? _b : DEFAULT_EVENTS,
30
+ selectorPrefix: (_c = options === null || options === void 0 ? void 0 : options.selectorPrefix) !== null && _c !== void 0 ? _c : DEFAULT_SELECTOR_PREFIX,
31
+ };
32
+ // Register event listeners
33
+ if ((0, utils_1.isBrowser)()) {
34
+ (_d = this.options.events) === null || _d === void 0 ? void 0 : _d.forEach(function (targetEvent) {
35
+ document.addEventListener(targetEvent, _this.handleDocEvent, {
36
+ capture: true,
37
+ });
38
+ });
39
+ this.trackerActive = true;
40
+ }
41
+ };
42
+ EventTracker.prototype.cleanup = function () {
43
+ var _this = this;
44
+ var _a;
45
+ // No-op if document listener is not active
46
+ if (!this.trackerActive) {
47
+ return;
48
+ }
49
+ // Clean up event listeners
50
+ (_a = this.options.events) === null || _a === void 0 ? void 0 : _a.forEach(function (targetEvent) {
51
+ document.removeEventListener(targetEvent, _this.handleDocEvent, {
52
+ capture: true,
53
+ });
54
+ });
55
+ };
56
+ EventTracker.prototype.handleDocEvent = function (event) {
57
+ var _a;
58
+ /**
59
+ * Example DOM element:
60
+ *
61
+ * ```
62
+ * <button
63
+ * data-amplify-analytics-on="click"
64
+ * data-amplify-analytics-name="click"
65
+ * data-amplify-analytics-attrs="attr1:attr1_value,attr2:attr2_value"
66
+ * />
67
+ * ```
68
+ */
69
+ var triggerSelector = "[".concat(this.options.selectorPrefix, "on]");
70
+ var attrSelector = "".concat(this.options.selectorPrefix, "attrs");
71
+ var eventNameSelector = "".concat(this.options.selectorPrefix, "name");
72
+ var eventSource = event.target;
73
+ // Validate that the triggering event type is being tracked
74
+ if (!((_a = this.options.events) === null || _a === void 0 ? void 0 : _a.includes(event.type))) {
75
+ return;
76
+ }
77
+ if (eventSource instanceof HTMLElement) {
78
+ var target = eventSource.closest(triggerSelector);
79
+ if (target) {
80
+ // Parse event name from the element
81
+ var eventName = target.getAttribute(eventNameSelector) || DEFAULT_EVENT_NAME;
82
+ // Parse attributes from the element
83
+ var elementAttributes_1 = {};
84
+ var rawElementAttributes = target.getAttribute(attrSelector);
85
+ rawElementAttributes === null || rawElementAttributes === void 0 ? void 0 : rawElementAttributes.split(/\s*,\s*/).forEach(function (attr) {
86
+ var tmp = attr.trim().split(/\s*:\s*/);
87
+ elementAttributes_1[tmp[0]] = tmp[1];
88
+ });
89
+ // Assemble final list of attributes
90
+ var attributes = Object.assign({
91
+ type: event.type,
92
+ target: "".concat(target.localName, " with id ").concat(target.id),
93
+ }, this.options.attributes, elementAttributes_1);
94
+ logger.debug('Recording automatically tracked DOM event', {
95
+ eventName: eventName,
96
+ attributes: attributes,
97
+ });
98
+ this.eventRecorder(eventName, attributes);
99
+ }
100
+ }
101
+ };
102
+ return EventTracker;
103
+ }());
104
+ exports.EventTracker = 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,116 @@
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.PageViewTracker = void 0;
6
+ var core_1 = require("@aws-amplify/core");
7
+ var utils_1 = require("@aws-amplify/core/internals/utils");
8
+ var logger = new core_1.ConsoleLogger('PageViewTracker');
9
+ var DEFAULT_EVENT_NAME = 'pageView';
10
+ var DEFAULT_APP_TYPE = 'singlePage';
11
+ var DEFAULT_URL_PROVIDER = function () {
12
+ return window.location.origin + window.location.pathname;
13
+ };
14
+ var PREV_URL_STORAGE_KEY = 'aws-amplify-analytics-prevUrl';
15
+ var PageViewTracker = /** @class */ (function () {
16
+ function PageViewTracker(eventRecorder, options) {
17
+ this.options = {};
18
+ this.trackerActive = true;
19
+ this.eventRecorder = eventRecorder;
20
+ this.spaTrackingActive = false;
21
+ this.handleLocationChange = this.handleLocationChange.bind(this);
22
+ this.configure(eventRecorder, options);
23
+ }
24
+ PageViewTracker.prototype.configure = function (eventRecorder, options) {
25
+ var _a, _b, _c, _d, _e, _f;
26
+ this.eventRecorder = eventRecorder;
27
+ // Clean up any existing listeners
28
+ this.cleanup();
29
+ // Apply defaults
30
+ this.options = {
31
+ appType: (_a = options === null || options === void 0 ? void 0 : options.appType) !== null && _a !== void 0 ? _a : DEFAULT_APP_TYPE,
32
+ attributes: (_b = options === null || options === void 0 ? void 0 : options.attributes) !== null && _b !== void 0 ? _b : undefined,
33
+ eventName: (_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.eventName) !== null && _d !== void 0 ? _d : DEFAULT_EVENT_NAME,
34
+ urlProvider: (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.urlProvider) !== null && _f !== void 0 ? _f : DEFAULT_URL_PROVIDER,
35
+ };
36
+ // Configure SPA or MPA page view tracking
37
+ if ((0, utils_1.isBrowser)()) {
38
+ if (this.options.appType === 'singlePage') {
39
+ this.setupSPATracking();
40
+ }
41
+ else {
42
+ this.setupMPATracking();
43
+ }
44
+ this.trackerActive = true;
45
+ }
46
+ };
47
+ PageViewTracker.prototype.cleanup = function () {
48
+ var _a, _b;
49
+ // No-op if document listener is not active
50
+ if (!this.trackerActive) {
51
+ return;
52
+ }
53
+ // Clean up SPA page view listeners
54
+ if (this.spaTrackingActive) {
55
+ window.history.pushState = this.originalPushState;
56
+ window.history.replaceState = this.originalReplaceState;
57
+ (_a = this.pushStateProxy) === null || _a === void 0 ? void 0 : _a.revoke();
58
+ (_b = this.replaceStateProxy) === null || _b === void 0 ? void 0 : _b.revoke();
59
+ window.removeEventListener('popstate', this.handleLocationChange);
60
+ this.spaTrackingActive = false;
61
+ }
62
+ };
63
+ PageViewTracker.prototype.setupSPATracking = function () {
64
+ var _this = this;
65
+ if (!this.spaTrackingActive) {
66
+ // Configure proxies on History APIs
67
+ this.pushStateProxy = Proxy.revocable(window.history.pushState, {
68
+ apply: function (target, thisArg, args) {
69
+ var proxiedResult = target.apply(thisArg, args);
70
+ _this.handleLocationChange();
71
+ return proxiedResult;
72
+ },
73
+ });
74
+ this.replaceStateProxy = Proxy.revocable(window.history.replaceState, {
75
+ apply: function (target, thisArg, args) {
76
+ var proxiedResult = target.apply(thisArg, args);
77
+ _this.handleLocationChange();
78
+ return proxiedResult;
79
+ },
80
+ });
81
+ this.originalPushState = window.history.pushState;
82
+ this.originalReplaceState = window.history.replaceState;
83
+ window.history.pushState = this.pushStateProxy.proxy;
84
+ window.history.replaceState = this.replaceStateProxy.proxy;
85
+ window.addEventListener('popstate', this.handleLocationChange);
86
+ sessionStorage.removeItem(PREV_URL_STORAGE_KEY);
87
+ this.spaTrackingActive = true;
88
+ }
89
+ };
90
+ PageViewTracker.prototype.setupMPATracking = function () {
91
+ this.handleLocationChange();
92
+ };
93
+ PageViewTracker.prototype.handleLocationChange = function () {
94
+ var currentUrl = this.options.urlProvider();
95
+ var eventName = this.options.eventName || DEFAULT_EVENT_NAME;
96
+ if (this.urlHasChanged()) {
97
+ sessionStorage.setItem(PREV_URL_STORAGE_KEY, currentUrl);
98
+ // Assemble attribute list
99
+ var attributes = Object.assign({
100
+ url: currentUrl,
101
+ }, this.options.attributes);
102
+ logger.debug('Recording automatically tracked page view event', {
103
+ eventName: eventName,
104
+ attributes: attributes,
105
+ });
106
+ this.eventRecorder(eventName, attributes);
107
+ }
108
+ };
109
+ PageViewTracker.prototype.urlHasChanged = function () {
110
+ var prevUrl = sessionStorage.getItem(PREV_URL_STORAGE_KEY);
111
+ var currUrl = this.options.urlProvider();
112
+ return currUrl !== prevUrl;
113
+ };
114
+ return PageViewTracker;
115
+ }());
116
+ exports.PageViewTracker = 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,73 @@
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.SessionTracker = void 0;
6
+ var utils_1 = require("@aws-amplify/core/internals/utils");
7
+ var core_1 = require("@aws-amplify/core");
8
+ var SESSION_START_EVENT = '_session.start';
9
+ var SESSION_STOP_EVENT = '_session.stop';
10
+ var logger = new core_1.ConsoleLogger('SessionTracker');
11
+ var SessionTracker = /** @class */ (function () {
12
+ function SessionTracker(eventRecorder, options) {
13
+ this.options = {};
14
+ this.eventRecorder = eventRecorder;
15
+ this.sessionTrackingActive = false;
16
+ this.initialEventSent = false;
17
+ this.handleStateChange = this.handleStateChange.bind(this);
18
+ this.configure(eventRecorder, options);
19
+ }
20
+ SessionTracker.prototype.configure = function (eventRecorder, options) {
21
+ var _a;
22
+ this.eventRecorder = eventRecorder;
23
+ // Clean up any existing listeners
24
+ this.cleanup();
25
+ // Apply defaults
26
+ this.options = {
27
+ attributes: (_a = options === null || options === void 0 ? void 0 : options.attributes) !== null && _a !== void 0 ? _a : {},
28
+ };
29
+ // Setup state listeners
30
+ if (!this.sessionTrackingActive) {
31
+ utils_1.sessionListener.addStateChangeListener(this.handleStateChange, !this.initialEventSent);
32
+ this.sessionTrackingActive = true;
33
+ }
34
+ };
35
+ SessionTracker.prototype.cleanup = function () {
36
+ if (this.sessionTrackingActive) {
37
+ utils_1.sessionListener.removeStateChangeListener(this.handleStateChange);
38
+ }
39
+ this.sessionTrackingActive = false;
40
+ };
41
+ SessionTracker.prototype.handleStateChange = function (state) {
42
+ if (state === 'started') {
43
+ this.sessionStarted();
44
+ }
45
+ else {
46
+ this.sessionStopped();
47
+ }
48
+ };
49
+ SessionTracker.prototype.sessionStarted = function () {
50
+ var _a;
51
+ var attributes = (_a = this.options.attributes) !== null && _a !== void 0 ? _a : {};
52
+ logger.debug('Recording automatically tracked page view event', {
53
+ SESSION_START_EVENT: SESSION_START_EVENT,
54
+ attributes: attributes,
55
+ });
56
+ this.eventRecorder(SESSION_START_EVENT, attributes);
57
+ // NOTE: The initial event will not be re-sent on re-configuration (e.g. to add additional custom attributes)
58
+ if (!this.initialEventSent) {
59
+ this.initialEventSent = true;
60
+ }
61
+ };
62
+ SessionTracker.prototype.sessionStopped = function () {
63
+ var _a;
64
+ var attributes = (_a = this.options.attributes) !== null && _a !== void 0 ? _a : {};
65
+ logger.debug('Recording automatically tracked page view event', {
66
+ SESSION_STOP_EVENT: SESSION_STOP_EVENT,
67
+ attributes: attributes,
68
+ });
69
+ this.eventRecorder(SESSION_STOP_EVENT, attributes);
70
+ };
71
+ return SessionTracker;
72
+ }());
73
+ exports.SessionTracker = 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,73 @@
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.SessionTracker = void 0;
6
+ var utils_1 = require("@aws-amplify/core/internals/utils");
7
+ var core_1 = require("@aws-amplify/core");
8
+ var SESSION_START_EVENT = '_session.start';
9
+ var SESSION_STOP_EVENT = '_session.stop';
10
+ var logger = new core_1.ConsoleLogger('SessionTracker');
11
+ var SessionTracker = /** @class */ (function () {
12
+ function SessionTracker(eventRecorder, options) {
13
+ this.options = {};
14
+ this.eventRecorder = eventRecorder;
15
+ this.initialEventSent = false;
16
+ this.sessionTrackingActive = false;
17
+ this.handleStateChange = this.handleStateChange.bind(this);
18
+ this.configure(eventRecorder, options);
19
+ }
20
+ SessionTracker.prototype.configure = function (eventRecorder, options) {
21
+ var _a;
22
+ this.eventRecorder = eventRecorder;
23
+ // Clean up any existing listeners
24
+ this.cleanup();
25
+ // Apply defaults
26
+ this.options = {
27
+ attributes: (_a = options === null || options === void 0 ? void 0 : options.attributes) !== null && _a !== void 0 ? _a : {},
28
+ };
29
+ // Setup state listeners
30
+ if (!this.sessionTrackingActive) {
31
+ utils_1.sessionListener.addStateChangeListener(this.handleStateChange, !this.initialEventSent);
32
+ this.sessionTrackingActive = true;
33
+ }
34
+ };
35
+ SessionTracker.prototype.cleanup = function () {
36
+ if (this.sessionTrackingActive) {
37
+ utils_1.sessionListener.removeStateChangeListener(this.handleStateChange);
38
+ }
39
+ this.sessionTrackingActive = false;
40
+ };
41
+ SessionTracker.prototype.handleStateChange = function (state) {
42
+ if (state === 'started') {
43
+ this.sessionStarted();
44
+ }
45
+ else {
46
+ this.sessionStopped();
47
+ }
48
+ };
49
+ SessionTracker.prototype.sessionStarted = function () {
50
+ var _a;
51
+ var attributes = (_a = this.options.attributes) !== null && _a !== void 0 ? _a : {};
52
+ logger.debug('Recording automatically tracked page view event', {
53
+ SESSION_START_EVENT: SESSION_START_EVENT,
54
+ attributes: attributes,
55
+ });
56
+ this.eventRecorder(SESSION_START_EVENT, attributes);
57
+ // NOTE: The initial event will not be re-sent on re-configuration (e.g. to add additional custom attributes)
58
+ if (!this.initialEventSent) {
59
+ this.initialEventSent = true;
60
+ }
61
+ };
62
+ SessionTracker.prototype.sessionStopped = function () {
63
+ var _a;
64
+ var attributes = (_a = this.options.attributes) !== null && _a !== void 0 ? _a : {};
65
+ logger.debug('Recording automatically tracked page view event', {
66
+ SESSION_STOP_EVENT: SESSION_STOP_EVENT,
67
+ attributes: attributes,
68
+ });
69
+ this.eventRecorder(SESSION_STOP_EVENT, attributes);
70
+ };
71
+ return SessionTracker;
72
+ }());
73
+ exports.SessionTracker = SessionTracker;
@@ -0,0 +1,3 @@
1
+ export { EventTracker } from './EventTracker';
2
+ export { PageViewTracker } from './PageViewTracker';
3
+ export { SessionTracker } from './SessionTracker';
@@ -0,0 +1,11 @@
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.SessionTracker = exports.PageViewTracker = exports.EventTracker = void 0;
6
+ var EventTracker_1 = require("./EventTracker");
7
+ Object.defineProperty(exports, "EventTracker", { enumerable: true, get: function () { return EventTracker_1.EventTracker; } });
8
+ var PageViewTracker_1 = require("./PageViewTracker");
9
+ Object.defineProperty(exports, "PageViewTracker", { enumerable: true, get: function () { return PageViewTracker_1.PageViewTracker; } });
10
+ var SessionTracker_1 = require("./SessionTracker");
11
+ Object.defineProperty(exports, "SessionTracker", { enumerable: true, get: function () { return SessionTracker_1.SessionTracker; } });