This tutorial will introduce you to the A/B testing plugin, which is basically just a simple function.
The idea for the plugin is to have a IP-based method to split visitors into two groups to display different content or slightly change the design of the site depending on the group. You'd then feed this into your analytics software and compare the numbers, conversion rates, etc.
The final plugin works like this in your templates and snippets:
Of course the same can be used to switch CSS files
In this case only visitors in group B would get to see the
I'm skipping the connection to Google Analytics and Co. because all analytics services have their own way of handling special tags and input.
Now let's build the plugin…
As you can see in the examples above, I've called the function
visitorgroup(). Let's create a folder called
visitorgroup in plugins with a php file called
That's it. Kirby will now automatically load the
index.php for us.
In the function we will have to do two things. First, decide in which group the visitor will be according to the ip and then returning either the group name or true/false, when you pass the group name you are looking for.
This is the full code for the
It looks fairly simple, but let me go through each step for you:
Kirby has a built-in method called
$kirby->visitor(), which returns an object that lets you get some useful information about the current visitor such as the ip, the preferred language or the user agent string.
The most straight-forward approach to split visitors into equal groups is to convert the IP to an integer and then determin if integer is odd or even. This can be done in two lines of code — or even just one if you write it a bit more compressed. But let's keep it readable.
In this step we use PHP's modulus operator:
$int % 2, which can be translated to remainder of
$int divided by 2. This will either return or
1, which we can use to return either
b. So at this point we end up with a
$group variable that contains the group name.
The next step is very easy. We could just return the group name now:
To make the function a bit more powerful though, we will also offer the option to pass an expected group name and get a boolean (
false) as the return value, so we can easily use this in if clauses.
In the final code for the function you can see that there's a parameter called
$which, which is by default
So let's first return the name of the group as long as
$which is still
This would not be very helpful in case we pass a group name for
$which. In that case we have to do a simple comparison of the group name and the name we got as argument.
And that's it! Our final plugin for A/B testing with Kirby.
I went through this with very detailed little steps and the more experienced coders among you will probably be bored by now, but let me take this chance to encourage every Kirby user — PHP beginner or pro — to try writing your own code first, before looking for a plugin.
I know there's this golden "Don't reinvent the wheel" rule, but most problems can be solved by just a little bit of code and along the way there's a good chance you might even learn something new. Take that chance. You will end up with much cleaner code and much more coding skills in the long run.