Skip to content

Instantly share code, notes, and snippets.

@dantodev
Last active November 18, 2021 16:40
Show Gist options
  • Save dantodev/bf17e314ac791d194f36543ce8d3000a to your computer and use it in GitHub Desktop.
Save dantodev/bf17e314ac791d194f36543ce8d3000a to your computer and use it in GitHub Desktop.
Rules Engine Example.js
window.xmSDK.require(["rules-engine"], ({ rulesEngine }) => {
const actions = rulesEngine.actions;
const conditions = rulesEngine.condition;
// show badge for 10% of the users that then opens a layered survey
rulesEngine.createPipeline(pipeline => {
pipeline.add(actions.diceRate({ all: 10 }));
pipeline.add(actions.showBadge({ position: "right" }));
pipeline.add(actions.showLayerSurvey({ surveyId: "survey-id" }));
});
// verify condition and then run tw o actions in parallel
rulesEngine.createPipeline(pipeline => {
pipeline.add(actions.verify(conditions.url({ comparator: "exact", value: "/en/home/" })));
pipeline.add(actions.parallel([
actions.showEmbeddedSurvey({ context: ".page-footer", surveyId: "survey-id" }),
actions.startRecording({ buffer: false })
]));
});
// custom action to execute custom logic, for example to extract information from external SDKs
rulesEngine.createPipeline(pipeline => {
let adobeVariant = null;
pipeline.add((next) => {
let adobeIntegration = window.Adobe.getIntegrationData();
adobeVariant = adobeIntegration.getActiveVariant() || null;
if (adobeVariant !== null) {
next();
}
});
pipeline.add(actions.setCustomProperty({
name: "adobe_variant",
value: () => adobeVariant,
analytics: true
}));
});
// trigger a survey invite with a delay of 5 seconds if a cpp matches a certain
rulesEngine.createPipeline(pipeline => {
pipeline.add(conditions.cpp({ name: "adobe_variant", comparator: "is", value: "variant_a" }));
pipeline.add(conditions.not(conditions.surveyShown({ surveyId: "survey-id" })));
pipeline.add(actions.delay(5));
pipeline.add(actions.showInvite());
pipeline.add(actions.showLayerSurvey({ surveyId: "survey-id" }));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment