Skip to content

Sticky rotation with PHP nodes

You can split visitors across several offers, or other nodes, and still send a returning visitor down the same route each time.

A PHP node and a cookie do that.

<?php
$cookie = 'sticky-rotator-X';
$number_of_routes = 3;
if (isset($_COOKIE[$cookie]) && ctype_digit($_COOKIE[$cookie])
&& $_COOKIE[$cookie] >= 1 && $_COOKIE[$cookie] <= $number_of_routes) {
return (int) $_COOKIE[$cookie];
}
$route = mt_rand(1, $number_of_routes);
setcookie($cookie, (string) $route, [
'expires' => time() + 30 * 24 * 60 * 60, // 30 days
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Lax',
]);
return $route;
?>

This sample expects three connections out of the PHP node: On Done 1, 2 and 3. Change $number_of_routes to match the number of connections you have. Use a different cookie name for each sticky node.

The cookie is set on your tracking domain, so it needs HTTPS for the secure flag. A stored route outside the current range, for example after you remove a connection, is ignored and the visitor gets a new route.

You can chain sticky nodes. For example, one PHP node splits traffic 50/50. On one of those routes, a second node splits 50/50 again between two offers. The end offers then see a 50 / 25 / 25 split.

For an uneven split in one node, map a random number to routes. Replace the $route = mt_rand(1, $number_of_routes); line with this, which sends 20% of new visitors to On Done 1, 30% to On Done 2 and 50% to On Done 3:

$roll = mt_rand(1, 100);
$route = $roll <= 20 ? 1 : ($roll <= 50 ? 2 : 3);

You can also raise $number_of_routes and point several On Done numbers at the same node. For example, with $number_of_routes = 10, connect On Done 1-2 to one node, 3-5 to a second and 6-10 to a third for a 20 / 30 / 50 split. Keep $number_of_routes equal to the number of outgoing connections. A route with no connection sends the visitor down the lowest On Done number instead.