Offer Rotation Waterfall using a PHP Node
Use this when you have several offers and want visits to fill them in order:
- All visits go to the node on On Done 1 until its click limit is hit, for example 1000 clicks. This counts visits through the PHP node, not conversions.
- After that, visits go to the node on On Done 2, then On Done 3, and so on.
- When every limit is reached, visits go to the node on On Done 64, the default route.
That order is useful when you want data on new offers in a set sequence, or when offers have lifetime caps.
The counters do not reset on a schedule.
A funnel using this looks like this:

Set up the routes
Section titled “Set up the routes”Right-click each connection leaving the PHP node and set its On Done #. Number the capped routes from 1 upwards, in the order they should fill. Set the default connection to On Done 64. The code stops with an error message if there is no On Done 64 connection.
Each capped route needs a custom token with its click limit. Open the funnel settings, expand Advanced settings and add one line per route under Custom tokens:
fulfillment_clicks_route_1=300fulfillment_clicks_route_2=500fulfillment_clicks_route_3=100
With these tokens:
- The node on On Done 1 receives 300 visits.
- The node on On Done 2 receives the next 500.
- The node on On Done 3 receives the next 100.
- All later visits go to the node on On Done 64.
If a route has no matching token, the code stops with an error message naming the missing token.
Each counter belongs to the target node. For an external URL node it also depends on the URL, and for a lander or offer node on the node name. If you change the URL of a connected external URL node, rename a connected lander or offer node, or connect the route to a different node, that route’s counter starts again from zero.
The code
Section titled “The code”Paste this code into the PHP node:
<?php
return getFulfillmentRoute();
function getFulfillmentRoute() { // Get funnel tokens to ensure fulfillment route settings are added by user $funnel = DBTableCampaignFunnels::loadFunnel('{funnel-id}', false); if( !$funnel ) { throw new \Exception('funnel {funnel-id} cannot be loaded...'); } $funnelTokens = $funnel->getCustomTokens(true);
// Get all connected routes and max clicks assigned to each $nodeConnections = DBTableCampaignFunnelConnections::getNodeExitConnections('{node-id}');
$cache = new FulfillmentCache();
$defaultRoute = 64; $routeToConnections = []; foreach($nodeConnections as $conn) { $route = $conn->getParam1Int(); $routeToConnections[$route] = $conn; } ksort($routeToConnections); if( !isset($routeToConnections[$defaultRoute]) ) { fulfillmentError("onDone 64 must be set for backup URL"); }
foreach($routeToConnections as $route => $conn) { if( $route == $defaultRoute ) { return $route; }
$tokenName = "fulfillment_clicks_route_{$route}"; $maxClicks = array_key_exists($tokenName, $funnelTokens) ? $funnelTokens[$tokenName] : null; if( $maxClicks === null || !is_numeric($maxClicks) ) { fulfillmentError($tokenName); }
$node = DBTableCampaignFunnelNodes::loadNode($conn->getTargetNodeId()); if( !$node ) { throw new \Exception("node {$conn->getTargetNodeId()} cannot be loaded..."); } $targetID = $node->getNodeId(); switch($node->getType()) { case FunnelNodeTypes::EXTERNAL_URL: $targetID .= md5($node->getParams()['pageUrl']); break;
case FunnelNodeTypes::FLUX_LANDER: case FunnelNodeTypes::FLUX_OFFER: $targetID .= md5($node->getName()); break; } $cacheKey = "{node-id}-{$targetID}"; $clicks = $cache->get($cacheKey); if( $clicks === Cache::NOT_FOUND ) { $clicks = 0; } $found = false; if( $clicks < $maxClicks ) { $clicks++; $found = true; } $cache->set($cacheKey, $clicks);
if( $found ) { return $route; } }}
function fulfillmentError($error) { echo "Fulfillment PHP code error: {$error}"; exit;}
class FulfillmentCache{ public function set($key, $value) { file_put_contents($this->getFilePath($key), $value, LOCK_EX); }
public function get($key) { $path = $this->getFilePath($key); if( !file_exists($path) ) { return Cache::NOT_FOUND; } return file_get_contents_lck($path); }
private function getFilePath($key) { return DBTableApplication::getCacheFolderPath() . "fulfillment-php-code-" . md5($key); }}
