Skip to content

Commit

Permalink
trackProposition command
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwaters committed Apr 26, 2024
1 parent 2a1ec9e commit e8457e2
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 41 deletions.
4 changes: 3 additions & 1 deletion src/components/Personalization/createComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default ({
viewCache,
showContainers,
applyPropositions,
trackProposition,
setTargetMigration,
mergeDecisionsMeta,
renderedPropositions,
Expand Down Expand Up @@ -121,7 +122,8 @@ export default ({
optionsValidator: options =>
validateApplyPropositionsOptions({ logger, options }),
run: applyPropositions
}
},
trackProposition: trackProposition.command
}
};
};
116 changes: 116 additions & 0 deletions src/components/Personalization/createTrackProposition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { anything, arrayOf, objectOf, string } from "../../utils/validation";
import createDecorateProposition from "./handlers/createDecorateProposition";
import { includes } from "../../utils";
import { HTML_CONTENT_ITEM, JSON_CONTENT_ITEM } from "../../constants/schema";
import isDomElement from "./dom-actions/dom/isDomElement";
import selectNodes from "../../utils/dom/selectNodes";

const validateOptions = ({ options }) => {
const validator = objectOf({
proposition: objectOf({
id: string(),
scope: string(),
scopeDetails: anything(),
items: arrayOf(anything())
}).required(),
element: anything(),
selector: string()
}).noUnknownFields();

return validator(options);
};

const REQUIRED_FIELDS_PROPOSITION = ["id", "scope", "scopeDetails", "items"];
const REQUIRED_FIELDS_ITEM = ["id", "schema", "data"];

const checkMalformedPropositionJSON = propositionJSON => {
for (let i = 0; i < REQUIRED_FIELDS_PROPOSITION.length; i += 1) {
if (
!Object.hasOwnProperty.call(
propositionJSON,
REQUIRED_FIELDS_PROPOSITION[i]
)
) {
return new Error(
`Proposition object is missing "${REQUIRED_FIELDS_PROPOSITION[i]}" field`
);
}
}

const { items } = propositionJSON;

if (!Array.isArray(items)) {
return new Error(`Proposition items must be an Array`);
}

for (let i = 0; i < items.length; i += 1) {
for (let j = 0; j < REQUIRED_FIELDS_ITEM.length; j += 1) {
if (!Object.hasOwnProperty.call(items[i], REQUIRED_FIELDS_ITEM[j])) {
return new Error(
`Proposition item is missing "${REQUIRED_FIELDS_ITEM[j]}" field`
);
}
}
}

return undefined;
};

export default ({
autoTrackPropositionInteractions,
storeInteractionMeta,
createProposition
}) => {
const run = ({ proposition: propositionJSON, element, selector }) => {
const error = checkMalformedPropositionJSON(propositionJSON);

if (error) {
return Promise.reject(error);
}

const elements = isDomElement(element) ? [element] : selectNodes(selector);

const proposition = createProposition(propositionJSON);

proposition
.getItems()
.filter(item =>
includes([HTML_CONTENT_ITEM, JSON_CONTENT_ITEM], item.getSchema())
)
.forEach(item => {
const decorateProposition = createDecorateProposition(
autoTrackPropositionInteractions,
item.getSchemaType(),
proposition.getId(),
item.getId(),
item.getTrackingLabel(),
proposition.getScopeType(),
proposition.getNotification(),
storeInteractionMeta
);
elements.forEach(el => decorateProposition(el));
});

return Promise.resolve();
};

const optionsValidator = options => validateOptions({ options });

return {
command: {
optionsValidator,
run
}
};
};
13 changes: 13 additions & 0 deletions src/components/Personalization/dom-actions/dom/isDomElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
export default element =>
element instanceof Element || element instanceof HTMLDocument;
9 changes: 9 additions & 0 deletions src/components/Personalization/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
NEVER,
PROPOSITION_INTERACTION_TYPES
} from "../../constants/propositionInteractionType";
import createTrackProposition from "./createTrackProposition";

const createPersonalization = ({ config, logger, eventManager }) => {
const {
Expand Down Expand Up @@ -147,6 +148,13 @@ const createPersonalization = ({ config, logger, eventManager }) => {
getClickSelectors,
autoTrackPropositionInteractions
});

const trackProposition = createTrackProposition({
autoTrackPropositionInteractions,
storeInteractionMeta,
createProposition
});

const viewChangeHandler = createViewChangeHandler({
processPropositions,
viewCache
Expand Down Expand Up @@ -178,6 +186,7 @@ const createPersonalization = ({ config, logger, eventManager }) => {
viewCache,
showContainers,
applyPropositions,
trackProposition,
setTargetMigration,
mergeDecisionsMeta,
renderedPropositions,
Expand Down
1 change: 1 addition & 0 deletions test/functional/helpers/createAlloyProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const commands = [
"getLibraryInfo",
"appendIdentityToUrl",
"applyPropositions",
"trackProposition",
"subscribeRulesetItems",
"evaluateRulesets"
];
Expand Down
3 changes: 2 additions & 1 deletion test/functional/specs/LibraryInfo/C2589.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ test("C2589: getLibraryInfo command returns library information.", async () => {
"getIdentity",
"sendEvent",
"setConsent",
"setDebug"
"setDebug",
"trackProposition"
];
const currentConfigs = {
clickCollectionEnabled: true,
Expand Down
29 changes: 29 additions & 0 deletions test/unit/helpers/createMockProposition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import injectCreateProposition from "../../../src/components/Personalization/handlers/injectCreateProposition";

const createProposition = injectCreateProposition({
preprocess: data => data,
isPageWideSurface: () => false
});

export default (item, scopeDetails = {}) => {
return createProposition({
id: "id",
scope: "scope",
scopeDetails: {
decisionProvider: "AJO",
...scopeDetails
},
items: [item]
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ describe("Personalization", () => {
let renderedPropositions;
let cacheUpdate;

const trackProposition = {
optionsValidator: jasmine.createSpy("optionsValidator"),
run: jasmine.createSpy("run")
};

const build = () => {
personalizationComponent = createComponent({
logger,
Expand All @@ -40,7 +45,8 @@ describe("Personalization", () => {
showContainers,
setTargetMigration,
mergeDecisionsMeta,
renderedPropositions
renderedPropositions,
trackProposition
});
};

Expand Down
Loading

0 comments on commit e8457e2

Please sign in to comment.