A/B testing - how to in Craft? 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!
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 %}


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.