@aws-amplify/analytics 7.0.1-console-preview.b5a42af.0 → 7.0.1-console-preview.4ae78ed.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 (234) hide show
  1. package/lib/errors/assertValidationError.d.ts +1 -1
  2. package/lib/errors/assertValidationError.js +7 -3
  3. package/lib/errors/validation.d.ts +3 -1
  4. package/lib/errors/validation.js +8 -0
  5. package/lib/index.d.ts +1 -1
  6. package/lib/index.js +2 -1
  7. package/lib/providers/kinesis/apis/flushEvents.d.ts +8 -0
  8. package/lib/providers/kinesis/apis/flushEvents.js +37 -0
  9. package/lib/providers/kinesis/apis/index.d.ts +1 -0
  10. package/lib/providers/kinesis/apis/index.js +3 -1
  11. package/lib/providers/kinesis/apis/record.d.ts +1 -1
  12. package/lib/providers/kinesis/apis/record.js +40 -2
  13. package/lib/providers/kinesis/index.d.ts +1 -1
  14. package/lib/providers/kinesis/index.js +2 -1
  15. package/lib/providers/kinesis/types/buffer.d.ts +15 -0
  16. package/lib/providers/kinesis/types/buffer.js +4 -0
  17. package/lib/providers/kinesis/types/index.d.ts +1 -0
  18. package/lib/providers/kinesis/types/inputs.d.ts +6 -1
  19. package/lib/providers/kinesis/utils/constants.d.ts +6 -0
  20. package/lib/providers/kinesis/utils/constants.js +11 -0
  21. package/lib/providers/kinesis/utils/getEventBuffer.d.ts +3 -0
  22. package/lib/providers/kinesis/utils/getEventBuffer.js +103 -0
  23. package/lib/providers/kinesis/utils/resolveConfig.d.ts +7 -0
  24. package/lib/providers/kinesis/utils/resolveConfig.js +24 -0
  25. package/lib/providers/kinesis-firehose/apis/flushEvents.d.ts +8 -0
  26. package/lib/providers/kinesis-firehose/apis/flushEvents.js +36 -0
  27. package/lib/providers/kinesis-firehose/apis/index.d.ts +1 -0
  28. package/lib/providers/kinesis-firehose/apis/index.js +3 -1
  29. package/lib/providers/kinesis-firehose/apis/record.d.ts +1 -1
  30. package/lib/providers/kinesis-firehose/apis/record.js +37 -2
  31. package/lib/providers/kinesis-firehose/index.d.ts +1 -1
  32. package/lib/providers/kinesis-firehose/index.js +2 -1
  33. package/lib/providers/kinesis-firehose/types/buffer.d.ts +15 -0
  34. package/lib/providers/kinesis-firehose/types/buffer.js +4 -0
  35. package/lib/providers/kinesis-firehose/types/index.d.ts +1 -0
  36. package/lib/providers/kinesis-firehose/types/inputs.d.ts +5 -1
  37. package/lib/providers/kinesis-firehose/utils/constants.d.ts +6 -0
  38. package/lib/providers/kinesis-firehose/utils/constants.js +11 -0
  39. package/lib/providers/kinesis-firehose/utils/getEventBuffer.d.ts +3 -0
  40. package/lib/providers/kinesis-firehose/utils/getEventBuffer.js +100 -0
  41. package/lib/providers/kinesis-firehose/utils/index.d.ts +2 -0
  42. package/lib/providers/kinesis-firehose/utils/index.js +9 -0
  43. package/lib/providers/kinesis-firehose/utils/resolveConfig.d.ts +7 -0
  44. package/lib/providers/kinesis-firehose/utils/resolveConfig.js +24 -0
  45. package/lib/providers/personalize/apis/flushEvents.d.ts +8 -0
  46. package/lib/providers/personalize/apis/flushEvents.js +35 -0
  47. package/lib/providers/personalize/apis/index.d.ts +1 -0
  48. package/lib/providers/personalize/apis/index.js +3 -1
  49. package/lib/providers/personalize/apis/record.d.ts +1 -1
  50. package/lib/providers/personalize/apis/record.js +65 -2
  51. package/lib/providers/personalize/index.d.ts +1 -1
  52. package/lib/providers/personalize/index.js +2 -1
  53. package/lib/providers/personalize/types/buffer.d.ts +16 -0
  54. package/lib/providers/personalize/types/buffer.js +4 -0
  55. package/lib/providers/personalize/types/index.d.ts +2 -1
  56. package/lib/providers/personalize/types/inputs.d.ts +7 -1
  57. package/lib/providers/personalize/utils/autoTrackMedia.d.ts +10 -0
  58. package/lib/providers/personalize/utils/autoTrackMedia.js +182 -0
  59. package/lib/providers/personalize/utils/cachedSession.d.ts +5 -0
  60. package/lib/providers/personalize/utils/cachedSession.js +52 -0
  61. package/lib/providers/personalize/utils/constants.d.ts +7 -0
  62. package/lib/providers/personalize/utils/constants.js +12 -0
  63. package/lib/providers/personalize/utils/getEventBuffer.d.ts +3 -0
  64. package/lib/providers/personalize/utils/getEventBuffer.js +100 -0
  65. package/lib/providers/personalize/utils/index.d.ts +5 -0
  66. package/lib/providers/personalize/utils/index.js +17 -0
  67. package/lib/providers/personalize/utils/resolveConfig.d.ts +7 -0
  68. package/lib/providers/personalize/utils/resolveConfig.js +25 -0
  69. package/lib/providers/pinpoint/apis/flushEvents.d.ts +8 -0
  70. package/lib/providers/pinpoint/apis/flushEvents.js +27 -0
  71. package/lib/providers/pinpoint/apis/identifyUser.d.ts +2 -2
  72. package/lib/providers/pinpoint/apis/identifyUser.js +3 -3
  73. package/lib/providers/pinpoint/apis/index.d.ts +1 -0
  74. package/lib/providers/pinpoint/apis/index.js +3 -1
  75. package/lib/providers/pinpoint/index.d.ts +1 -1
  76. package/lib/providers/pinpoint/index.js +2 -1
  77. package/lib/tsconfig.tsbuildinfo +1 -1
  78. package/lib/types/index.d.ts +1 -0
  79. package/lib/types/kinesis.d.ts +8 -0
  80. package/lib/types/kinesis.js +4 -0
  81. package/lib/utils/eventBuffer/EventBuffer.d.ts +16 -0
  82. package/lib/utils/eventBuffer/EventBuffer.js +91 -0
  83. package/lib/utils/eventBuffer/index.d.ts +2 -0
  84. package/lib/utils/eventBuffer/index.js +7 -0
  85. package/lib/utils/eventBuffer/types.d.ts +8 -0
  86. package/lib/utils/eventBuffer/types.js +4 -0
  87. package/lib/utils/groupBy.d.ts +1 -0
  88. package/lib/utils/groupBy.js +15 -0
  89. package/lib/utils/index.d.ts +3 -0
  90. package/lib/utils/index.js +7 -1
  91. package/lib/utils/resolveCredentials.d.ts +9 -0
  92. package/lib/utils/resolveCredentials.js +21 -0
  93. package/lib-esm/errors/assertValidationError.d.ts +1 -1
  94. package/lib-esm/errors/assertValidationError.js +7 -3
  95. package/lib-esm/errors/validation.d.ts +3 -1
  96. package/lib-esm/errors/validation.js +8 -0
  97. package/lib-esm/index.d.ts +1 -1
  98. package/lib-esm/index.js +1 -1
  99. package/lib-esm/providers/kinesis/apis/flushEvents.d.ts +8 -0
  100. package/lib-esm/providers/kinesis/apis/flushEvents.js +33 -0
  101. package/lib-esm/providers/kinesis/apis/index.d.ts +1 -0
  102. package/lib-esm/providers/kinesis/apis/index.js +1 -0
  103. package/lib-esm/providers/kinesis/apis/record.d.ts +1 -1
  104. package/lib-esm/providers/kinesis/apis/record.js +40 -2
  105. package/lib-esm/providers/kinesis/index.d.ts +1 -1
  106. package/lib-esm/providers/kinesis/index.js +1 -1
  107. package/lib-esm/providers/kinesis/types/buffer.d.ts +15 -0
  108. package/lib-esm/providers/kinesis/types/buffer.js +3 -0
  109. package/lib-esm/providers/kinesis/types/index.d.ts +1 -0
  110. package/lib-esm/providers/kinesis/types/inputs.d.ts +6 -1
  111. package/lib-esm/providers/kinesis/utils/constants.d.ts +6 -0
  112. package/lib-esm/providers/kinesis/utils/constants.js +8 -0
  113. package/lib-esm/providers/kinesis/utils/getEventBuffer.d.ts +3 -0
  114. package/lib-esm/providers/kinesis/utils/getEventBuffer.js +99 -0
  115. package/lib-esm/providers/kinesis/utils/resolveConfig.d.ts +7 -0
  116. package/lib-esm/providers/kinesis/utils/resolveConfig.js +20 -0
  117. package/lib-esm/providers/kinesis-firehose/apis/flushEvents.d.ts +8 -0
  118. package/lib-esm/providers/kinesis-firehose/apis/flushEvents.js +32 -0
  119. package/lib-esm/providers/kinesis-firehose/apis/index.d.ts +1 -0
  120. package/lib-esm/providers/kinesis-firehose/apis/index.js +1 -0
  121. package/lib-esm/providers/kinesis-firehose/apis/record.d.ts +1 -1
  122. package/lib-esm/providers/kinesis-firehose/apis/record.js +37 -2
  123. package/lib-esm/providers/kinesis-firehose/index.d.ts +1 -1
  124. package/lib-esm/providers/kinesis-firehose/index.js +1 -1
  125. package/lib-esm/providers/kinesis-firehose/types/buffer.d.ts +15 -0
  126. package/lib-esm/providers/kinesis-firehose/types/buffer.js +3 -0
  127. package/lib-esm/providers/kinesis-firehose/types/index.d.ts +1 -0
  128. package/lib-esm/providers/kinesis-firehose/types/inputs.d.ts +5 -1
  129. package/lib-esm/providers/kinesis-firehose/utils/constants.d.ts +6 -0
  130. package/lib-esm/providers/kinesis-firehose/utils/constants.js +8 -0
  131. package/lib-esm/providers/kinesis-firehose/utils/getEventBuffer.d.ts +3 -0
  132. package/lib-esm/providers/kinesis-firehose/utils/getEventBuffer.js +96 -0
  133. package/lib-esm/providers/kinesis-firehose/utils/index.d.ts +2 -0
  134. package/lib-esm/providers/kinesis-firehose/utils/index.js +4 -0
  135. package/lib-esm/providers/kinesis-firehose/utils/resolveConfig.d.ts +7 -0
  136. package/lib-esm/providers/kinesis-firehose/utils/resolveConfig.js +20 -0
  137. package/lib-esm/providers/personalize/apis/flushEvents.d.ts +8 -0
  138. package/lib-esm/providers/personalize/apis/flushEvents.js +31 -0
  139. package/lib-esm/providers/personalize/apis/index.d.ts +1 -0
  140. package/lib-esm/providers/personalize/apis/index.js +1 -0
  141. package/lib-esm/providers/personalize/apis/record.d.ts +1 -1
  142. package/lib-esm/providers/personalize/apis/record.js +65 -2
  143. package/lib-esm/providers/personalize/index.d.ts +1 -1
  144. package/lib-esm/providers/personalize/index.js +1 -1
  145. package/lib-esm/providers/personalize/types/buffer.d.ts +16 -0
  146. package/lib-esm/providers/personalize/types/buffer.js +3 -0
  147. package/lib-esm/providers/personalize/types/index.d.ts +2 -1
  148. package/lib-esm/providers/personalize/types/inputs.d.ts +7 -1
  149. package/lib-esm/providers/personalize/utils/autoTrackMedia.d.ts +10 -0
  150. package/lib-esm/providers/personalize/utils/autoTrackMedia.js +178 -0
  151. package/lib-esm/providers/personalize/utils/cachedSession.d.ts +5 -0
  152. package/lib-esm/providers/personalize/utils/cachedSession.js +47 -0
  153. package/lib-esm/providers/personalize/utils/constants.d.ts +7 -0
  154. package/lib-esm/providers/personalize/utils/constants.js +9 -0
  155. package/lib-esm/providers/personalize/utils/getEventBuffer.d.ts +3 -0
  156. package/lib-esm/providers/personalize/utils/getEventBuffer.js +96 -0
  157. package/lib-esm/providers/personalize/utils/index.d.ts +5 -0
  158. package/lib-esm/providers/personalize/utils/index.js +7 -0
  159. package/lib-esm/providers/personalize/utils/resolveConfig.d.ts +7 -0
  160. package/lib-esm/providers/personalize/utils/resolveConfig.js +21 -0
  161. package/lib-esm/providers/pinpoint/apis/flushEvents.d.ts +8 -0
  162. package/lib-esm/providers/pinpoint/apis/flushEvents.js +23 -0
  163. package/lib-esm/providers/pinpoint/apis/identifyUser.d.ts +2 -2
  164. package/lib-esm/providers/pinpoint/apis/identifyUser.js +3 -3
  165. package/lib-esm/providers/pinpoint/apis/index.d.ts +1 -0
  166. package/lib-esm/providers/pinpoint/apis/index.js +1 -0
  167. package/lib-esm/providers/pinpoint/index.d.ts +1 -1
  168. package/lib-esm/providers/pinpoint/index.js +1 -1
  169. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  170. package/lib-esm/types/index.d.ts +1 -0
  171. package/lib-esm/types/kinesis.d.ts +8 -0
  172. package/lib-esm/types/kinesis.js +3 -0
  173. package/lib-esm/utils/eventBuffer/EventBuffer.d.ts +16 -0
  174. package/lib-esm/utils/eventBuffer/EventBuffer.js +88 -0
  175. package/lib-esm/utils/eventBuffer/index.d.ts +2 -0
  176. package/lib-esm/utils/eventBuffer/index.js +3 -0
  177. package/lib-esm/utils/eventBuffer/types.d.ts +8 -0
  178. package/lib-esm/utils/eventBuffer/types.js +3 -0
  179. package/lib-esm/utils/groupBy.d.ts +1 -0
  180. package/lib-esm/utils/groupBy.js +11 -0
  181. package/lib-esm/utils/index.d.ts +3 -0
  182. package/lib-esm/utils/index.js +3 -0
  183. package/lib-esm/utils/resolveCredentials.d.ts +9 -0
  184. package/lib-esm/utils/resolveCredentials.js +17 -0
  185. package/package.json +5 -4
  186. package/src/errors/assertValidationError.ts +9 -3
  187. package/src/errors/validation.ts +8 -0
  188. package/src/index.ts +1 -0
  189. package/src/providers/kinesis/apis/flushEvents.ts +42 -0
  190. package/src/providers/kinesis/apis/index.ts +1 -0
  191. package/src/providers/kinesis/apis/record.ts +54 -2
  192. package/src/providers/kinesis/index.ts +1 -1
  193. package/src/providers/kinesis/types/buffer.ts +20 -0
  194. package/src/providers/kinesis/types/index.ts +1 -0
  195. package/src/providers/kinesis/types/inputs.ts +7 -1
  196. package/src/providers/kinesis/utils/constants.ts +9 -0
  197. package/src/providers/kinesis/utils/getEventBuffer.ts +110 -0
  198. package/src/providers/kinesis/utils/resolveConfig.ts +36 -0
  199. package/src/providers/kinesis-firehose/apis/flushEvents.ts +41 -0
  200. package/src/providers/kinesis-firehose/apis/index.ts +1 -0
  201. package/src/providers/kinesis-firehose/apis/record.ts +47 -2
  202. package/src/providers/kinesis-firehose/index.ts +1 -1
  203. package/src/providers/kinesis-firehose/types/buffer.ts +20 -0
  204. package/src/providers/kinesis-firehose/types/index.ts +5 -0
  205. package/src/providers/kinesis-firehose/types/inputs.ts +6 -1
  206. package/src/providers/kinesis-firehose/utils/constants.ts +9 -0
  207. package/src/providers/kinesis-firehose/utils/getEventBuffer.ts +118 -0
  208. package/src/providers/kinesis-firehose/utils/index.ts +5 -0
  209. package/src/providers/kinesis-firehose/utils/resolveConfig.ts +36 -0
  210. package/src/providers/personalize/apis/flushEvents.ts +39 -0
  211. package/src/providers/personalize/apis/index.ts +1 -0
  212. package/src/providers/personalize/apis/record.ts +98 -2
  213. package/src/providers/personalize/index.ts +1 -1
  214. package/src/providers/personalize/types/buffer.ts +21 -0
  215. package/src/providers/personalize/types/index.ts +2 -1
  216. package/src/providers/personalize/types/inputs.ts +8 -1
  217. package/src/providers/personalize/utils/autoTrackMedia.ts +231 -0
  218. package/src/providers/personalize/utils/cachedSession.ts +67 -0
  219. package/src/providers/personalize/utils/constants.ts +12 -0
  220. package/src/providers/personalize/utils/getEventBuffer.ts +111 -0
  221. package/src/providers/personalize/utils/index.ts +11 -0
  222. package/src/providers/personalize/utils/resolveConfig.ts +41 -0
  223. package/src/providers/pinpoint/apis/flushEvents.ts +34 -0
  224. package/src/providers/pinpoint/apis/identifyUser.ts +3 -3
  225. package/src/providers/pinpoint/apis/index.ts +1 -0
  226. package/src/providers/pinpoint/index.ts +1 -1
  227. package/src/types/index.ts +2 -0
  228. package/src/types/kinesis.ts +13 -0
  229. package/src/utils/eventBuffer/EventBuffer.ts +84 -0
  230. package/src/utils/eventBuffer/index.ts +5 -0
  231. package/src/utils/eventBuffer/types.ts +12 -0
  232. package/src/utils/groupBy.ts +12 -0
  233. package/src/utils/index.ts +7 -0
  234. package/src/utils/resolveCredentials.ts +14 -0
@@ -0,0 +1,178 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { __assign, __awaiter, __generator, __rest } from "tslib";
4
+ import { ConsoleLogger, isBrowser } from '@aws-amplify/core/internals/utils';
5
+ var HTML5_MEDIA_EVENT;
6
+ (function (HTML5_MEDIA_EVENT) {
7
+ HTML5_MEDIA_EVENT["PLAY"] = "play";
8
+ HTML5_MEDIA_EVENT["PAUSE"] = "pause";
9
+ HTML5_MEDIA_EVENT["ENDED"] = "Ended";
10
+ })(HTML5_MEDIA_EVENT || (HTML5_MEDIA_EVENT = {}));
11
+ var MEDIA_TYPE;
12
+ (function (MEDIA_TYPE) {
13
+ MEDIA_TYPE["IFRAME"] = "IFRAME";
14
+ MEDIA_TYPE["VIDEO"] = "VIDEO";
15
+ MEDIA_TYPE["AUDIO"] = "AUDIO";
16
+ })(MEDIA_TYPE || (MEDIA_TYPE = {}));
17
+ var EVENT_TYPE;
18
+ (function (EVENT_TYPE) {
19
+ EVENT_TYPE["PLAY"] = "Play";
20
+ EVENT_TYPE["ENDED"] = "Ended";
21
+ EVENT_TYPE["PAUSE"] = "Pause";
22
+ EVENT_TYPE["TIME_WATCHED"] = "TimeWatched";
23
+ })(EVENT_TYPE || (EVENT_TYPE = {}));
24
+ var logger = new ConsoleLogger('MediaAutoTrack');
25
+ var startIframeAutoTracking = function (element, recordEvent) {
26
+ var isPlaying = false;
27
+ var player;
28
+ var mediaProperties = function () {
29
+ var duration = Number(parseFloat(player.getDuration()).toFixed(4));
30
+ var currentTime = Number(parseFloat(player.getCurrentTime()).toFixed(4));
31
+ return {
32
+ duration: duration,
33
+ eventValue: Number((currentTime / duration).toFixed(4)),
34
+ };
35
+ };
36
+ var scriptElement = document.createElement('script');
37
+ scriptElement.type = 'text/javascript';
38
+ scriptElement.src = 'https://www.youtube.com/iframe_api';
39
+ document.body.append(scriptElement);
40
+ var timer = setInterval(function () {
41
+ if (isPlaying && player) {
42
+ recordEvent(EVENT_TYPE.TIME_WATCHED, mediaProperties());
43
+ }
44
+ }, 3000);
45
+ element.addEventListener('unload', function () { return clearInterval(timer); });
46
+ // @ts-ignore
47
+ window.onYouTubeIframeAPIReady = function () {
48
+ // @ts-ignore
49
+ delete window.onYouTubeIframeAPIReady;
50
+ // @ts-ignore
51
+ player = new window.YT.Player(element.id, {
52
+ events: {
53
+ onStateChange: function (event) {
54
+ var iframeEventMapping = {
55
+ 0: EVENT_TYPE.ENDED,
56
+ 1: EVENT_TYPE.PLAY,
57
+ 2: EVENT_TYPE.PAUSE,
58
+ };
59
+ // @ts-ignore
60
+ var eventType = iframeEventMapping[event.data];
61
+ switch (eventType) {
62
+ case EVENT_TYPE.ENDED:
63
+ case EVENT_TYPE.PAUSE:
64
+ isPlaying = false;
65
+ break;
66
+ case EVENT_TYPE.PLAY:
67
+ isPlaying = true;
68
+ break;
69
+ }
70
+ if (eventType) {
71
+ recordEvent(eventType, mediaProperties());
72
+ }
73
+ },
74
+ },
75
+ });
76
+ };
77
+ };
78
+ var startHTMLMediaAutoTracking = function (element, recordEvent) {
79
+ var isPlaying = false;
80
+ var mediaProperties = function () { return ({
81
+ duration: element.duration,
82
+ eventValue: Number((element.currentTime / element.duration).toFixed(4)),
83
+ }); };
84
+ var timer = setInterval(function () {
85
+ if (isPlaying) {
86
+ recordEvent(EVENT_TYPE.TIME_WATCHED, mediaProperties());
87
+ }
88
+ }, 3000);
89
+ element.addEventListener('unload', function () { return clearInterval(timer); });
90
+ element.addEventListener(HTML5_MEDIA_EVENT.PLAY, function () {
91
+ isPlaying = true;
92
+ recordEvent(EVENT_TYPE.PLAY, mediaProperties());
93
+ });
94
+ element.addEventListener(HTML5_MEDIA_EVENT.PAUSE, function () {
95
+ isPlaying = false;
96
+ recordEvent(EVENT_TYPE.PAUSE, mediaProperties());
97
+ });
98
+ element.addEventListener(HTML5_MEDIA_EVENT.ENDED, function () {
99
+ isPlaying = false;
100
+ recordEvent(EVENT_TYPE.ENDED, mediaProperties());
101
+ });
102
+ };
103
+ var checkElementLoaded = function (interval, maxTries) {
104
+ var retryCount = 0;
105
+ var wait = function () { return new Promise(function (r) { return setTimeout(r, interval); }); };
106
+ var check = function (elementId) { return __awaiter(void 0, void 0, void 0, function () {
107
+ var domElement;
108
+ return __generator(this, function (_a) {
109
+ switch (_a.label) {
110
+ case 0:
111
+ if (retryCount >= maxTries) {
112
+ return [2 /*return*/, false];
113
+ }
114
+ domElement = document.getElementById(elementId);
115
+ if (!(domElement && domElement.clientHeight)) return [3 /*break*/, 1];
116
+ return [2 /*return*/, true];
117
+ case 1:
118
+ retryCount += 1;
119
+ return [4 /*yield*/, wait()];
120
+ case 2:
121
+ _a.sent();
122
+ return [4 /*yield*/, check(elementId)];
123
+ case 3: return [2 /*return*/, _a.sent()];
124
+ }
125
+ });
126
+ }); };
127
+ return check;
128
+ };
129
+ var recordEvent = function (config, eventBuffer) {
130
+ return function (eventType, properties) {
131
+ // override eventType and merge properties
132
+ eventBuffer.append(__assign(__assign({}, config), { event: __assign(__assign({}, config.event), { eventType: eventType, properties: __assign(__assign({}, config.event.properties), properties) }), timestamp: Date.now() }));
133
+ };
134
+ };
135
+ export var autoTrackMedia = function (config, eventBuffer) { return __awaiter(void 0, void 0, void 0, function () {
136
+ var _a, eventType, properties, domElementId, otherProperties, elementId, isElementLoaded, autoTrackConfigWithoutDomElementId, element;
137
+ return __generator(this, function (_b) {
138
+ switch (_b.label) {
139
+ case 0:
140
+ _a = config.event, eventType = _a.eventType, properties = _a.properties;
141
+ domElementId = properties.domElementId, otherProperties = __rest(properties, ["domElementId"]);
142
+ if (!isBrowser()) {
143
+ logger.debug("".concat(eventType, " only for browser"));
144
+ return [2 /*return*/];
145
+ }
146
+ if (typeof domElementId === 'string' && !domElementId) {
147
+ logger.debug("Missing domElementId field in 'properties' for MediaAutoTrack event type.");
148
+ return [2 /*return*/];
149
+ }
150
+ elementId = domElementId;
151
+ return [4 /*yield*/, checkElementLoaded(500, 5)(elementId)];
152
+ case 1:
153
+ isElementLoaded = _b.sent();
154
+ if (isElementLoaded) {
155
+ autoTrackConfigWithoutDomElementId = __assign(__assign({}, config), { event: __assign(__assign({}, config.event), { properties: otherProperties }) });
156
+ element = document.getElementById(elementId);
157
+ switch (element === null || element === void 0 ? void 0 : element.tagName) {
158
+ case MEDIA_TYPE.IFRAME:
159
+ startIframeAutoTracking(element, recordEvent(autoTrackConfigWithoutDomElementId, eventBuffer));
160
+ break;
161
+ case MEDIA_TYPE.VIDEO:
162
+ case MEDIA_TYPE.AUDIO:
163
+ if (element instanceof HTMLMediaElement) {
164
+ startHTMLMediaAutoTracking(element, recordEvent(autoTrackConfigWithoutDomElementId, eventBuffer));
165
+ }
166
+ break;
167
+ default:
168
+ logger.debug("Unsupported DOM element tag: ".concat(element === null || element === void 0 ? void 0 : element.tagName));
169
+ break;
170
+ }
171
+ }
172
+ else {
173
+ logger.debug('Cannot find the media element.');
174
+ }
175
+ return [2 /*return*/];
176
+ }
177
+ });
178
+ }); };
@@ -0,0 +1,5 @@
1
+ export declare const resolveCachedSession: (trackingId: string) => {
2
+ sessionId: string;
3
+ userId: string | undefined;
4
+ };
5
+ export declare const updateCachedSession: (newUserId?: string, currentSessionId?: string, currentUserId?: string) => void;
@@ -0,0 +1,47 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { Cache } from '@aws-amplify/core';
4
+ import { isBrowser } from '@aws-amplify/core/internals/utils';
5
+ import { v4 as uuid } from 'uuid';
6
+ var PERSONALIZE_CACHE_USERID = '_awsct_uid';
7
+ var PERSONALIZE_CACHE_SESSIONID = '_awsct_sid';
8
+ var DEFAULT_CACHE_PREFIX = 'personalize';
9
+ var DELIMITER = '.';
10
+ var CACHE_EXPIRY_IN_DAYS = 7;
11
+ var normalize = function (key) {
12
+ return [key, isBrowser() ? window.location.host : DEFAULT_CACHE_PREFIX].join(DELIMITER);
13
+ };
14
+ var getCache = function (key) { return Cache.getItem(normalize(key)); };
15
+ var setCache = function (key, value) {
16
+ var expiredAt = new Date(Date.now() + 3600000 * 24 * CACHE_EXPIRY_IN_DAYS);
17
+ Cache.setItem(normalize(key), value, {
18
+ expires: expiredAt.getTime(),
19
+ });
20
+ };
21
+ export var resolveCachedSession = function (trackingId) {
22
+ var sessionId = getCache(PERSONALIZE_CACHE_SESSIONID);
23
+ if (!sessionId) {
24
+ sessionId = uuid();
25
+ setCache(PERSONALIZE_CACHE_SESSIONID, sessionId);
26
+ }
27
+ var userId = getCache(PERSONALIZE_CACHE_USERID);
28
+ return {
29
+ sessionId: sessionId,
30
+ userId: userId,
31
+ };
32
+ };
33
+ export var updateCachedSession = function (newUserId, currentSessionId, currentUserId) {
34
+ var isNoCachedSession = !currentSessionId;
35
+ var isSignOutCase = !newUserId && !currentUserId;
36
+ var isSwitchUserCase = !!newUserId && !!currentUserId && newUserId !== currentUserId;
37
+ var isRequireNewSession = isNoCachedSession || isSignOutCase || isSwitchUserCase;
38
+ var isRequireUpdateSession = !!currentSessionId && !currentUserId && !!newUserId;
39
+ if (isRequireNewSession) {
40
+ var newSessionId = uuid();
41
+ setCache(PERSONALIZE_CACHE_SESSIONID, newSessionId);
42
+ setCache(PERSONALIZE_CACHE_USERID, newUserId);
43
+ }
44
+ else if (isRequireUpdateSession) {
45
+ setCache(PERSONALIZE_CACHE_USERID, newUserId);
46
+ }
47
+ };
@@ -0,0 +1,7 @@
1
+ export declare const DEFAULT_PERSONALIZE_CONFIG: {
2
+ flushSize: number;
3
+ flushInterval: number;
4
+ };
5
+ export declare const PERSONALIZE_FLUSH_SIZE_MAX = 10;
6
+ export declare const IDENTIFY_EVENT_TYPE = "Identify";
7
+ export declare const MEDIA_AUTO_TRACK_EVENT_TYPE = "MediaAutoTrack";
@@ -0,0 +1,9 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export var DEFAULT_PERSONALIZE_CONFIG = {
4
+ flushSize: 5,
5
+ flushInterval: 5000,
6
+ };
7
+ export var PERSONALIZE_FLUSH_SIZE_MAX = 10;
8
+ export var IDENTIFY_EVENT_TYPE = 'Identify';
9
+ export var MEDIA_AUTO_TRACK_EVENT_TYPE = 'MediaAutoTrack';
@@ -0,0 +1,3 @@
1
+ import { EventBuffer } from '../../../utils';
2
+ import { PersonalizeBufferConfig, PersonalizeBufferEvent } from '../types';
3
+ export declare const getEventBuffer: ({ region, flushSize, flushInterval, bufferSize, credentials, identityId, userAgentValue, }: PersonalizeBufferConfig) => EventBuffer<PersonalizeBufferEvent>;
@@ -0,0 +1,96 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { __awaiter, __generator, __read, __values } from "tslib";
4
+ import { EventBuffer, groupBy } from '../../../utils';
5
+ import { PersonalizeEventsClient, PutEventsCommand, } from '@aws-sdk/client-personalize-events';
6
+ /**
7
+ * These Records hold cached event buffers and AWS clients.
8
+ * The hash key is determined by the region and session,
9
+ * consisting of a combined value comprising [region, sessionToken, identityId].
10
+ *
11
+ * Only one active session should exist at any given moment.
12
+ * When a new session is initiated, the previous ones should be released.
13
+ * */
14
+ var eventBufferMap = {};
15
+ var cachedClients = {};
16
+ var DELIMITER = '#';
17
+ var createPutEventsCommand = function (ids, events) {
18
+ var _a = __read(ids.split(DELIMITER), 3), trackingId = _a[0], sessionId = _a[1], userId = _a[2];
19
+ return new PutEventsCommand({
20
+ trackingId: trackingId,
21
+ sessionId: sessionId,
22
+ userId: userId,
23
+ eventList: events.map(function (event) { return ({
24
+ eventId: event.event.eventId,
25
+ eventType: event.event.eventType,
26
+ properties: JSON.stringify(event.event.properties),
27
+ sentAt: new Date(event.timestamp),
28
+ }); }),
29
+ });
30
+ };
31
+ var submitEvents = function (events, client) { return __awaiter(void 0, void 0, void 0, function () {
32
+ var groupedByIds, requests;
33
+ return __generator(this, function (_a) {
34
+ switch (_a.label) {
35
+ case 0:
36
+ groupedByIds = Object.entries(groupBy(function (event) {
37
+ return [event.trackingId, event.sessionId, event.userId]
38
+ .filter(function (id) { return !!id; })
39
+ .join(DELIMITER);
40
+ }, events));
41
+ requests = groupedByIds
42
+ .map(function (_a) {
43
+ var _b = __read(_a, 2), ids = _b[0], events = _b[1];
44
+ return createPutEventsCommand(ids, events);
45
+ })
46
+ .map(function (command) { return client.send(command); });
47
+ return [4 /*yield*/, Promise.allSettled(requests)];
48
+ case 1:
49
+ _a.sent();
50
+ return [2 /*return*/, Promise.resolve([])];
51
+ }
52
+ });
53
+ }); };
54
+ export var getEventBuffer = function (_a) {
55
+ var e_1, _b;
56
+ var region = _a.region, flushSize = _a.flushSize, flushInterval = _a.flushInterval, bufferSize = _a.bufferSize, credentials = _a.credentials, identityId = _a.identityId, userAgentValue = _a.userAgentValue;
57
+ var sessionToken = credentials.sessionToken;
58
+ var sessionIdentityKey = [region, sessionToken, identityId]
59
+ .filter(function (x) { return !!x; })
60
+ .join('-');
61
+ if (!eventBufferMap[sessionIdentityKey]) {
62
+ var getClient = function () {
63
+ if (!cachedClients[sessionIdentityKey]) {
64
+ cachedClients[sessionIdentityKey] = new PersonalizeEventsClient({
65
+ region: region,
66
+ credentials: credentials,
67
+ customUserAgent: userAgentValue,
68
+ });
69
+ }
70
+ return function (events) { return submitEvents(events, cachedClients[sessionIdentityKey]); };
71
+ };
72
+ eventBufferMap[sessionIdentityKey] =
73
+ new EventBuffer({
74
+ bufferSize: bufferSize,
75
+ flushSize: flushSize,
76
+ flushInterval: flushInterval,
77
+ }, getClient);
78
+ var releaseSessionKeys = Object.keys(eventBufferMap).filter(function (key) { return key !== sessionIdentityKey; });
79
+ try {
80
+ for (var releaseSessionKeys_1 = __values(releaseSessionKeys), releaseSessionKeys_1_1 = releaseSessionKeys_1.next(); !releaseSessionKeys_1_1.done; releaseSessionKeys_1_1 = releaseSessionKeys_1.next()) {
81
+ var releaseSessionKey = releaseSessionKeys_1_1.value;
82
+ eventBufferMap[releaseSessionKey].release();
83
+ delete eventBufferMap[releaseSessionKey];
84
+ delete cachedClients[releaseSessionKey];
85
+ }
86
+ }
87
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
88
+ finally {
89
+ try {
90
+ if (releaseSessionKeys_1_1 && !releaseSessionKeys_1_1.done && (_b = releaseSessionKeys_1.return)) _b.call(releaseSessionKeys_1);
91
+ }
92
+ finally { if (e_1) throw e_1.error; }
93
+ }
94
+ }
95
+ return eventBufferMap[sessionIdentityKey];
96
+ };
@@ -0,0 +1,5 @@
1
+ export { getEventBuffer } from './getEventBuffer';
2
+ export { resolveConfig } from './resolveConfig';
3
+ export { autoTrackMedia } from './autoTrackMedia';
4
+ export { resolveCachedSession, updateCachedSession } from './cachedSession';
5
+ export { DEFAULT_PERSONALIZE_CONFIG, PERSONALIZE_FLUSH_SIZE_MAX, } from './constants';
@@ -0,0 +1,7 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { getEventBuffer } from './getEventBuffer';
4
+ export { resolveConfig } from './resolveConfig';
5
+ export { autoTrackMedia } from './autoTrackMedia';
6
+ export { resolveCachedSession, updateCachedSession } from './cachedSession';
7
+ export { DEFAULT_PERSONALIZE_CONFIG, PERSONALIZE_FLUSH_SIZE_MAX, } from './constants';
@@ -0,0 +1,7 @@
1
+ export declare const resolveConfig: () => {
2
+ region: string;
3
+ trackingId: string;
4
+ bufferSize: number;
5
+ flushSize: number;
6
+ flushInterval: number;
7
+ };
@@ -0,0 +1,21 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { __assign } from "tslib";
4
+ import { Amplify } from '@aws-amplify/core';
5
+ import { AnalyticsValidationErrorCode, assertValidationError, } from '../../../errors';
6
+ import { DEFAULT_PERSONALIZE_CONFIG, PERSONALIZE_FLUSH_SIZE_MAX } from './';
7
+ export var resolveConfig = function () {
8
+ var _a;
9
+ var config = (_a = Amplify.getConfig().Analytics) === null || _a === void 0 ? void 0 : _a.Personalize;
10
+ var _b = __assign(__assign({}, DEFAULT_PERSONALIZE_CONFIG), config), region = _b.region, trackingId = _b.trackingId, _c = _b.flushSize, flushSize = _c === void 0 ? DEFAULT_PERSONALIZE_CONFIG.flushSize : _c, _d = _b.flushInterval, flushInterval = _d === void 0 ? DEFAULT_PERSONALIZE_CONFIG.flushInterval : _d;
11
+ assertValidationError(!!region, AnalyticsValidationErrorCode.NoRegion);
12
+ assertValidationError(!!trackingId, AnalyticsValidationErrorCode.NoTrackingId);
13
+ assertValidationError(flushSize <= PERSONALIZE_FLUSH_SIZE_MAX, AnalyticsValidationErrorCode.InvalidFlushSize, "FlushSize for Personalize should be less or equal than ".concat(PERSONALIZE_FLUSH_SIZE_MAX));
14
+ return {
15
+ region: region,
16
+ trackingId: trackingId,
17
+ bufferSize: flushSize + 1,
18
+ flushSize: flushSize,
19
+ flushInterval: flushInterval,
20
+ };
21
+ };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Flushes all buffered Pinpoint events to the service.
3
+ *
4
+ * @note
5
+ * This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
6
+ * this API may not be included in the flush.
7
+ */
8
+ export declare const flushEvents: () => void;
@@ -0,0 +1,23 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { resolveConfig, resolveCredentials } from '../utils';
4
+ import { flushEvents as flushEventsCore } from '@aws-amplify/core/internals/providers/pinpoint';
5
+ import { AnalyticsAction, ConsoleLogger, } from '@aws-amplify/core/internals/utils';
6
+ import { getAnalyticsUserAgentString } from '../../../utils';
7
+ var logger = new ConsoleLogger('Analytics');
8
+ /**
9
+ * Flushes all buffered Pinpoint events to the service.
10
+ *
11
+ * @note
12
+ * This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
13
+ * this API may not be included in the flush.
14
+ */
15
+ export var flushEvents = function () {
16
+ var _a = resolveConfig(), appId = _a.appId, region = _a.region;
17
+ resolveCredentials()
18
+ .then(function (_a) {
19
+ var credentials = _a.credentials, identityId = _a.identityId;
20
+ return flushEventsCore(appId, region, credentials, identityId, getAnalyticsUserAgentString(AnalyticsAction.Record));
21
+ })
22
+ .catch(function (e) { return logger.warn('Failed to flush events', e); });
23
+ };
@@ -19,7 +19,7 @@ import { IdentifyUserInput } from '../types';
19
19
  * await identifyUser({
20
20
  * userId,
21
21
  * userProfile: {
22
- * email: [userEmail]
22
+ * email: 'userEmail@example.com'
23
23
  * customProperties: {
24
24
  * phoneNumber: ['555-555-5555'],
25
25
  * },
@@ -33,7 +33,7 @@ import { IdentifyUserInput } from '../types';
33
33
  * await identifyUser({
34
34
  * userId,
35
35
  * userProfile: {
36
- * email: [userEmail]
36
+ * email: 'userEmail@example.com'
37
37
  * customProperties: {
38
38
  * phoneNumber: ['555-555-5555'],
39
39
  * },
@@ -25,7 +25,7 @@ import { resolveConfig, resolveCredentials } from '../utils';
25
25
  * await identifyUser({
26
26
  * userId,
27
27
  * userProfile: {
28
- * email: [userEmail]
28
+ * email: 'userEmail@example.com'
29
29
  * customProperties: {
30
30
  * phoneNumber: ['555-555-5555'],
31
31
  * },
@@ -39,7 +39,7 @@ import { resolveConfig, resolveCredentials } from '../utils';
39
39
  * await identifyUser({
40
40
  * userId,
41
41
  * userProfile: {
42
- * email: [userEmail]
42
+ * email: 'userEmail@example.com'
43
43
  * customProperties: {
44
44
  * phoneNumber: ['555-555-5555'],
45
45
  * },
@@ -71,7 +71,7 @@ export var identifyUser = function (_a) {
71
71
  userAttributes: userAttributes,
72
72
  userId: userId,
73
73
  userProfile: userProfile,
74
- userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.UpdateEndpoint),
74
+ userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.IdentifyUser),
75
75
  });
76
76
  return [2 /*return*/];
77
77
  }
@@ -1,2 +1,3 @@
1
1
  export { record } from './record';
2
2
  export { identifyUser } from './identifyUser';
3
+ export { flushEvents } from './flushEvents';
@@ -2,3 +2,4 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  export { record } from './record';
4
4
  export { identifyUser } from './identifyUser';
5
+ export { flushEvents } from './flushEvents';
@@ -1,2 +1,2 @@
1
- export { record, identifyUser } from './apis';
1
+ export { record, identifyUser, flushEvents } from './apis';
2
2
  export { RecordInput, IdentifyUserInput } 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 } from './apis';
3
+ export { record, identifyUser, flushEvents } from './apis';