Custom Evaluator Not Working
I'm developing a custom evaluator to check weather a Product ID exists in a users Shopping Cart.
The JavaScript appears to work as expected when I manually put it in the browser console.
(function() {
var items = CartJS.cart.items;
var bool = false;
if(items.length > 0){
for (var i = 0; i < items.length; i++) {
if(items[i].id === 31117487472699 || items[i].id === 17979711617 || items[i].id === 32028112289851 || items[i].id === 16865395009){
bool = true;
}
}
if(bool === true){
return 'true';
}
}else{
return 'false';
}
})();
I used the DY Custom Evaluator code to double check that it's working, and it says it returns the value as expected:
DYO.Q(DYO.CoreUtils.safeEval(DYO.oevals[17044].code))
.then(function(result) {
console.log("Returned value from evaluator:", result);
});
However, the notification does not appear on the Preview.
Any help would be appreciated, thanks!
-
The reason that although the plain code (from the console) seems to run as expected while the targeting condition of your notification campaign does not work as you desire is that the evaluator's code runs without waiting for the "CartJS.cart.items" variable.
There are a few ways for how this issue can be resolved, but here are the common two approaches:- The simpler way that doesn't require any adjustments in the evaluator's code is to simply set a waiting condition to the notification campaign itself. This will delay the targeting conditions' evaluation until the condition you will specify is going to be triggered.
Specifically in your case, you can make the campaign wait for the "CartJS.cart.items" variable to be available:
The limitation of using this method is that no experiences under this campaign are going to be served if "CartJS.cart.items" is not going to get defined.
Otherwise, it is a perfectly good path to take.
-
The second approach is to account for the waiting of this variable directly in the evaluator's code itself (async code or a JS promise). You can do this by using a method like the following:
DYO.CoreUtils.waitForVariable('CartJS.cart.items', 50).then(function(){
// Write your callback function here!
})To learn more about the best practices in using evaluators feel free to refer to this Knowledge Base article.
- The simpler way that doesn't require any adjustments in the evaluator's code is to simply set a waiting condition to the notification campaign itself. This will delay the targeting conditions' evaluation until the condition you will specify is going to be triggered.
Please sign in to leave a comment.
Comments
2 comments