A/B testing SDK for the Shopify Liquid templating language. This SDK brings the power of ABsmartly's experimentation platform to server-side rendered templates.
The ABsmartly Liquid SDK is compatible with:
- Ruby: Version 3.0 and later
- Liquid: Version 5.0 and later
- Shopify Themes: All modern Shopify themes
Jekyll 4 depends on Liquid 4, so it is not compatible with this Liquid 5 gem.
Note on Naming: This SDK uses the module name ABsmartly::Liquid (with namespace) to avoid clashing with the Ruby SDK's main module (Absmartly). When requiring the gem, use require 'absmartly/liquid'.
Add to your Gemfile:
gem 'absmartly-liquid-sdk'Then run:
bundle installPlease follow the installation instructions before trying the following code.
This example assumes an API Key, an Application, and an Environment have been created in the ABsmartly web console.
require 'absmartly/liquid'
sdk = ABSmartly::SDK.new(
endpoint: 'https://your-company.absmartly.io/v1',
api_key: ENV['ABSMARTLY_API_KEY'],
application: 'my-shopify-store',
environment: 'production'
)sdk = ABSmartly::SDK.new(
endpoint: 'https://your-company.absmartly.io/v1',
api_key: ENV['ABSMARTLY_API_KEY'],
application: 'my-shopify-store',
environment: 'production',
retries: 3,
timeout: 5000
)SDK Options
| Option | Type | Required? | Default | Description |
|---|---|---|---|---|
| endpoint | String |
✅ | nil |
The URL to your API endpoint. Most commonly "https://your-company.absmartly.io/v1" |
| api_key | String |
✅ | nil |
Your API key which can be found on the Web Console. |
| environment | String |
✅ | nil |
The environment of the platform where the SDK is installed. Environments are created on the Web Console. |
| application | String |
✅ | nil |
The name of the application where the SDK is installed. Applications are created on the Web Console. |
| retries | Integer |
❌ | 5 |
Maximum number of HTTP retries for failed requests |
| timeout | Integer |
❌ | 3000 |
HTTP timeout in milliseconds |
| event_logger | Proc |
❌ | nil |
Custom event logger callback (see Advanced section) |
context = sdk.create_context(
units: {
session_id: session[:id],
customer_id: current_customer&.id
}
)
context.ready
drop = ABSmartly::Liquid::Drop.new(context)For better performance, pre-fetch context data and reuse it to avoid an additional round-trip.
data = sdk.get_client.get_context(
units: { session_id: session[:id] }
)
context = sdk.create_context_with(
{ units: { session_id: session[:id] } },
data
)
drop = ABSmartly::Liquid::Drop.new(context)For long-running contexts, the context is usually created once when the application is first started. However, any experiments started after the context was created will not be triggered.
context = sdk.create_context(
units: { session_id: session[:id] },
refresh_period: 4 * 60 * 60 * 1000
)
# Or refresh manually
context.refreshYou can add additional units to a context. This may be used, for example, when a user logs in to your application.
Note: You cannot override an already set unit type as that would be a change of identity. In this case, you must create a new context instead.
context.set_unit('db_user_id', '1000013')Use the absmartly_treatment filter to select a treatment variant:
{% assign variant = 'exp_button_color' | absmartly_treatment %}
{% if variant == 0 %}
<button class="btn-blue">Buy Now</button>
{% elsif variant == 1 %}
<button class="btn-red">Buy Now</button>
{% endif %}Use the block tag for cleaner syntax with a local variant variable:
{% absmartly_treatment 'exp_button_color' %}
{% if variant == 0 %}
<button class="btn-blue">Buy Now</button>
{% elsif variant == 1 %}
<button class="btn-red">Buy Now</button>
{% endif %}
{% endabsmartly_treatment %}Variables allow you to configure experiment variations without code changes:
{% assign button_text = 'checkout_button_text' | absmartly_variable: 'Checkout' %}
{% assign button_color = 'checkout_button_color' | absmartly_variable: 'blue' %}
<a href="/checkout" class="btn btn-{{ button_color }}">
{{ button_text }}
</a>Although generally not recommended, it is sometimes necessary to peek at a treatment without triggering an exposure.
{% assign variant = 'exp_feature' | absmartly_peek %}{% assign color = 'button_color' | absmartly_peek_variable: 'blue' %}During development, force specific variants:
context.override('exp_test', 1)
context.overrides({
'exp_test' => 1,
'exp_another' => 0
})Track goal achievement with properties:
<!-- Using filter -->
{{ 'purchase' | absmartly_track: amount: order.total_price, items: order.line_items.size }}
<!-- Using tag -->
{% absmartly_track 'add_to_cart', product_id: product.id, price: product.price %}Add metadata for audience targeting:
context.attributes({
user_agent: request.user_agent,
customer_age: 'new_customer',
account_type: 'premium'
})Ensure all events are published before proceeding:
context.publishThe close method will ensure all events have been published to the ABsmartly collector, like publish, and will also "seal" the context, preventing any further events from being tracked.
context.closeMonitor SDK events for debugging, analytics, or integrating with other systems.
event_logger = ->(context, event_name, data) {
case event_name
when 'exposure'
Rails.logger.info "ABsmartly exposure: #{data[:name]}"
when 'goal'
Rails.logger.info "ABsmartly goal: #{data[:name]}"
when 'error'
Rails.logger.error "ABsmartly error: #{data}"
when 'ready'
Rails.logger.info "ABsmartly context ready"
when 'refresh'
Rails.logger.info "ABsmartly context refreshed"
when 'publish'
Rails.logger.info "ABsmartly events published"
when 'close'
Rails.logger.info "ABsmartly context closed"
end
}
sdk = ABSmartly::SDK.new(
endpoint: ENV['ABSMARTLY_ENDPOINT'],
api_key: ENV['ABSMARTLY_API_KEY'],
application: 'my-store',
environment: Rails.env,
event_logger: event_logger
)Event Types
| Event | When | Data |
|---|---|---|
ready |
Context turns ready | Context initialization data |
refresh |
refresh() succeeds |
Refreshed context data |
publish |
publish() succeeds |
Published events |
exposure |
treatment() succeeds on first exposure |
Exposure data |
goal |
track() succeeds |
Goal data |
close |
close() succeeds the first time |
nil |
error |
Error occurs | Error object |
| Filter | Description | Returns |
|---|---|---|
absmartly_treatment |
Get treatment variant and track exposure | Integer variant number (0, 1, ...) |
absmartly_peek |
Get treatment variant without tracking exposure | Integer variant number |
absmartly_variable |
Get variable value and track exposure | Variable value or default |
absmartly_peek_variable |
Get variable value without tracking exposure | Variable value or default |
absmartly_custom_field |
Get custom field value for an experiment | Parsed field value |
absmartly_track |
Track goal achievement with properties | Empty string |
| Tag | Description |
|---|---|
{% absmartly_treatment %} |
Block tag for treatment with local variant variable |
{% absmartly_track %} |
Tag for tracking goals with properties |
The absmartly drop object is available in templates when injected via ABSmartly::Liquid::Drop.new(context):
{% if absmartly.ready %}
<!-- Context is ready -->
{% endif %}
{% for exp in absmartly.experiments %}
<li>{{ exp }}</li>
{% endfor %}class ApplicationController < ActionController::Base
before_action :init_absmartly
after_action :close_absmartly
private
def init_absmartly
$absmartly_sdk ||= ABSmartly::SDK.new(
endpoint: ENV['ABSMARTLY_ENDPOINT'],
api_key: ENV['ABSMARTLY_API_KEY'],
application: 'my-shopify-store',
environment: Rails.env
)
@context = $absmartly_sdk.create_context(
units: {
session_id: session[:id],
customer_id: current_customer&.id
}
)
@context.ready
@context.attributes({
user_agent: request.user_agent,
customer_logged_in: current_customer.present?
})
@absmartly_drop = ABSmartly::Liquid::Drop.new(@context)
rescue => e
Rails.logger.error "ABsmartly initialization failed: #{e.message}"
@absmartly_drop = nil
end
def close_absmartly
@context&.close
end
endIn your template:
{% if absmartly.ready %}
{% assign header_color = 'header_color' | absmartly_variable: 'blue' %}
<header style="background: {{ header_color }}">
{% render 'header' %}
</header>
{% else %}
<header style="background: blue">
{% render 'header' %}
</header>
{% endif %}A/B Smartly is the leading provider of state-of-the-art, on-premises, full-stack experimentation platforms for engineering and product teams that want to confidently deploy features as fast as they can develop them. A/B Smartly's real-time analytics helps engineering and product teams ensure that new features will improve the customer experience without breaking or degrading performance and/or business metrics.