PHPackages                             lullabot/ab\_tests - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lullabot/ab\_tests

ActiveDrupal-module[Utility &amp; Helpers](/categories/utility)

lullabot/ab\_tests
==================

A/B Tests Drupal module

1.0.3(3mo ago)16.6k—8.3%[8 issues](https://github.com/Lullabot/ab_tests/issues)[1 PRs](https://github.com/Lullabot/ab_tests/pulls)proprietaryPHPCI passing

Since Feb 18Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/Lullabot/ab_tests)[ Packagist](https://packagist.org/packages/lullabot/ab_tests)[ RSS](/packages/lullabot-ab-tests/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (45)Used By (0)

A/B Tests
=========

[](#ab-tests)

A flexible and extensible Drupal module for running A/B tests on your content. This module empowers content teams to experiment with different content presentations while collecting valuable user interaction data through a sophisticated server-side rendering approach.

What Does It Do?
----------------

[](#what-does-it-do)

The A/B Tests module enables you to:

- **View Mode Testing**: Present entire entities using different Drupal view modes
- **Block Testing**: A/B test individual Layout Builder blocks with different configurations
- **Analytics Integration**: Collect and analyze user interaction data through pluggable analytics systems
- **Custom Decision Logic**: Implement sophisticated variant selection algorithms
- **Server-Side Rendering**: Eliminate flash-of-original-content issues common with client-side tools

Thanks to its pluggable architecture, you can easily extend the module to:

- Implement custom variant decision logic (timeout-based, user-based, feature flags, etc.)
- Integrate with any analytics platform (Google Analytics, Adobe Analytics, custom systems)
- Add new tracking mechanisms and custom event collection
- Support different content types and rendering approaches

Architecture
------------

[](#architecture)

The A/B Tests module uses a sophisticated pluggable architecture built around two core plugin types and two distinct testing approaches:

### Plugin System

[](#plugin-system)

#### Deciders

[](#deciders)

Deciders determine which variant to show to a user. They implement `AbVariantDeciderInterface` and can consider factors such as:

- User session data
- Time-based rules
- Random distribution
- Custom business logic
- Feature-specific requirements (view modes vs blocks)

**Base Classes:**

- `AbVariantDeciderPluginBase`: Extends Drupal's `PluginBase` with configuration and form interfaces
- `TimeoutAbDeciderBase`: Abstract base for timeout-based random selection

#### Analytics/Trackers

[](#analyticstrackers)

Analytics plugins manage the reporting and tracking of test results. They implement `AbAnalyticsInterface` and handle:

- Recording which variant was shown to users
- Tracking user interactions with variants
- Integration with external analytics platforms
- Custom event and metrics collection

**Base Classes:**

- `AbAnalyticsPluginBase`: Extends Drupal's `PluginBase` with UI and configuration support

### Testing Approaches

[](#testing-approaches)

#### View Mode Testing

[](#view-mode-testing)

Tests entire entity rendering using different Drupal view modes:

- **Scope**: Complete entity presentation (node, user, etc.)
- **Configuration**: Set up at content type level via third-party settings
- **Rendering**: Server-side with Ajax re-rendering for variants
- **Use Cases**: Testing different content layouts, field arrangements, or styling approaches

#### Block Testing (Layout Builder Integration)

[](#block-testing-layout-builder-integration)

Tests individual blocks within Layout Builder with different configurations:

- **Scope**: Individual block instances within layouts
- **Configuration**: Per-block via Layout Builder interface
- **Rendering**: Client-side Ajax with sophisticated context preservation
- **Use Cases**: Testing block settings, display options, or conditional visibility

### Sub-modules

[](#sub-modules)

The module includes several sub-modules that demonstrate and extend functionality:

#### ab\_blocks

[](#ab_blocks)

Enables A/B testing on Layout Builder blocks:

- Integrates with Layout Builder's component system
- Provides block-level configuration forms
- Handles context serialization for Ajax requests
- Location: `modules/ab_blocks/`

#### ab\_analytics\_tracker\_example

[](#ab_analytics_tracker_example)

Demonstrates custom analytics implementation:

- Provides `MockTracker` plugin example
- Shows analytics configuration patterns
- Includes JavaScript tracking component
- Location: `modules/ab_analytics_tracker_example/`

#### ab\_variant\_decider\_view\_mode\_timeout

[](#ab_variant_decider_view_mode_timeout)

Time-based view mode variant selection:

- Extends `TimeoutAbDeciderBase`
- Provides view mode selection interface
- Demonstrates feature-restricted plugins
- Location: `modules/ab_variant_decider_view_mode_timeout/`

#### ab\_variant\_decider\_block\_timeout

[](#ab_variant_decider_block_timeout)

Time-based block configuration variants:

- Similar to view mode timeout but for blocks
- Supports JSON-based block setting overrides
- Location: `modules/ab_variant_decider_block_timeout/`

### JavaScript Architecture

[](#javascript-architecture)

The module includes a sophisticated client-side component system:

- **BaseAction**: Common functionality for status tracking and error handling
- **BaseDecider/BaseTracker**: Abstract classes for plugin implementation
- **AbTestsManager**: Orchestrates deciders and trackers
- **DecisionHandlerFactory**: Creates appropriate handlers based on test type
- **ViewModeDecisionHandler/BlockDecisionHandler**: Feature-specific variant loading

Configuration
-------------

[](#configuration)

### Ignoring Configuration Export

[](#ignoring-configuration-export)

The module provides an option to ignore A/B test configurations during configuration export. This is useful when you want to:

- Keep A/B test configurations out of version control
- Have different A/B test settings per environment
- Prevent A/B tests from being deployed/overritten across environments

To enable this feature, navigate to /admin/config/search/ab-tests and check the box.

When enabled, any third-party settings from the A/B Tests module will be excluded during configuration export and import.

Usage
-----

[](#usage)

### Enabling View Mode Testing

[](#enabling-view-mode-testing)

To set up A/B testing for different view modes on a content type:

1. Navigate to **Structure → Content types → \[Your content type\] → Edit**
2. Scroll to the **A/B Tests** fieldset
3. Configure **Decider Plugin**:

- Select a decider (e.g., "Timeout (View Mode)")
- Configure plugin settings (timeout values, view modes to test)

4. Configure **Analytics Plugin** (optional):

- Select an analytics tracker
- Configure tracking settings

5. Save the content type configuration

View mode testing will now apply to all entities of this content type.

### Enabling Block Testing

[](#enabling-block-testing)

To set up A/B testing for Layout Builder blocks:

1. Edit a page using Layout Builder
2. Add or configure an existing block
3. Look for the **A/B Testing** contextual link on the block
4. Configure the test:

- Select a decider plugin (e.g., "Timeout (Block)")
- Configure variant settings (block configuration overrides)
- Set up analytics tracking

5. Save the layout

The block will now be A/B tested with the configured variants.

### Admin Settings

[](#admin-settings)

Global module settings are available at `/admin/config/search/ab-tests`:

- **Debug Mode**: Enable console logging for development
- **Configuration Export Control**: Exclude A/B test configs from export/import

Implementation Guide
--------------------

[](#implementation-guide)

### Creating a Custom Decider Plugin

[](#creating-a-custom-decider-plugin)

Decider plugins determine which variant to show users. They consist of both PHP and JavaScript components that work together to make decisions and handle variant loading.

#### 1. PHP Plugin Structure

[](#1-php-plugin-structure)

Create your decider plugin by extending `AbVariantDeciderPluginBase`:

```
