rules-engine
v0.3.0
Published
A node.js module to check if an event array matches some specifications. Usefull when you want to compare many rules with many events and check if the final output is true/false.
Downloads
89
Readme
node-rules-engine
A node.js module to check if an event array matches some specifications. Usefull when you want to compare many rules with many events and check if the final output is true/false.
Dial Once uses this module to easily check if a user match a goal/funnel using our interfaces. See exemple below for a more explanatory exemple.
installation
npm install rules-engine
var engine = require('rules-engine');
engine.apply(events, rules).then(function(result){
console.log(result); //boolean, true if events are matching rules, false otherwise
});
example
We want here to check if user viewed page 1, page 3, and then made a click (basic conversion test). ('page' and 'click' here are arbitrary and you can use whatever you want)
var engine = require('rules-engine');
var events = [
{'page': {val: 1}},
{'page': {val: 3}},
{'click': {val: true}}
];
var rules = [
{'page': {val: 1, should: true}},
{'page': {val: 3, should: true}},
{'click': {val: true, should: true}}
];
engine.apply(events, rules).then(function(result){
console.log(result); //true!
});
Then things can be a bit more complicated, if we want to check if user viewed page 1, 3 but did not make any click:
var rules = [
{'page': {val: 1, should: true}},
{'page': {val: 3, should: true}},
{'click': {val: true, should: false}}
];
engine.apply(events, rules).then(function(result){
console.log(result); //false!
});
Some more cases can be handled, like 'user viewed either page 1, 2, or 3 and made a click:
var rules = [
{'page': {val: [1, 2, 3], should: true}},
{'click': {val: true, should: true}}
];
User viewed any page:
var rules = [
{'page': {val: '*', should: true}}
];
or
var rules = [
{'page': {val: /.+/, should: true}}
];
User viewed any page BUT page 3:
var rules = [
{'page': {val: '*', should: true}},
{'page': {val: 3, should: false}}
];
complex rules exemple
The following rules can be used in val
to compute complex rules:
{$gt: 0} //true if event val is greater than the value
{$lt: 0} //true if event val is lesser than the value
{$gte: 0} //greater than or equal
{$lte: 0} //lesser than or equal
Value can be a Date
, number, string, etc. Anything natively comparable in JS
User should have made more than 10 clicks:
var events = [
{'clicks': {val: 12}}
];
var rules = [
{'clicks': {val: {$gt: 10}, should: true}}
];
OR conditions and optional rules
You can specify some rules as optional, thus if they don't match, other rules will have priority.
User should do more than 10 clicks OR less than 3 pageviews:
var events = [
{'clicks': {val: 12}}
];
var rules = [
{'clicks': {val: {$gt: 10}, should: true, optional: true}},
{'views': {val: {$lt: 3}, should: true, optional: true}}
];
If none are matched, it will be rejected. If one of the two matches, its a true!
You can then make some complex scenario, like: User should do more than 10 clicks, AND (views less than 3 pages OR make 1 comment)
var events = [
{'clicks': {val: 12}}
];
var rules = [
{'clicks': {val: {$gt: 10}, should: true}},
{'views': {val: {$lt: 3}, should: true, optional: true }},
{'comments': {val: 1, should: true, optional: true }}
];