@aws-amplify/notifications 2.0.1-console-preview.5f040a2.0 → 2.0.1-console-preview.23dc225.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 (190) hide show
  1. package/lib/common/AWSPinpointProviderCommon/index.d.ts +0 -1
  2. package/lib/common/AWSPinpointProviderCommon/index.js +14 -19
  3. package/lib/common/AWSPinpointProviderCommon/index.js.map +1 -1
  4. package/lib/common/eventListeners/index.d.ts +2 -2
  5. package/lib/common/eventListeners/index.js +3 -1
  6. package/lib/common/eventListeners/index.js.map +1 -1
  7. package/lib/common/eventListeners/types.d.ts +3 -0
  8. package/lib/common/index.d.ts +1 -1
  9. package/lib/inAppMessaging/errors/validation.d.ts +2 -1
  10. package/lib/inAppMessaging/errors/validation.js +5 -0
  11. package/lib/inAppMessaging/errors/validation.js.map +1 -1
  12. package/lib/inAppMessaging/index.d.ts +1 -1
  13. package/lib/inAppMessaging/index.js +8 -0
  14. package/lib/inAppMessaging/index.js.map +1 -1
  15. package/lib/inAppMessaging/providers/index.d.ts +1 -1
  16. package/lib/inAppMessaging/providers/index.js +8 -0
  17. package/lib/inAppMessaging/providers/index.js.map +1 -1
  18. package/lib/inAppMessaging/providers/pinpoint/apis/dispatchEvent.d.ts +26 -0
  19. package/lib/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js +69 -0
  20. package/lib/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js.map +1 -0
  21. package/lib/inAppMessaging/providers/pinpoint/apis/identifyUser.d.ts +1 -4
  22. package/lib/inAppMessaging/providers/pinpoint/apis/identifyUser.js +5 -5
  23. package/lib/inAppMessaging/providers/pinpoint/apis/identifyUser.js.map +1 -1
  24. package/lib/inAppMessaging/providers/pinpoint/apis/index.d.ts +8 -0
  25. package/lib/inAppMessaging/providers/pinpoint/apis/index.js +16 -0
  26. package/lib/inAppMessaging/providers/pinpoint/apis/index.js.map +1 -1
  27. package/lib/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.d.ts +12 -0
  28. package/lib/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js +61 -0
  29. package/lib/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js.map +1 -0
  30. package/lib/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.d.ts +16 -0
  31. package/lib/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js +27 -0
  32. package/lib/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js.map +1 -0
  33. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.d.ts +18 -0
  34. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js +27 -0
  35. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js.map +1 -0
  36. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.d.ts +18 -0
  37. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js +27 -0
  38. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js.map +1 -0
  39. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.d.ts +18 -0
  40. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js +27 -0
  41. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js.map +1 -0
  42. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageReceived.d.ts +18 -0
  43. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js +27 -0
  44. package/lib/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js.map +1 -0
  45. package/lib/inAppMessaging/providers/pinpoint/apis/setConflictHandler.d.ts +32 -0
  46. package/lib/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js +66 -0
  47. package/lib/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js.map +1 -0
  48. package/lib/inAppMessaging/providers/pinpoint/apis/syncMessages.d.ts +1 -3
  49. package/lib/inAppMessaging/providers/pinpoint/apis/syncMessages.js +20 -18
  50. package/lib/inAppMessaging/providers/pinpoint/apis/syncMessages.js.map +1 -1
  51. package/lib/inAppMessaging/providers/pinpoint/index.d.ts +1 -1
  52. package/lib/inAppMessaging/providers/pinpoint/index.js +8 -0
  53. package/lib/inAppMessaging/providers/pinpoint/index.js.map +1 -1
  54. package/lib/inAppMessaging/providers/pinpoint/types/index.d.ts +2 -1
  55. package/lib/inAppMessaging/providers/pinpoint/types/index.js +2 -0
  56. package/lib/inAppMessaging/providers/pinpoint/types/index.js.map +1 -1
  57. package/lib/inAppMessaging/providers/pinpoint/types/inputs.d.ts +33 -2
  58. package/lib/inAppMessaging/providers/pinpoint/types/outputs.d.ts +17 -0
  59. package/lib/inAppMessaging/providers/pinpoint/types/outputs.js +5 -0
  60. package/lib/inAppMessaging/providers/pinpoint/types/outputs.js.map +1 -0
  61. package/lib/inAppMessaging/providers/pinpoint/types/types.d.ts +4 -1
  62. package/lib/inAppMessaging/providers/pinpoint/types/types.js +6 -6
  63. package/lib/inAppMessaging/providers/pinpoint/types/types.js.map +1 -1
  64. package/lib/inAppMessaging/providers/pinpoint/utils/helpers.d.ts +17 -0
  65. package/lib/inAppMessaging/providers/pinpoint/utils/helpers.js +260 -0
  66. package/lib/inAppMessaging/providers/pinpoint/utils/helpers.js.map +1 -0
  67. package/lib/inAppMessaging/providers/pinpoint/utils/index.d.ts +1 -0
  68. package/lib/inAppMessaging/providers/pinpoint/utils/index.js +4 -0
  69. package/lib/inAppMessaging/providers/pinpoint/utils/index.js.map +1 -1
  70. package/lib/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.d.ts +6 -0
  71. package/lib/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js +247 -0
  72. package/lib/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js.map +1 -0
  73. package/lib/inAppMessaging/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
  74. package/lib/inAppMessaging/types/index.d.ts +1 -1
  75. package/lib/inAppMessaging/utils/index.d.ts +1 -0
  76. package/lib/inAppMessaging/utils/index.js +9 -0
  77. package/lib/inAppMessaging/utils/index.js.map +1 -0
  78. package/lib/inAppMessaging/utils/statusHelpers.d.ts +13 -0
  79. package/lib/inAppMessaging/utils/statusHelpers.js +25 -0
  80. package/lib/inAppMessaging/utils/statusHelpers.js.map +1 -0
  81. package/lib-esm/common/AWSPinpointProviderCommon/index.d.ts +0 -1
  82. package/lib-esm/common/AWSPinpointProviderCommon/index.js +15 -20
  83. package/lib-esm/common/AWSPinpointProviderCommon/index.js.map +1 -1
  84. package/lib-esm/common/eventListeners/index.d.ts +2 -2
  85. package/lib-esm/common/eventListeners/index.js +3 -1
  86. package/lib-esm/common/eventListeners/index.js.map +1 -1
  87. package/lib-esm/common/eventListeners/types.d.ts +3 -0
  88. package/lib-esm/common/index.d.ts +1 -1
  89. package/lib-esm/inAppMessaging/errors/validation.d.ts +2 -1
  90. package/lib-esm/inAppMessaging/errors/validation.js +5 -0
  91. package/lib-esm/inAppMessaging/errors/validation.js.map +1 -1
  92. package/lib-esm/inAppMessaging/index.d.ts +1 -1
  93. package/lib-esm/inAppMessaging/index.js +1 -1
  94. package/lib-esm/inAppMessaging/index.js.map +1 -1
  95. package/lib-esm/inAppMessaging/providers/index.d.ts +1 -1
  96. package/lib-esm/inAppMessaging/providers/index.js +1 -1
  97. package/lib-esm/inAppMessaging/providers/index.js.map +1 -1
  98. package/lib-esm/inAppMessaging/providers/pinpoint/apis/dispatchEvent.d.ts +26 -0
  99. package/lib-esm/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js +66 -0
  100. package/lib-esm/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js.map +1 -0
  101. package/lib-esm/inAppMessaging/providers/pinpoint/apis/identifyUser.d.ts +1 -4
  102. package/lib-esm/inAppMessaging/providers/pinpoint/apis/identifyUser.js +5 -5
  103. package/lib-esm/inAppMessaging/providers/pinpoint/apis/identifyUser.js.map +1 -1
  104. package/lib-esm/inAppMessaging/providers/pinpoint/apis/index.d.ts +8 -0
  105. package/lib-esm/inAppMessaging/providers/pinpoint/apis/index.js +8 -0
  106. package/lib-esm/inAppMessaging/providers/pinpoint/apis/index.js.map +1 -1
  107. package/lib-esm/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.d.ts +12 -0
  108. package/lib-esm/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js +57 -0
  109. package/lib-esm/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js.map +1 -0
  110. package/lib-esm/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.d.ts +16 -0
  111. package/lib-esm/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js +24 -0
  112. package/lib-esm/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js.map +1 -0
  113. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.d.ts +18 -0
  114. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js +24 -0
  115. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js.map +1 -0
  116. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.d.ts +18 -0
  117. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js +24 -0
  118. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js.map +1 -0
  119. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.d.ts +18 -0
  120. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js +24 -0
  121. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js.map +1 -0
  122. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageReceived.d.ts +18 -0
  123. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js +24 -0
  124. package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js.map +1 -0
  125. package/lib-esm/inAppMessaging/providers/pinpoint/apis/setConflictHandler.d.ts +32 -0
  126. package/lib-esm/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js +63 -0
  127. package/lib-esm/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js.map +1 -0
  128. package/lib-esm/inAppMessaging/providers/pinpoint/apis/syncMessages.d.ts +1 -3
  129. package/lib-esm/inAppMessaging/providers/pinpoint/apis/syncMessages.js +20 -18
  130. package/lib-esm/inAppMessaging/providers/pinpoint/apis/syncMessages.js.map +1 -1
  131. package/lib-esm/inAppMessaging/providers/pinpoint/index.d.ts +1 -1
  132. package/lib-esm/inAppMessaging/providers/pinpoint/index.js +1 -1
  133. package/lib-esm/inAppMessaging/providers/pinpoint/index.js.map +1 -1
  134. package/lib-esm/inAppMessaging/providers/pinpoint/types/index.d.ts +2 -1
  135. package/lib-esm/inAppMessaging/providers/pinpoint/types/index.js +1 -0
  136. package/lib-esm/inAppMessaging/providers/pinpoint/types/index.js.map +1 -1
  137. package/lib-esm/inAppMessaging/providers/pinpoint/types/inputs.d.ts +33 -2
  138. package/lib-esm/inAppMessaging/providers/pinpoint/types/outputs.d.ts +17 -0
  139. package/lib-esm/inAppMessaging/providers/pinpoint/types/outputs.js +3 -0
  140. package/lib-esm/inAppMessaging/providers/pinpoint/types/outputs.js.map +1 -0
  141. package/lib-esm/inAppMessaging/providers/pinpoint/types/types.d.ts +4 -1
  142. package/lib-esm/inAppMessaging/providers/pinpoint/types/types.js +6 -6
  143. package/lib-esm/inAppMessaging/providers/pinpoint/types/types.js.map +1 -1
  144. package/lib-esm/inAppMessaging/providers/pinpoint/utils/helpers.d.ts +17 -0
  145. package/lib-esm/inAppMessaging/providers/pinpoint/utils/helpers.js +258 -0
  146. package/lib-esm/inAppMessaging/providers/pinpoint/utils/helpers.js.map +1 -0
  147. package/lib-esm/inAppMessaging/providers/pinpoint/utils/index.d.ts +1 -0
  148. package/lib-esm/inAppMessaging/providers/pinpoint/utils/index.js +1 -0
  149. package/lib-esm/inAppMessaging/providers/pinpoint/utils/index.js.map +1 -1
  150. package/lib-esm/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.d.ts +6 -0
  151. package/lib-esm/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js +242 -0
  152. package/lib-esm/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js.map +1 -0
  153. package/lib-esm/inAppMessaging/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
  154. package/lib-esm/inAppMessaging/types/index.d.ts +1 -1
  155. package/lib-esm/inAppMessaging/utils/index.d.ts +1 -0
  156. package/lib-esm/inAppMessaging/utils/index.js +4 -0
  157. package/lib-esm/inAppMessaging/utils/index.js.map +1 -0
  158. package/lib-esm/inAppMessaging/utils/statusHelpers.d.ts +13 -0
  159. package/lib-esm/inAppMessaging/utils/statusHelpers.js +22 -0
  160. package/lib-esm/inAppMessaging/utils/statusHelpers.js.map +1 -0
  161. package/package.json +5 -6
  162. package/src/common/AWSPinpointProviderCommon/index.ts +9 -14
  163. package/src/common/eventListeners/index.ts +5 -3
  164. package/src/common/eventListeners/types.ts +4 -0
  165. package/src/common/index.ts +5 -1
  166. package/src/inAppMessaging/errors/validation.ts +6 -0
  167. package/src/inAppMessaging/index.ts +12 -1
  168. package/src/inAppMessaging/providers/index.ts +12 -1
  169. package/src/inAppMessaging/providers/pinpoint/apis/dispatchEvent.ts +63 -0
  170. package/src/inAppMessaging/providers/pinpoint/apis/identifyUser.ts +3 -4
  171. package/src/inAppMessaging/providers/pinpoint/apis/index.ts +8 -0
  172. package/src/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.ts +64 -0
  173. package/src/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.ts +28 -0
  174. package/src/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.ts +29 -0
  175. package/src/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.ts +29 -0
  176. package/src/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.ts +29 -0
  177. package/src/inAppMessaging/providers/pinpoint/apis/onMessageReceived.ts +29 -0
  178. package/src/inAppMessaging/providers/pinpoint/apis/setConflictHandler.ts +68 -0
  179. package/src/inAppMessaging/providers/pinpoint/apis/syncMessages.ts +6 -5
  180. package/src/inAppMessaging/providers/pinpoint/index.ts +12 -1
  181. package/src/inAppMessaging/providers/pinpoint/types/index.ts +14 -1
  182. package/src/inAppMessaging/providers/pinpoint/types/inputs.ts +49 -2
  183. package/src/inAppMessaging/providers/pinpoint/types/outputs.ts +24 -0
  184. package/src/inAppMessaging/providers/pinpoint/types/types.ts +9 -1
  185. package/src/inAppMessaging/providers/pinpoint/utils/helpers.ts +325 -0
  186. package/src/inAppMessaging/providers/pinpoint/utils/index.ts +6 -0
  187. package/src/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.ts +196 -0
  188. package/src/inAppMessaging/types/index.ts +7 -1
  189. package/src/inAppMessaging/utils/index.ts +8 -0
  190. package/src/inAppMessaging/utils/statusHelpers.ts +32 -0
@@ -2,5 +2,18 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  export { UpdateEndpointException } from './errors';
5
- export { IdentifyUserInput } from './inputs';
5
+ export {
6
+ IdentifyUserInput,
7
+ DispatchEventInput,
8
+ SetConflictHandlerInput,
9
+ } from './inputs';
6
10
  export { IdentifyUserOptions } from './options';
11
+ export {
12
+ PinpointMessageEvent,
13
+ MetricsComparator,
14
+ InAppMessageCounts,
15
+ InAppMessageCountMap,
16
+ DailyInAppMessageCounter,
17
+ InAppMessageConflictHandler,
18
+ OnMessageInteractionEventHandler,
19
+ } from './types';
@@ -1,11 +1,58 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { IdentifyUserOptions } from '.';
5
- import { InAppMessagingIdentifyUserInput } from '../../../types';
4
+ import {
5
+ IdentifyUserOptions,
6
+ InAppMessageConflictHandler,
7
+ OnMessageInteractionEventHandler,
8
+ } from '.';
9
+ import {
10
+ InAppMessage,
11
+ InAppMessageInteractionEvent,
12
+ InAppMessagingEvent,
13
+ InAppMessagingIdentifyUserInput,
14
+ } from '../../../types';
6
15
 
7
16
  /**
8
17
  * Input type for Pinpoint identifyUser API.
9
18
  */
10
19
  export type IdentifyUserInput =
11
20
  InAppMessagingIdentifyUserInput<IdentifyUserOptions>;
21
+
22
+ /**
23
+ * Input type for Pinpoint dispatchEvent API.
24
+ */
25
+ export type DispatchEventInput = InAppMessagingEvent;
26
+
27
+ /**
28
+ * Input type for Pinpoint SetConflictHandler API.
29
+ */
30
+ export type SetConflictHandlerInput = InAppMessageConflictHandler;
31
+
32
+ /**
33
+ * Input type for OnMessageReceived API.
34
+ */
35
+ export type OnMessageReceivedInput = OnMessageInteractionEventHandler;
36
+
37
+ /**
38
+ * Input type for OnMessageDisplayed API.
39
+ */
40
+ export type OnMessageDisplayedInput = OnMessageInteractionEventHandler;
41
+
42
+ /**
43
+ * Input type for OnMessageDismissed API.
44
+ */
45
+ export type OnMessageDismissedInput = OnMessageInteractionEventHandler;
46
+
47
+ /**
48
+ * Input type for OnMessageActionTaken API.
49
+ */
50
+ export type OnMessageActionTakenInput = OnMessageInteractionEventHandler;
51
+
52
+ /**
53
+ * Input type for NotifyMessageInteraction API.
54
+ */
55
+ export type NotifyMessageInteractionInput = {
56
+ message: InAppMessage;
57
+ type: InAppMessageInteractionEvent;
58
+ };
@@ -0,0 +1,24 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { EventListenerRemover } from '../../../../common';
5
+
6
+ /**
7
+ * Output type for OnMessageReceived API.
8
+ */
9
+ export type OnMessageReceivedOutput = EventListenerRemover;
10
+
11
+ /**
12
+ * Output type for OnMessageDisplayed API.
13
+ */
14
+ export type OnMessageDisplayedOutput = EventListenerRemover;
15
+
16
+ /**
17
+ * Output type for OnMessageDismissed API.
18
+ */
19
+ export type OnMessageDismissedOutput = EventListenerRemover;
20
+
21
+ /**
22
+ * Output type for OnMessageActionTaken API.
23
+ */
24
+ export type OnMessageActionTakenOutput = EventListenerRemover;
@@ -1,6 +1,8 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import { InAppMessage } from '../../../types';
5
+
4
6
  export type InAppMessageCountMap = Record<string, number>;
5
7
 
6
8
  export type DailyInAppMessageCounter = {
@@ -19,8 +21,14 @@ export type MetricsComparator = (
19
21
  eventVal: number
20
22
  ) => boolean;
21
23
 
22
- export enum AWSPinpointMessageEvent {
24
+ export enum PinpointMessageEvent {
23
25
  MESSAGE_DISPLAYED = '_inapp.message_displayed',
24
26
  MESSAGE_DISMISSED = '_inapp.message_dismissed',
25
27
  MESSAGE_ACTION_TAKEN = '_inapp.message_clicked',
26
28
  }
29
+
30
+ export type InAppMessageConflictHandler = (
31
+ messages: InAppMessage[]
32
+ ) => InAppMessage;
33
+
34
+ export type OnMessageInteractionEventHandler = (message: InAppMessage) => void;
@@ -0,0 +1,325 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ ConsoleLogger,
6
+ InAppMessagingAction,
7
+ } from '@aws-amplify/core/internals/utils';
8
+ import type { InAppMessageCampaign as PinpointInAppMessage } from '@aws-amplify/core/internals/aws-clients/pinpoint';
9
+ import isEmpty from 'lodash/isEmpty';
10
+ import {
11
+ InAppMessage,
12
+ InAppMessageAction,
13
+ InAppMessageContent,
14
+ InAppMessageLayout,
15
+ InAppMessageTextAlign,
16
+ InAppMessagingEvent,
17
+ } from '../../../types';
18
+ import { MetricsComparator, PinpointMessageEvent } from '../types';
19
+ import { record as recordCore } from '@aws-amplify/core/internals/providers/pinpoint';
20
+ import { resolveConfig } from './resolveConfig';
21
+ import { resolveCredentials } from './resolveCredentials';
22
+ import { CATEGORY } from './constants';
23
+ import { getInAppMessagingUserAgentString } from './userAgent';
24
+
25
+ const DELIVERY_TYPE = 'IN_APP_MESSAGE';
26
+
27
+ let eventNameMemo = {};
28
+ let eventAttributesMemo = {};
29
+ let eventMetricsMemo = {};
30
+
31
+ export const logger = new ConsoleLogger('InAppMessaging.Pinpoint.Utils');
32
+
33
+ export const recordAnalyticsEvent = (
34
+ event: PinpointMessageEvent,
35
+ message: InAppMessage
36
+ ) => {
37
+ const { appId, region } = resolveConfig();
38
+
39
+ const { id, metadata } = message;
40
+ resolveCredentials()
41
+ .then(({ credentials, identityId }) => {
42
+ recordCore({
43
+ appId,
44
+ category: CATEGORY,
45
+ credentials,
46
+ event: {
47
+ name: event,
48
+ attributes: {
49
+ campaign_id: id,
50
+ delivery_type: DELIVERY_TYPE,
51
+ treatment_id: metadata?.treatmentId,
52
+ },
53
+ },
54
+ identityId,
55
+ region,
56
+ userAgentValue: getInAppMessagingUserAgentString(
57
+ InAppMessagingAction.DispatchEvent
58
+ ),
59
+ });
60
+ })
61
+ .catch(e => {
62
+ // An error occured while fetching credentials or persisting the event to the buffer
63
+ logger.warn('Failed to record event.', e);
64
+ });
65
+ };
66
+
67
+ export const getStartOfDay = (): string => {
68
+ const now = new Date();
69
+ now.setHours(0, 0, 0, 0);
70
+ return now.toISOString();
71
+ };
72
+
73
+ export const matchesEventType = (
74
+ { CampaignId, Schedule }: PinpointInAppMessage,
75
+ { name: eventType }: InAppMessagingEvent
76
+ ) => {
77
+ const { EventType } = Schedule?.EventFilter?.Dimensions;
78
+ const memoKey = `${CampaignId}:${eventType}`;
79
+ if (!eventNameMemo.hasOwnProperty(memoKey)) {
80
+ eventNameMemo[memoKey] = !!EventType?.Values.includes(eventType);
81
+ }
82
+ return eventNameMemo[memoKey];
83
+ };
84
+
85
+ export const matchesAttributes = (
86
+ { CampaignId, Schedule }: PinpointInAppMessage,
87
+ { attributes }: InAppMessagingEvent
88
+ ): boolean => {
89
+ const { Attributes } = Schedule?.EventFilter?.Dimensions;
90
+ if (isEmpty(Attributes)) {
91
+ // if message does not have attributes defined it does not matter what attributes are on the event
92
+ return true;
93
+ }
94
+ if (isEmpty(attributes)) {
95
+ // if message does have attributes but the event does not then it always fails the check
96
+ return false;
97
+ }
98
+ const memoKey = `${CampaignId}:${JSON.stringify(attributes)}`;
99
+ if (!eventAttributesMemo.hasOwnProperty(memoKey)) {
100
+ eventAttributesMemo[memoKey] = Object.entries(Attributes).every(
101
+ ([key, { Values }]) => Values.includes(attributes[key])
102
+ );
103
+ }
104
+ return eventAttributesMemo[memoKey];
105
+ };
106
+
107
+ export const matchesMetrics = (
108
+ { CampaignId, Schedule }: PinpointInAppMessage,
109
+ { metrics }: InAppMessagingEvent
110
+ ): boolean => {
111
+ const { Metrics } = Schedule?.EventFilter?.Dimensions;
112
+ if (isEmpty(Metrics)) {
113
+ // if message does not have metrics defined it does not matter what metrics are on the event
114
+ return true;
115
+ }
116
+ if (isEmpty(metrics)) {
117
+ // if message does have metrics but the event does not then it always fails the check
118
+ return false;
119
+ }
120
+ const memoKey = `${CampaignId}:${JSON.stringify(metrics)}`;
121
+ if (!eventMetricsMemo.hasOwnProperty(memoKey)) {
122
+ eventMetricsMemo[memoKey] = Object.entries(Metrics).every(
123
+ ([key, { ComparisonOperator, Value }]) => {
124
+ const compare = getComparator(ComparisonOperator);
125
+ // if there is some unknown comparison operator, treat as a comparison failure
126
+ return compare ? compare(Value, metrics[key]) : false;
127
+ }
128
+ );
129
+ }
130
+ return eventMetricsMemo[memoKey];
131
+ };
132
+
133
+ export const getComparator = (operator: string): MetricsComparator => {
134
+ switch (operator) {
135
+ case 'EQUAL':
136
+ return (metricsVal, eventVal) => metricsVal === eventVal;
137
+ case 'GREATER_THAN':
138
+ return (metricsVal, eventVal) => metricsVal < eventVal;
139
+ case 'GREATER_THAN_OR_EQUAL':
140
+ return (metricsVal, eventVal) => metricsVal <= eventVal;
141
+ case 'LESS_THAN':
142
+ return (metricsVal, eventVal) => metricsVal > eventVal;
143
+ case 'LESS_THAN_OR_EQUAL':
144
+ return (metricsVal, eventVal) => metricsVal >= eventVal;
145
+ default:
146
+ return null;
147
+ }
148
+ };
149
+
150
+ export const isBeforeEndDate = ({
151
+ Schedule,
152
+ }: PinpointInAppMessage): boolean => {
153
+ if (!Schedule?.EndDate) {
154
+ return true;
155
+ }
156
+ return new Date() < new Date(Schedule.EndDate);
157
+ };
158
+
159
+ export const isQuietTime = (message: PinpointInAppMessage): boolean => {
160
+ const { Schedule } = message;
161
+ if (!Schedule?.QuietTime) {
162
+ return false;
163
+ }
164
+
165
+ const pattern = /^[0-2]\d:[0-5]\d$/; // basic sanity check, not a fully featured HH:MM validation
166
+ const { Start, End } = Schedule.QuietTime;
167
+ if (
168
+ !Start ||
169
+ !End ||
170
+ Start === End ||
171
+ !pattern.test(Start) ||
172
+ !pattern.test(End)
173
+ ) {
174
+ return false;
175
+ }
176
+
177
+ const now = new Date();
178
+ const start = new Date(now);
179
+ const end = new Date(now);
180
+ const [startHours, startMinutes] = Start.split(':');
181
+ const [endHours, endMinutes] = End.split(':');
182
+
183
+ start.setHours(
184
+ Number.parseInt(startHours, 10),
185
+ Number.parseInt(startMinutes, 10),
186
+ 0,
187
+ 0
188
+ );
189
+ end.setHours(
190
+ Number.parseInt(endHours, 10),
191
+ Number.parseInt(endMinutes, 10),
192
+ 0,
193
+ 0
194
+ );
195
+
196
+ // if quiet time includes midnight, bump the end time to the next day
197
+ if (start > end) {
198
+ end.setDate(end.getDate() + 1);
199
+ }
200
+
201
+ const isQuietTime = now >= start && now <= end;
202
+ if (isQuietTime) {
203
+ logger.debug('message filtered due to quiet time', message);
204
+ }
205
+ return isQuietTime;
206
+ };
207
+
208
+ export const clearMemo = () => {
209
+ eventNameMemo = {};
210
+ eventAttributesMemo = {};
211
+ eventMetricsMemo = {};
212
+ };
213
+
214
+ // in the pinpoint console when a message is created with a Modal or Full Screen layout,
215
+ // it is assigned a layout value of MOBILE_FEED or OVERLAYS respectively in the message payload.
216
+ // In the future, Pinpoint will be updating the layout values in the aforementioned scenario
217
+ // to MODAL and FULL_SCREEN.
218
+ //
219
+ // This utility acts as a safeguard to ensure that:
220
+ // - 1. the usage of MOBILE_FEED and OVERLAYS as values for message layouts are not leaked
221
+ // outside the Pinpoint provider
222
+ // - 2. Amplify correctly handles the legacy layout values from Pinpoint after they are updated
223
+ export const interpretLayout = (
224
+ layout: PinpointInAppMessage['InAppMessage']['Layout']
225
+ ): InAppMessageLayout => {
226
+ if (layout === 'MOBILE_FEED') {
227
+ return 'MODAL';
228
+ }
229
+
230
+ if (layout === 'OVERLAYS') {
231
+ return 'FULL_SCREEN';
232
+ }
233
+
234
+ // cast as PinpointInAppMessage['InAppMessage']['Layout'] allows `string` as a value
235
+ return layout as InAppMessageLayout;
236
+ };
237
+
238
+ export const extractContent = ({
239
+ InAppMessage: message,
240
+ }: PinpointInAppMessage): InAppMessageContent[] => {
241
+ return (
242
+ message?.Content?.map(content => {
243
+ const {
244
+ BackgroundColor,
245
+ BodyConfig,
246
+ HeaderConfig,
247
+ ImageUrl,
248
+ PrimaryBtn,
249
+ SecondaryBtn,
250
+ } = content;
251
+ const defaultPrimaryButton = PrimaryBtn?.DefaultConfig;
252
+ const defaultSecondaryButton = SecondaryBtn?.DefaultConfig;
253
+ const extractedContent: InAppMessageContent = {};
254
+ if (BackgroundColor) {
255
+ extractedContent.container = {
256
+ style: {
257
+ backgroundColor: BackgroundColor,
258
+ },
259
+ };
260
+ }
261
+ if (HeaderConfig) {
262
+ extractedContent.header = {
263
+ content: HeaderConfig.Header,
264
+ style: {
265
+ color: HeaderConfig.TextColor,
266
+ textAlign:
267
+ HeaderConfig.Alignment.toLowerCase() as InAppMessageTextAlign,
268
+ },
269
+ };
270
+ }
271
+ if (BodyConfig) {
272
+ extractedContent.body = {
273
+ content: BodyConfig.Body,
274
+ style: {
275
+ color: BodyConfig.TextColor,
276
+ textAlign:
277
+ BodyConfig.Alignment.toLowerCase() as InAppMessageTextAlign,
278
+ },
279
+ };
280
+ }
281
+ if (ImageUrl) {
282
+ extractedContent.image = {
283
+ src: ImageUrl,
284
+ };
285
+ }
286
+ if (defaultPrimaryButton) {
287
+ extractedContent.primaryButton = {
288
+ title: defaultPrimaryButton.Text,
289
+ action: defaultPrimaryButton.ButtonAction as InAppMessageAction,
290
+ url: defaultPrimaryButton.Link,
291
+ style: {
292
+ backgroundColor: defaultPrimaryButton.BackgroundColor,
293
+ borderRadius: defaultPrimaryButton.BorderRadius,
294
+ color: defaultPrimaryButton.TextColor,
295
+ },
296
+ };
297
+ }
298
+ if (defaultSecondaryButton) {
299
+ extractedContent.secondaryButton = {
300
+ title: defaultSecondaryButton.Text,
301
+ action: defaultSecondaryButton.ButtonAction as InAppMessageAction,
302
+ url: defaultSecondaryButton.Link,
303
+ style: {
304
+ backgroundColor: defaultSecondaryButton.BackgroundColor,
305
+ borderRadius: defaultSecondaryButton.BorderRadius,
306
+ color: defaultSecondaryButton.TextColor,
307
+ },
308
+ };
309
+ }
310
+ return extractedContent;
311
+ }) ?? []
312
+ );
313
+ };
314
+
315
+ export const extractMetadata = ({
316
+ InAppMessage,
317
+ Priority,
318
+ Schedule,
319
+ TreatmentId,
320
+ }: PinpointInAppMessage): InAppMessage['metadata'] => ({
321
+ customData: InAppMessage?.CustomConfig,
322
+ endDate: Schedule?.EndDate,
323
+ priority: Priority,
324
+ treatmentId: TreatmentId,
325
+ });
@@ -10,3 +10,9 @@ export {
10
10
  CHANNEL_TYPE,
11
11
  STORAGE_KEY_SUFFIX,
12
12
  } from './constants';
13
+
14
+ export {
15
+ processInAppMessages,
16
+ sessionStateChangeHandler,
17
+ incrementMessageCounts,
18
+ } from './messageProcessingHelpers';
@@ -0,0 +1,196 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { InAppMessage, InAppMessagingEvent } from '../../../types';
5
+ import {
6
+ InAppMessageCounts,
7
+ InAppMessageCountMap,
8
+ DailyInAppMessageCounter,
9
+ } from '../types';
10
+ import {
11
+ extractContent,
12
+ extractMetadata,
13
+ interpretLayout,
14
+ isBeforeEndDate,
15
+ matchesAttributes,
16
+ matchesEventType,
17
+ matchesMetrics,
18
+ } from './helpers';
19
+ import type { InAppMessageCampaign as PinpointInAppMessage } from '@aws-amplify/core/internals/aws-clients/pinpoint';
20
+ import { ConsoleLogger } from '@aws-amplify/core/internals/utils';
21
+ import { defaultStorage } from '@aws-amplify/core';
22
+ import { SessionState } from '../../../sessionTracker';
23
+
24
+ const MESSAGE_DAILY_COUNT_KEY = 'pinpointProvider_inAppMessages_dailyCount';
25
+ const MESSAGE_TOTAL_COUNT_KEY = 'pinpointProvider_inAppMessages_totalCount';
26
+ const logger = new ConsoleLogger('InAppMessaging.processInAppMessages');
27
+
28
+ let sessionMessageCountMap: InAppMessageCountMap = {};
29
+
30
+ export async function processInAppMessages(
31
+ messages: PinpointInAppMessage[],
32
+ event: InAppMessagingEvent
33
+ ): Promise<InAppMessage[]> {
34
+ let highestPrioritySeen: number;
35
+ let acc: PinpointInAppMessage[] = [];
36
+ for (let index = 0; index < messages.length; index++) {
37
+ const message = messages[index];
38
+ const messageQualifies =
39
+ matchesEventType(message, event) &&
40
+ matchesAttributes(message, event) &&
41
+ matchesMetrics(message, event) &&
42
+ isBeforeEndDate(message) &&
43
+ (await isBelowCap(message));
44
+ // filter all qualifying messages returning only those that are of (relative) highest priority
45
+ if (messageQualifies) {
46
+ // have not yet encountered message with priority
47
+ if (!highestPrioritySeen) {
48
+ // this message has priority, so reset the accumulator with this message only
49
+ if (message.Priority) {
50
+ highestPrioritySeen = message.Priority;
51
+ acc = [message];
52
+ } else {
53
+ // this message also has no priority, so just add this message to accumulator
54
+ acc.push(message);
55
+ }
56
+ // have previously encountered message with priority, so only messages with priority matter now
57
+ } else if (message.Priority) {
58
+ // this message has higher priority (lower number), so reset the accumulator with this message only
59
+ if (message.Priority < highestPrioritySeen) {
60
+ highestPrioritySeen = message.Priority;
61
+ acc = [message];
62
+ // this message has the same priority, so just add this message to accumulator
63
+ } else if (message.Priority === highestPrioritySeen) {
64
+ acc.push(message);
65
+ }
66
+ }
67
+ }
68
+ }
69
+ return normalizeMessages(acc);
70
+ }
71
+
72
+ export function sessionStateChangeHandler(state: SessionState): void {
73
+ if (state === 'started') {
74
+ console.log('Resetting the count');
75
+ // reset all session counts
76
+ sessionMessageCountMap = {};
77
+ }
78
+ }
79
+
80
+ export async function incrementMessageCounts(messageId: string): Promise<void> {
81
+ const { sessionCount, dailyCount, totalCount } = await getMessageCounts(
82
+ messageId
83
+ );
84
+ setSessionCount(messageId, sessionCount + 1);
85
+ setDailyCount(dailyCount + 1);
86
+ await setTotalCount(messageId, totalCount + 1);
87
+ }
88
+
89
+ function normalizeMessages(messages: PinpointInAppMessage[]): InAppMessage[] {
90
+ return messages.map(message => {
91
+ const { CampaignId, InAppMessage } = message;
92
+ return {
93
+ id: CampaignId,
94
+ content: extractContent(message),
95
+ layout: interpretLayout(InAppMessage.Layout),
96
+ metadata: extractMetadata(message),
97
+ };
98
+ });
99
+ }
100
+
101
+ async function isBelowCap({
102
+ CampaignId,
103
+ SessionCap,
104
+ DailyCap,
105
+ TotalCap,
106
+ }: PinpointInAppMessage): Promise<Boolean> {
107
+ const { sessionCount, dailyCount, totalCount } = await getMessageCounts(
108
+ CampaignId
109
+ );
110
+
111
+ return (
112
+ (!SessionCap || sessionCount < SessionCap) &&
113
+ (!DailyCap || dailyCount < DailyCap) &&
114
+ (!TotalCap || totalCount < TotalCap)
115
+ );
116
+ }
117
+
118
+ async function getMessageCounts(
119
+ messageId: string
120
+ ): Promise<InAppMessageCounts> {
121
+ try {
122
+ return {
123
+ sessionCount: getSessionCount(messageId),
124
+ dailyCount: await getDailyCount(),
125
+ totalCount: await getTotalCount(messageId),
126
+ };
127
+ } catch (err) {
128
+ logger.error('Failed to get message counts from storage', err);
129
+ }
130
+ }
131
+
132
+ function getSessionCount(messageId: string): number {
133
+ return sessionMessageCountMap[messageId] ?? 0;
134
+ }
135
+
136
+ function setSessionCount(messageId: string, count: number): void {
137
+ sessionMessageCountMap[messageId] = count;
138
+ }
139
+
140
+ function setDailyCount(count: number): void {
141
+ const dailyCount: DailyInAppMessageCounter = {
142
+ count,
143
+ lastCountTimestamp: getStartOfDay(),
144
+ };
145
+ try {
146
+ defaultStorage.setItem(MESSAGE_DAILY_COUNT_KEY, JSON.stringify(dailyCount));
147
+ } catch (err) {
148
+ logger.error('Failed to save daily message count to storage', err);
149
+ }
150
+ }
151
+
152
+ function setTotalCountMap(countMap: InAppMessageCountMap): void {
153
+ try {
154
+ defaultStorage.setItem(MESSAGE_TOTAL_COUNT_KEY, JSON.stringify(countMap));
155
+ } catch (err) {
156
+ logger.error('Failed to save total count to storage', err);
157
+ }
158
+ }
159
+
160
+ async function setTotalCount(messageId: string, count: number): Promise<void> {
161
+ const totalCountMap = await getTotalCountMap();
162
+ const updatedMap = {
163
+ ...totalCountMap,
164
+ [messageId]: count,
165
+ };
166
+ setTotalCountMap(updatedMap);
167
+ }
168
+
169
+ async function getDailyCount(): Promise<number> {
170
+ const today = getStartOfDay();
171
+ const item = await defaultStorage.getItem(MESSAGE_DAILY_COUNT_KEY);
172
+ // Parse stored count or initialize as empty count
173
+ const counter: DailyInAppMessageCounter = item
174
+ ? JSON.parse(item)
175
+ : { count: 0, lastCountTimestamp: today };
176
+ // If the stored counter timestamp is today, use it as the count, otherwise reset to 0
177
+ return counter.lastCountTimestamp === today ? counter.count : 0;
178
+ }
179
+
180
+ async function getTotalCountMap(): Promise<InAppMessageCountMap> {
181
+ const item = await defaultStorage.getItem(MESSAGE_TOTAL_COUNT_KEY);
182
+ // Parse stored count map or initialize as empty
183
+ return item ? JSON.parse(item) : {};
184
+ }
185
+
186
+ async function getTotalCount(messageId: string): Promise<number> {
187
+ const countMap = await getTotalCountMap();
188
+ // Return stored count or initialize as empty count
189
+ return countMap[messageId] || 0;
190
+ }
191
+
192
+ const getStartOfDay = (): string => {
193
+ const now = new Date();
194
+ now.setHours(0, 0, 0, 0);
195
+ return now.toISOString();
196
+ };
@@ -5,4 +5,10 @@ export { InAppMessagingServiceOptions } from './options';
5
5
  export { InAppMessagingIdentifyUserInput } from './inputs';
6
6
  export { InAppMessagingConfig } from './config';
7
7
  export { InAppMessageInteractionEvent, InAppMessagingEvent } from './event';
8
- export { InAppMessage } from './message';
8
+ export {
9
+ InAppMessage,
10
+ InAppMessageAction,
11
+ InAppMessageContent,
12
+ InAppMessageLayout,
13
+ InAppMessageTextAlign,
14
+ } from './message';
@@ -0,0 +1,8 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export {
5
+ isInitialized,
6
+ assertIsInitialized,
7
+ initialize,
8
+ } from './statusHelpers';