14

What's your process for A/B testing a Craft entry? I'm curious about how you'd structure alternative content for a page within Craft and what tools you'd use to measure the results.

Any comments appreciated - I know there are lots of different ways you could approach this!

2 Answers 2

14

You can set a cookie from your template, which assigns visitors to specific groups:


Craft 2: (Use the LJ Cookies plugin)

{% set testGroup = craft.lj_cookies.get('group') %}

{% if not testGroup %}
    {# New user - assign them to a group at random #}
    {% set testGroup = random(['a', 'b']) %}
    {% set expires = now|date_modify('+30 days').timestamp %}
    {% do craft.lj_cookies.set('group', testGroup, expires) %}
{% endif %}

Craft 3:

{% set testGroup = craft.app.request.cookies.get('group') %}

{% if not testGroup %}
    {# New user - assign them to a group at random #}
    {% set testGroup = random(['a', 'b']) %}
    {% set expires = now|date_modify('+30 days').timestamp %}
    {% do craft.app.response.cookies.add(create({
         class: 'yii\\web\\Cookie',
         name: 'group',
         value: testGroup,
         httpOnly: true
    })) %} 
{% endif %}

And then reference that testGroup variable whenever you need to know which group they’re in:

{% switch testGroup %}
    {% case 'a' %}
        ....
    {% case 'b' %}
        ....
{% endswitch %}

The nice thing about this approach is that you can actually have multiple sets of test groups at the same time, using different cookie/variable names. You’re also not limited to two groups per set.

3
  • Awesome, thanks Brandon.
    – Leah
    Oct 26, 2015 at 12:40
  • Thanks, is this still the best approach for AB testing 3 years later? Apr 13, 2022 at 12:35
  • @mateostabio I would saying cookies is still a viable option. Without any account based system in place it's really a easy and low maintenance way of doing A/B testing Jul 19, 2022 at 17:30
1

https://plugins.craftcms.com/optimum?craft4

https://github.com/matfish2/craft-optimum

The Optimum plugin allows for server-side A/B/n testing in Craft. You can set multiple experiments on the same page, each with unlimited variants. The plugin automatically sends the data to a GA4 custom dimension.

Full disclosure: I'm the author.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.