@adobe/alloy 2.19.0-beta.11 → 2.19.0-beta.13

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 (101) hide show
  1. package/libEs5/components/DataCollector/index.js +3 -0
  2. package/libEs5/components/DataCollector/validateApplyResponse.js +2 -1
  3. package/libEs5/components/DataCollector/validateUserEventOptions.js +2 -1
  4. package/libEs5/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.js +32 -0
  5. package/libEs5/components/DecisioningEngine/consequenceAdapters/schemaTypeConsequenceAdapter.js +25 -0
  6. package/libEs5/components/DecisioningEngine/constants.js +31 -0
  7. package/libEs5/components/DecisioningEngine/createApplyResponse.js +34 -0
  8. package/libEs5/components/DecisioningEngine/createConsequenceAdapter.js +29 -0
  9. package/libEs5/components/DecisioningEngine/createContextProvider.js +93 -0
  10. package/libEs5/components/DecisioningEngine/createDecisionHistory.js +28 -0
  11. package/libEs5/components/DecisioningEngine/createDecisionProvider.js +51 -0
  12. package/libEs5/components/DecisioningEngine/createEvaluableRulesetPayload.js +80 -0
  13. package/libEs5/components/DecisioningEngine/createEvaluateRulesetsCommand.js +49 -0
  14. package/libEs5/components/DecisioningEngine/createEventRegistry.js +123 -0
  15. package/libEs5/components/DecisioningEngine/createOnResponseHandler.js +43 -0
  16. package/libEs5/components/DecisioningEngine/createSubscribeRulesetItems.js +87 -0
  17. package/libEs5/components/DecisioningEngine/index.js +114 -0
  18. package/libEs5/components/DecisioningEngine/utils.js +83 -0
  19. package/libEs5/components/Personalization/createActionsProvider.js +72 -0
  20. package/libEs5/components/Personalization/createApplyPropositions.js +2 -2
  21. package/libEs5/components/Personalization/createCollect.js +9 -4
  22. package/libEs5/components/Personalization/createComponent.js +5 -3
  23. package/libEs5/components/Personalization/createOnClickHandler.js +5 -3
  24. package/libEs5/components/Personalization/createOnDecisionHandler.js +45 -0
  25. package/libEs5/components/Personalization/createPersonalizationDetails.js +8 -5
  26. package/libEs5/components/Personalization/createPreprocessors.js +21 -0
  27. package/libEs5/components/Personalization/createViewCacheManager.js +9 -2
  28. package/libEs5/components/Personalization/event.js +25 -22
  29. package/libEs5/components/Personalization/handlers/createProcessInAppMessage.js +76 -0
  30. package/libEs5/components/Personalization/in-app-message-actions/actions/displayIframeContent.js +288 -0
  31. package/libEs5/components/Personalization/in-app-message-actions/initInAppMessageActionsModules.js +23 -0
  32. package/libEs5/components/Personalization/in-app-message-actions/utils.js +53 -0
  33. package/libEs5/components/Personalization/index.js +17 -5
  34. package/libEs5/constants/contentType.js +18 -0
  35. package/libEs5/constants/eventType.js +25 -0
  36. package/libEs5/{components/Personalization/constants/eventType.js → constants/handle.js} +4 -7
  37. package/libEs5/constants/libraryVersion.js +1 -1
  38. package/libEs5/constants/propositionEventType.js +33 -0
  39. package/libEs5/{components/Personalization/constants → constants}/schema.js +9 -1
  40. package/libEs5/core/componentCreators.js +2 -1
  41. package/libEs5/core/config/createCoreConfigs.js +1 -0
  42. package/libEs5/core/createEvent.js +26 -1
  43. package/libEs5/core/createEventManager.js +3 -0
  44. package/libEs5/core/createLifecycle.js +4 -1
  45. package/libEs5/utils/createSubscription.js +91 -0
  46. package/libEs5/utils/debounce.js +30 -0
  47. package/libEs5/utils/flattenArray.js +37 -0
  48. package/libEs5/utils/flattenObject.js +43 -0
  49. package/libEs5/utils/index.js +7 -0
  50. package/libEs5/utils/parseUrl.js +70 -0
  51. package/libEs6/components/DataCollector/index.js +2 -0
  52. package/libEs6/components/DataCollector/validateApplyResponse.js +2 -1
  53. package/libEs6/components/DataCollector/validateUserEventOptions.js +2 -1
  54. package/libEs6/components/DecisioningEngine/consequenceAdapters/inAppMessageConsequenceAdapter.js +30 -0
  55. package/{libEs5/components/Personalization/constants/propositionEventType.js → libEs6/components/DecisioningEngine/consequenceAdapters/schemaTypeConsequenceAdapter.js} +13 -9
  56. package/libEs6/components/DecisioningEngine/constants.js +25 -0
  57. package/libEs6/components/DecisioningEngine/createApplyResponse.js +29 -0
  58. package/libEs6/components/DecisioningEngine/createConsequenceAdapter.js +29 -0
  59. package/libEs6/components/DecisioningEngine/createContextProvider.js +95 -0
  60. package/libEs6/components/DecisioningEngine/createDecisionHistory.js +25 -0
  61. package/libEs6/components/DecisioningEngine/createDecisionProvider.js +41 -0
  62. package/libEs6/components/DecisioningEngine/createEvaluableRulesetPayload.js +79 -0
  63. package/libEs6/components/DecisioningEngine/createEvaluateRulesetsCommand.js +45 -0
  64. package/libEs6/components/DecisioningEngine/createEventRegistry.js +113 -0
  65. package/libEs6/components/DecisioningEngine/createOnResponseHandler.js +42 -0
  66. package/libEs6/components/DecisioningEngine/createSubscribeRulesetItems.js +79 -0
  67. package/libEs6/components/DecisioningEngine/index.js +119 -0
  68. package/libEs6/components/DecisioningEngine/utils.js +74 -0
  69. package/libEs6/components/Personalization/createActionsProvider.js +70 -0
  70. package/libEs6/components/Personalization/createApplyPropositions.js +2 -2
  71. package/libEs6/components/Personalization/createCollect.js +7 -4
  72. package/libEs6/components/Personalization/createComponent.js +5 -3
  73. package/libEs6/components/Personalization/createOnClickHandler.js +5 -3
  74. package/libEs6/components/Personalization/createOnDecisionHandler.js +43 -0
  75. package/libEs6/components/Personalization/createPersonalizationDetails.js +8 -5
  76. package/libEs6/components/Personalization/createPreprocessors.js +19 -0
  77. package/libEs6/components/Personalization/createViewCacheManager.js +12 -3
  78. package/libEs6/components/Personalization/event.js +14 -9
  79. package/libEs6/components/Personalization/handlers/createProcessInAppMessage.js +77 -0
  80. package/libEs6/components/Personalization/in-app-message-actions/actions/displayIframeContent.js +291 -0
  81. package/libEs6/components/Personalization/in-app-message-actions/initInAppMessageActionsModules.js +17 -0
  82. package/libEs6/components/Personalization/in-app-message-actions/utils.js +49 -0
  83. package/libEs6/components/Personalization/index.js +19 -6
  84. package/libEs6/{components/Personalization/constants/eventType.js → constants/contentType.js} +3 -4
  85. package/libEs6/constants/eventType.js +17 -0
  86. package/libEs6/{components/Personalization/constants/propositionEventType.js → constants/handle.js} +2 -6
  87. package/libEs6/constants/libraryVersion.js +1 -1
  88. package/libEs6/constants/propositionEventType.js +33 -0
  89. package/libEs6/{components/Personalization/constants → constants}/schema.js +4 -0
  90. package/libEs6/core/componentCreators.js +2 -1
  91. package/libEs6/core/config/createCoreConfigs.js +1 -0
  92. package/libEs6/core/createEvent.js +26 -1
  93. package/libEs6/core/createEventManager.js +2 -0
  94. package/libEs6/core/createLifecycle.js +4 -1
  95. package/libEs6/utils/createSubscription.js +70 -0
  96. package/libEs6/utils/debounce.js +22 -0
  97. package/libEs6/utils/flattenArray.js +26 -0
  98. package/libEs6/utils/flattenObject.js +28 -0
  99. package/libEs6/utils/index.js +1 -0
  100. package/libEs6/utils/parseUrl.js +62 -0
  101. package/package.json +6 -2
@@ -0,0 +1,29 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ export default (lifecycle => {
13
+ return ({
14
+ viewName,
15
+ renderDecisions = false,
16
+ propositions = []
17
+ }) => {
18
+ if (propositions.length > 0 && lifecycle) {
19
+ lifecycle.onDecision({
20
+ renderDecisions,
21
+ viewName,
22
+ propositions
23
+ });
24
+ }
25
+ return {
26
+ propositions
27
+ };
28
+ };
29
+ });
@@ -0,0 +1,29 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import inAppMessageConsequenceAdapter from "./consequenceAdapters/inAppMessageConsequenceAdapter";
13
+ import schemaTypeConsequenceAdapter from "./consequenceAdapters/schemaTypeConsequenceAdapter";
14
+ const CJM_IN_APP_MESSAGE_TYPE = "cjmiam";
15
+ const SCHEMA = "schema";
16
+ const adapters = {
17
+ [CJM_IN_APP_MESSAGE_TYPE]: inAppMessageConsequenceAdapter,
18
+ [SCHEMA]: schemaTypeConsequenceAdapter
19
+ };
20
+ export default (() => {
21
+ return consequence => {
22
+ const {
23
+ id,
24
+ type,
25
+ detail
26
+ } = consequence;
27
+ return typeof adapters[type] === "function" ? adapters[type](id, type, detail) : detail;
28
+ };
29
+ });
@@ -0,0 +1,95 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import getBrowser from "../../utils/getBrowser";
13
+ import parseUrl from "../../utils/parseUrl";
14
+ import flattenObject from "../../utils/flattenObject";
15
+ import libraryVersion from "../../constants/libraryVersion";
16
+ export default (({
17
+ eventRegistry,
18
+ window
19
+ }) => {
20
+ const pageLoadTimestamp = new Date().getTime();
21
+ const getBrowserContext = () => {
22
+ return {
23
+ name: getBrowser(window)
24
+ };
25
+ };
26
+ const getPageContext = () => {
27
+ return {
28
+ title: window.title,
29
+ url: window.url,
30
+ ...parseUrl(window.url)
31
+ };
32
+ };
33
+ const getReferrerContext = () => {
34
+ return {
35
+ url: window.referrer,
36
+ ...parseUrl(window.referrer)
37
+ };
38
+ };
39
+ const getTimeContext = () => {
40
+ const now = new Date();
41
+ const currentTimestamp = now.getTime();
42
+ return {
43
+ pageLoadTimestamp,
44
+ currentTimestamp,
45
+ currentDate: now.getDate(),
46
+ // Day of the week starts on Monday as is practiced in ISO 8601, but we want it to start on Sunday to match the authoring UI rule
47
+ "~state.com.adobe.module.lifecycle/lifecyclecontextdata.dayofweek": now.getDay() + 1,
48
+ "~state.com.adobe.module.lifecycle/lifecyclecontextdata.hourofday": now.getHours(),
49
+ currentMinute: now.getMinutes(),
50
+ currentMonth: now.getMonth(),
51
+ currentYear: now.getFullYear(),
52
+ pageVisitDuration: currentTimestamp - pageLoadTimestamp,
53
+ "~timestampu": currentTimestamp / 1000,
54
+ "~timestampz": now.toISOString()
55
+ };
56
+ };
57
+ const getWindowContext = () => {
58
+ const height = window.height;
59
+ const width = window.width;
60
+ const scrollY = window.scrollY;
61
+ const scrollX = window.scrollX;
62
+ return {
63
+ height,
64
+ width,
65
+ scrollY,
66
+ scrollX
67
+ };
68
+ };
69
+ const coreGlobalContext = {
70
+ browser: getBrowserContext(),
71
+ page: getPageContext(),
72
+ referringPage: getReferrerContext()
73
+ };
74
+ const getGlobalContext = () => {
75
+ return {
76
+ ...coreGlobalContext,
77
+ ...getTimeContext(),
78
+ window: getWindowContext(),
79
+ "~sdkver": libraryVersion
80
+ };
81
+ };
82
+ const getContext = (addedContext = {}) => {
83
+ const context = {
84
+ ...getGlobalContext(),
85
+ ...addedContext
86
+ };
87
+ return {
88
+ ...flattenObject(context),
89
+ events: eventRegistry.toJSON()
90
+ };
91
+ };
92
+ return {
93
+ getContext
94
+ };
95
+ });
@@ -0,0 +1,25 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import { PropositionEventType } from "../../constants/propositionEventType";
13
+ export default (({
14
+ eventRegistry
15
+ }) => {
16
+ const recordQualified = id => {
17
+ if (!id) {
18
+ return undefined;
19
+ }
20
+ return eventRegistry.addEvent({}, PropositionEventType.TRIGGER, id);
21
+ };
22
+ return {
23
+ recordQualified
24
+ };
25
+ });
@@ -0,0 +1,41 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import createEvaluableRulesetPayload from "./createEvaluableRulesetPayload";
13
+ import createDecisionHistory from "./createDecisionHistory";
14
+ import { getActivityId } from "./utils";
15
+ export default (({
16
+ eventRegistry
17
+ }) => {
18
+ const payloadsBasedOnActivityId = {};
19
+ const decisionHistory = createDecisionHistory({
20
+ eventRegistry
21
+ });
22
+ const addPayload = payload => {
23
+ const activityId = getActivityId(payload);
24
+ if (!activityId) {
25
+ return;
26
+ }
27
+ const evaluableRulesetPayload = createEvaluableRulesetPayload(payload, eventRegistry, decisionHistory);
28
+ if (evaluableRulesetPayload.isEvaluable) {
29
+ payloadsBasedOnActivityId[activityId] = evaluableRulesetPayload;
30
+ }
31
+ };
32
+ const evaluate = (context = {}) => Object.values(payloadsBasedOnActivityId).map(payload => payload.evaluate(context)).filter(payload => payload.items.length > 0);
33
+ const addPayloads = personalizationPayloads => {
34
+ personalizationPayloads.forEach(addPayload);
35
+ };
36
+ return {
37
+ addPayload,
38
+ addPayloads,
39
+ evaluate
40
+ };
41
+ });
@@ -0,0 +1,79 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import RulesEngine from "@adobe/aep-rules-engine";
13
+ import { JSON_CONTENT_ITEM, RULESET_ITEM } from "../../constants/schema";
14
+ import { DISPLAY } from "../../constants/eventType";
15
+ import { getActivityId } from "./utils";
16
+ import flattenArray from "../../utils/flattenArray";
17
+ import createConsequenceAdapter from "./createConsequenceAdapter";
18
+ const isRulesetItem = item => {
19
+ const {
20
+ schema,
21
+ data
22
+ } = item;
23
+ if (schema === RULESET_ITEM) {
24
+ return true;
25
+ }
26
+ if (schema !== JSON_CONTENT_ITEM) {
27
+ return false;
28
+ }
29
+ try {
30
+ const content = typeof data.content === "string" ? JSON.parse(data.content) : data.content;
31
+ return content && Object.prototype.hasOwnProperty.call(content, "version") && Object.prototype.hasOwnProperty.call(content, "rules");
32
+ } catch (error) {
33
+ return false;
34
+ }
35
+ };
36
+ export default ((payload, eventRegistry, decisionHistory) => {
37
+ const consequenceAdapter = createConsequenceAdapter();
38
+ const activityId = getActivityId(payload);
39
+ const items = [];
40
+ const addItem = item => {
41
+ const {
42
+ data = {},
43
+ schema
44
+ } = item;
45
+ const content = schema === RULESET_ITEM ? data : data.content;
46
+ if (!content) {
47
+ return;
48
+ }
49
+ items.push(RulesEngine(typeof content === "string" ? JSON.parse(content) : content));
50
+ };
51
+ const evaluate = context => {
52
+ const displayEvent = eventRegistry.getEvent(DISPLAY, activityId);
53
+ const displayedDate = displayEvent ? displayEvent.firstTimestamp : undefined;
54
+ const qualifyingItems = flattenArray(items.map(item => item.execute(context))).map(consequenceAdapter).map(item => {
55
+ const {
56
+ firstTimestamp: qualifiedDate
57
+ } = decisionHistory.recordQualified(activityId);
58
+ return {
59
+ ...item,
60
+ data: {
61
+ ...item.data,
62
+ qualifiedDate,
63
+ displayedDate
64
+ }
65
+ };
66
+ });
67
+ return {
68
+ ...payload,
69
+ items: qualifyingItems
70
+ };
71
+ };
72
+ if (Array.isArray(payload.items)) {
73
+ payload.items.filter(isRulesetItem).forEach(addItem);
74
+ }
75
+ return {
76
+ evaluate,
77
+ isEvaluable: items.length > 0
78
+ };
79
+ });
@@ -0,0 +1,45 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import { boolean, objectOf } from "../../utils/validation";
13
+ const validateOptions = ({
14
+ options
15
+ }) => {
16
+ const validator = objectOf({
17
+ renderDecisions: boolean(),
18
+ personalization: objectOf({
19
+ decisionContext: objectOf({})
20
+ })
21
+ }).noUnknownFields();
22
+ return validator(options);
23
+ };
24
+ export default (({
25
+ contextProvider,
26
+ decisionProvider
27
+ }) => {
28
+ const run = ({
29
+ renderDecisions,
30
+ decisionContext,
31
+ applyResponse
32
+ }) => {
33
+ return applyResponse({
34
+ renderDecisions,
35
+ propositions: decisionProvider.evaluate(contextProvider.getContext(decisionContext))
36
+ });
37
+ };
38
+ const optionsValidator = options => validateOptions({
39
+ options
40
+ });
41
+ return {
42
+ optionsValidator,
43
+ run
44
+ };
45
+ });
@@ -0,0 +1,113 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import { createRestoreStorage, createSaveStorage, getExpirationDate, getActivityId, hasExperienceData } from "./utils";
13
+ import { EVENT_TYPE_TRUE } from "../../constants/eventType";
14
+ const STORAGE_KEY = "events";
15
+ const MAX_EVENT_RECORDS = 1000;
16
+ const RETENTION_PERIOD = 30;
17
+ const prefixed = key => `iam.${key}`;
18
+ export const createEventPruner = (limit = MAX_EVENT_RECORDS, retentionPeriod = RETENTION_PERIOD) => {
19
+ return events => {
20
+ const pruned = {};
21
+ Object.keys(events).forEach(eventType => {
22
+ pruned[eventType] = {};
23
+ Object.values(events[eventType]).filter(entry => new Date(entry.firstTimestamp) >= getExpirationDate(retentionPeriod)).sort((a, b) => a.firstTimestamp - b.firstTimestamp).slice(-1 * limit).forEach(entry => {
24
+ pruned[eventType][entry.event[prefixed("id")]] = entry;
25
+ });
26
+ });
27
+ return pruned;
28
+ };
29
+ };
30
+ export default (({
31
+ storage
32
+ }) => {
33
+ let currentStorage = storage;
34
+ let restore;
35
+ let save;
36
+ let events;
37
+ const setStorage = newStorage => {
38
+ currentStorage = newStorage;
39
+ restore = createRestoreStorage(currentStorage, STORAGE_KEY);
40
+ save = createSaveStorage(currentStorage, STORAGE_KEY, createEventPruner(MAX_EVENT_RECORDS, RETENTION_PERIOD));
41
+ events = restore({});
42
+ };
43
+ setStorage(storage);
44
+ const addEvent = (event, eventType, eventId, action) => {
45
+ if (!events[eventType]) {
46
+ events[eventType] = {};
47
+ }
48
+ const existingEvent = events[eventType][eventId];
49
+ const count = existingEvent ? existingEvent.count : 0;
50
+ const timestamp = new Date().getTime();
51
+ const firstTimestamp = existingEvent ? existingEvent.firstTimestamp || existingEvent.timestamp : timestamp;
52
+ events[eventType][eventId] = {
53
+ event: {
54
+ ...event,
55
+ [prefixed("id")]: eventId,
56
+ [prefixed("eventType")]: eventType,
57
+ [prefixed("action")]: action
58
+ },
59
+ firstTimestamp,
60
+ timestamp,
61
+ count: count + 1
62
+ };
63
+ save(events);
64
+ return events[eventType][eventId];
65
+ };
66
+ const addExperienceEdgeEvent = event => {
67
+ const {
68
+ xdm = {}
69
+ } = event.getContent();
70
+ const {
71
+ _experience
72
+ } = xdm;
73
+ if (!hasExperienceData(xdm)) {
74
+ return;
75
+ }
76
+ const {
77
+ decisioning = {}
78
+ } = _experience;
79
+ const {
80
+ propositionEventType: propositionEventTypeObj = {},
81
+ propositionAction = {},
82
+ propositions = []
83
+ } = decisioning;
84
+ const propositionEventTypesList = Object.keys(propositionEventTypeObj);
85
+
86
+ // https://wiki.corp.adobe.com/pages/viewpage.action?spaceKey=CJM&title=Proposition+Event+Types
87
+ if (propositionEventTypesList.length === 0) {
88
+ return;
89
+ }
90
+ const validPropositionEventType = propositionEventType => propositionEventTypeObj[propositionEventType] === EVENT_TYPE_TRUE;
91
+ const {
92
+ id: action
93
+ } = propositionAction;
94
+ propositionEventTypesList.filter(validPropositionEventType).forEach(propositionEventType => {
95
+ propositions.forEach(proposition => {
96
+ addEvent({}, propositionEventType, getActivityId(proposition), action);
97
+ });
98
+ });
99
+ };
100
+ const getEvent = (eventType, eventId) => {
101
+ if (!events[eventType]) {
102
+ return undefined;
103
+ }
104
+ return events[eventType][eventId];
105
+ };
106
+ return {
107
+ addExperienceEdgeEvent,
108
+ addEvent,
109
+ getEvent,
110
+ toJSON: () => events,
111
+ setStorage
112
+ };
113
+ });
@@ -0,0 +1,42 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import { PERSONALIZATION_DECISIONS_HANDLE } from "../../constants/handle";
13
+ import flattenObject from "../../utils/flattenObject";
14
+ export default (({
15
+ renderDecisions,
16
+ decisionProvider,
17
+ applyResponse,
18
+ event,
19
+ decisionContext
20
+ }) => {
21
+ const context = {
22
+ ...flattenObject(event.getContent()),
23
+ ...decisionContext
24
+ };
25
+ const viewName = event.getViewName();
26
+ return ({
27
+ response
28
+ }) => {
29
+ decisionProvider.addPayloads(response.getPayloadsByType(PERSONALIZATION_DECISIONS_HANDLE));
30
+
31
+ // only evaluate events that include a personalization query
32
+ if (!event.hasQuery()) {
33
+ return;
34
+ }
35
+ const propositions = decisionProvider.evaluate(context);
36
+ applyResponse({
37
+ viewName,
38
+ renderDecisions,
39
+ propositions
40
+ });
41
+ };
42
+ });
@@ -0,0 +1,79 @@
1
+ /*
2
+ Copyright 2023 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ import { arrayOf, callback as callbackType, objectOf, string } from "../../utils/validation";
13
+ import createSubscription from "../../utils/createSubscription";
14
+ import { includes } from "../../utils";
15
+ const validateOptions = ({
16
+ options
17
+ }) => {
18
+ const validator = objectOf({
19
+ surfaces: arrayOf(string()).uniqueItems(),
20
+ schemas: arrayOf(string()).uniqueItems(),
21
+ callback: callbackType().required()
22
+ }).noUnknownFields();
23
+ return validator(options);
24
+ };
25
+ const emissionPreprocessor = (params, propositions) => {
26
+ const {
27
+ surfacesFilter,
28
+ schemasFilter
29
+ } = params;
30
+ const result = {
31
+ propositions: propositions.filter(payload => surfacesFilter ? includes(surfacesFilter, payload.scope) : true).map(payload => {
32
+ const {
33
+ items = []
34
+ } = payload;
35
+ return {
36
+ ...payload,
37
+ items: items.filter(item => schemasFilter ? includes(schemasFilter, item.schema) : true)
38
+ };
39
+ }).filter(payload => payload.items.length > 0)
40
+ };
41
+ return [result];
42
+ };
43
+ const emissionCondition = (params, result) => {
44
+ return result.propositions.length > 0;
45
+ };
46
+ export default (() => {
47
+ const subscription = createSubscription();
48
+ subscription.setEmissionPreprocessor(emissionPreprocessor);
49
+ subscription.setEmissionCondition(emissionCondition);
50
+ const run = ({
51
+ surfaces,
52
+ schemas,
53
+ callback
54
+ }) => {
55
+ const unsubscribe = subscription.add(callback, {
56
+ surfacesFilter: surfaces instanceof Array ? surfaces : undefined,
57
+ schemasFilter: schemas instanceof Array ? schemas : undefined
58
+ });
59
+ return Promise.resolve({
60
+ unsubscribe
61
+ });
62
+ };
63
+ const optionsValidator = options => validateOptions({
64
+ options
65
+ });
66
+ const refresh = propositions => {
67
+ if (!subscription.hasSubscriptions()) {
68
+ return;
69
+ }
70
+ subscription.emit(propositions);
71
+ };
72
+ return {
73
+ refresh,
74
+ command: {
75
+ optionsValidator,
76
+ run
77
+ }
78
+ };
79
+ });