rl
This module is included in DXPR CMS.
The Reinforcement Learning (RL) module implements A/B testing in the most efficient and effective way possible, minizing lost conversions using machine learning.
Thompson Sampling is a learning-while-doing method. Each time a visitor lands on your site the algorithm "rolls the dice" based on what it has learned so far. Variants that have performed well roll larger numbers, so they are shown more often, while weak copies still get a small chance to prove themselves. This simple trick means the system can discover winners very quickly without stopping normal traffic.
Traditional A/B tests run for a fixed horizon—say two weeks—during which half your visitors keep seeing the weaker version. Thompson Sampling avoids this waste. As soon as the algorithm has even a little evidence it quietly shifts most traffic to the better variant, saving conversions and shortening the wait for useful insights.
For full details of what goes on behind the curtains check the source code:
ThompsonCalculator.php.
RL Modules
- AI Sorting - Intelligent content ordering/switching for Drupal Views
Features
- Thompson Sampling Algorithm - Pure PHP implementation
- Fast HTTP REST API - Optimized JSON endpoints for tracking and decisions
- Administrative Reports - Experiment analysis interface
- Service-based Architecture - Extensible design
- Data Sovereignty, Privacy First - No cloud, just Drupal
You need RL if
- A/B Testing - Test content variations
- Content Optimization - Track content engagement
- Feature Selection - Choose features to show users
- Recommendations - Optimize content recommendations
- Resource Allocation - Distribute resources across options
Thompson Sampling
Unlike traditional A/B testing, Thompson Sampling:
- Adapts automatically - Shifts traffic to better options
- Handles multiple options - Works with 2+ variations, even thousands of arms are handled well
- Continuous learning - No fixed test duration
- Bayesian approach - Incorporates uncertainty
Prefer a turnkey demo site?
Spin up DXPR CMS—Drupal pre-configured with DXPR Builder, DXPR Theme, RL (Reinforcement Learning) module, and security best practices.
Installation
composer require drupal/rl drush en rl
Verify rl.php Access
The RL module includes a .htaccess file that allows direct access to rl.php (following the same pattern as Drupal 11's contrib statistics module). Test that it's working:
curl -X POST -d "action=turns&experiment_id=test&arm_ids=1" http://example.com/modules/contrib/rl/rl.phpIf the test fails:
- Apache: Ensure
.htaccessfiles are processed (AllowOverride All) - Nginx: Copy the rewrite rules from
.htaccessto your server config - Security modules: Whitelist
/modules/contrib/rl/rl.php
If server policies prevent direct access to rl.php, use the Drupal Routes API instead.
API
// Get the experiment manager $experiment_manager = \Drupal::service('rl.experiment_manager'); // Record a trial (content shown) $experiment_manager->recordTurn('my-experiment', 'variant-a'); // Record a success (user clicked) $experiment_manager->recordReward('my-experiment', 'variant-a'); // Get Thompson Sampling scores $scores = $experiment_manager->getThompsonScores('my-experiment'); // Select the best option $ts_calculator = \Drupal::service('rl.ts_calculator'); $best_option = $ts_calculator->selectBestArm($scores); // Override page cache for web components (optional) $cache_manager = \Drupal::service('rl.cache_manager'); $cache_manager->overridePageCacheIfShorter(60); // 60 seconds
HTTP Endpoints
rl.php - Direct Endpoint (Recommended)
Use the direct rl.php endpoint for optimal performance:
// Record turns (trials) - when content is viewed const formData = new FormData(); formData.append('action', 'turns'); formData.append('experiment_uuid', 'abc123'); formData.append('arm_ids', '1,2,3'); navigator.sendBeacon('/modules/contrib/rl/rl.php', formData); // Record reward (success) - when user clicks/converts const rewardData = new FormData(); rewardData.append('action', 'rewards'); rewardData.append('experiment_uuid', 'abc123'); rewardData.append('arm_id', '1'); navigator.sendBeacon('/modules/contrib/rl/rl.php', rewardData);
Drupal Routes - Alternative API
Use only when server security policies prevent direct access to rl.php:
POST /rl/experiment/{uuid}/turns- Record trialsPOST /rl/experiment/{uuid}/rewards- Record successesGET /rl/experiment/{uuid}/scores- Get scores
Cache Management
RL provides optional cache management for web components:
// Override page cache if experiment cache is shorter than site cache \Drupal::service('rl.cache_manager')->overridePageCacheIfShorter(30);
How it works:
- If site cache is 300s and experiment needs 30s → overrides to 30s
- If site cache is 60s and experiment needs 300s → leaves at 60s
- If site cache is disabled → no override
Use cases:
- Views plugins using RL for content sorting
- Blocks displaying A/B tested content
- Components needing frequent RL score updates
Ready to get started? Install the module and begin implementing intelligent, adaptive decision-making in your Drupal applications today!