What is the Rules Engine?
The Rules Engine is a high-performance targeting system that enables real-time content personalization and campaign management through sophisticated rule evaluation. It uses WebSocket technology to provide ultra-fast context evaluation (77x faster than REST) and delivers personalized content based on complex rule sets.
Core Concepts
Context Objects
A context object represents the current state that the Rules Engine will evaluate against. It contains three main sections that rules can target:
-
Moment: Represents the current point in time for rules evaluation
start
: Current timestamp in millisecondsduration
: Time window for rule validity- Example use case: Rules that trigger during specific scenes in a video or time periods in a live event
-
Environment: Contains situational data for rule evaluation
- Device information (mobile/desktop, browser type)
- User demographics and location
- Weather conditions
- Time of day/date
- Media information (what they're watching/listening to)
- Example use case: Rules targeting mobile users in cold weather with hot beverage ads
-
Metadata: Additional rule evaluation criteria
- Keywords detected in the content
- IAB categories present
- Example use case: Rules triggering on sports-related content when sports keywords are detected
Rules & Strategies
The Rules Engine uses strategies as containers for business rules. Each strategy contains:
-
Conditions: Rule definitions that determine content targeting
- Can be simple matches or complex logical operations
- Uses JSON Logic for powerful rule creation
- Example:
{
"type": "json-logic",
"id": "weather-rule",
"rule": {
"and": [
{"==": [{"var": "environment.weather.condition"}, "raining"]},
{">": [{"var": "environment.weather.temperature"}, 70]}
]
}
}
-
Effects: Actions triggered when rules match
- Content to display
- Timing information
- Presentation details
- Example:
{
"type": "content",
"id": "rainy-day-ad",
"on": "weather-rule",
"content": [{
"type": "activation",
"id": "umbrella-promotion",
"instance": {
"text": "Stay dry with our premium umbrellas!",
"start": 1000,
"duration": 10000
}
}]
}
Implementation Guide
Connection Methods
WebSocket (Recommended)
The Rules Engine provides a WebSocket interface for real-time rule evaluation:
const ws = new WebSocket('wss://your-rules-engine-url');
ws.onopen = () => {
console.log('Connected to Rules Engine');
// Send context for evaluation
const context = {
moment: {
start: Date.now(),
duration: 10000
},
environment: {
device: {
type: 'mobile',
os: 'ios',
browser: 'safari'
},
// ... other environment data
},
metadata: {
keywords: [/* ... */],
categories: {
iab: [/* ... */]
}
}
};
ws.send(JSON.stringify(context));
};
ws.onmessage = (event) => {
const result = JSON.parse(event.data);
// Handle matching rules and their effects
result.effects.forEach(effect => {
handleEffect(effect);
});
};
HTTP (Alternative)
For systems that can't use WebSockets, an HTTP interface is available:
async function evaluateRules(context) {
const response = await fetch('https://your-rules-engine-url', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(context)
});
return response.json();
}
Rule Creation Best Practices
-
Granular Rules
- Create specific, focused rules for better maintainability
- Combine rules using JSON Logic for complex scenarios
- Example: Separate weather and time-of-day rules that can be combined
-
Performance Optimization
- Order conditions from most to least likely to fail
- Use appropriate time windows in moment objects
- Cache context objects when possible
-
Testing Rules
- Use the Rules Engine playground for testing
- Create test contexts covering edge cases
- Verify rule combinations work as expected
Advanced Features
Rule Chaining
Rules can trigger other rules using the on
field:
{
"type": "json-logic",
"id": "primary-rule",
"rule": {
"==": [{"var": "environment.weather.condition"}, "sunny"]
}
}
{
"type": "json-logic",
"id": "secondary-rule",
"on": "primary-rule",
"rule": {
">": [{"var": "environment.temperature"}, 80]
}
}
Multi-Condition Rules
Combine multiple conditions for sophisticated targeting:
{
"type": "json-logic",
"id": "complex-rule",
"rule": {
"and": [
{
"or": [
{"==": [{"var": "environment.device.type"}, "mobile"]},
{"==": [{"var": "environment.device.type"}, "tablet"]}
]
},
{
">=": [{"var": "environment.user.age"}, 18]
},
{
"in": [
{"var": "environment.time.dayName"},
["saturday", "sunday"]
]
}
]
}
}
Real-time Updates
The Rules Engine supports live rule updates without service restart:
- Rules are synchronized in real-time
- Changes take effect immediately
- No service interruption required
Monetization Features
Value Attribution
Each rule can include monetization parameters:
{
"type": "content",
"id": "premium-ad",
"strategy": {
"id": "summer-campaign",
"value": {
"impression": 0.0015,
"engagement": 0.01,
"action": 0.05
}
}
}
Performance Tracking
Track rule performance with built-in metrics:
- Impression rates
- Engagement rates
- Conversion rates
- Rule triggering frequency
Administration
Rule Management
Access the admin interface at http://your-rules-engine-url/admin
to:
- Create and edit rules
- Monitor rule performance
- View active connections
- Send test contexts
Monitoring
Monitor system health and performance:
- WebSocket connection status
- Rule evaluation timing
- Error rates
- System uptime
Security Considerations
-
Authentication
- Use secure WebSocket connections (WSS)
- Implement proper API key management
- Follow least privilege principles
-
Data Privacy
- Only send necessary context data
- Follow data protection regulations
- Implement data retention policies
-
Rate Limiting
- Monitor connection frequency
- Implement appropriate timeouts
- Handle backpressure appropriately
Support and Troubleshooting
Common Issues
-
Connection Problems
- Verify WebSocket URL
- Check network connectivity
- Confirm SSL certificate validity
-
Rule Evaluation Issues
- Validate context object format
- Check rule syntax
- Review rule logic
-
Performance Problems
- Monitor context object size
- Review rule complexity
- Check network latency
Getting Help
- Review this documentation
- Check example implementations
- Contact support with specific issues