Resource Center

Dynamic Tracking - Click ID Capture and Storage: PHP

 
<?php
class PJNClicks
{
    /*
     * Destination URLs have the format "http://example.com/pagename.php?clickId=<CLICK_ID>""
    */
    private $clickIdUrlParameter = "clickId";
    private $clickIdCookieName = "pjn_clicks";
    private $lookbackPeriodInDays = 60;
    private $maxCookieAge = 60 * (60 * 24 * 60);

    /**
     * Return the data of the cookie storing the click IDs,
     * using the cookie name defined in clickIdCookieName
     */
    function getCookieData()
    {
        if (array_key_exists($this->clickIdCookieName, $_COOKIE)) {
            $decodedData = str_replace("|",",",str_replace("_"," ",$_COOKIE[$this->clickIdCookieName]));
            return json_decode($decodedData, true);
        }
        else {
            return array();
        }
    }

    /**
     * Check for the tracking cookie, and then remove
     * click IDs older than the lookback period defined in lookbackPeriodInDays
     */
    function checkLookbackPeriod()
    {
        $cookieData = $this->getCookieData();
        $clickData = [];
        if (count($cookieData) > 0) {
            for ($i = 0; $i < count($cookieData); $i++) {
                $cookieDate = DateTime::createFromFormat('Ymd', $cookieData[$i]['date']);
                $today = new DateTime();
                $daysDiff = date_diff($cookieDate, $today, true)->format('%a');
                if ($daysDiff <= $this->lookbackPeriodInDays) {
                    array_push($clickData, $cookieData[$i]);
                }
            }
            // replace certain characters that php refuses to allow to be used in setrawcookie()
            $encodedData = str_replace(",","|",str_replace(" ","_",json_encode($clickData)));
            setrawcookie(
                $this->clickIdCookieName,
                $encodedData,
                time()+$this->maxCookieAge,
                "/"
            );
        }
    }

    /**
     * If a click ID is found in the query string,
     * create a cookie and store both the click ID and
     * the date on which it was created
     */
    function captureClickId()
    {
        $clickId = filter_input(INPUT_GET, $this->clickIdUrlParameter);
        if ($clickId) {
            $now = date('Ymd');
            $duplicate = false;
            $cookieData = $this->getCookieData();
            for ($i = 0; $i < count($cookieData); $i++) {
                if ($clickId === $cookieData[$i]['id']) {
                    $duplicate = true;
                    break;
                }
            }
            if (!$duplicate) {
                $clickArray = array('id' => $clickId, 'date' => $now);
                $cookieData[$i] = $clickArray;

                // replace certain characters that php refuses to allow to be used in setrawcookie()
                $encodedData = str_replace(",","|",str_replace(" ","_",json_encode($cookieData)));
                setrawcookie(
                    $this->clickIdCookieName,
                    $encodedData,
                    time()+$this->maxCookieAge,
                    "/"
                );
            }
        }
    }
}

if (headers_sent())
{
    echo("cookies must be sent *before* any output from your script");
    exit();
}

$click = new PJNClicks();
$click->checkLookbackPeriod();
$click->captureClickId();
?>
This is an example of how to use the code library above by including it on every page on your website. Here it is on your home page:
<?php
/* Pepperjam Click storage script
*  This script should be included on every page on your website
*/
include("pjnclicks.php");
/*
*  Your content goes below
*/
?>

And this is it on another page on your website:
 

<?php
/* Pepperjam Click storage script
*  This script should be included on every page on your website
*/
include("pjnclicks.php");
/*
*  Your content goes below
*/
?>


Here is an example of how to use this code on your website's Shopping Cart Confirmation or Thank You page. This code will create the Pepperjam conversion pixel. It is important to test the pixel on your confirmation page to make sure that everything is working properly.
 

<?php
// Tracking pixel script in php, with explanations.
include("pjnclicks.php");

// Pixel specific data
$integration = 'DYNAMIC';
$program_id  = 123;
$new_to_file = 0; // existing customer
$coupons     = '';
$pixel_html  = '';
$click_html  = '';
$order_id    = 123456;
$click_ids   = array();
$order_items = array();
$order_items[] = array(
    'id'       => 'SKU-922320',
    'quantity' => 2,
    'price'    => 25000.99
);
$order_items[] = array(
    'id'       => 'SKU-9233',
    'quantity' => 1,
    'price'    => 189.99
);

$click = new PJNClicks();
$clickData = $click->getCookieData();

/*
 * Now, we create and output the iframe.
 */
$x = 1;
foreach ($order_items as $order_item) {
    $pixel_html .=
        '&' . 'ITEM_ID'    . $x . '=' . $order_item['id'] .
        '&' . 'ITEM_PRICE' . $x . '=' . $order_item['price'] .
        '&' . 'QUANTITY'   . $x . '=' . $order_item['quantity'];
    $x++;
}

foreach ($clickData as $click) {
    $click_ids[] = $click['id'];
}


$pixel_html =
    '<iframe src="https://t.pepperjamnetwork.com/track?'; .
    'INT='          . $integration .
    '&PROGRAM_ID='  . $program_id .
    '&ORDER_ID='    . $order_id .
    '&CLICK_ID='    . implode($click_ids, ',') .
    '&COUPON='      . $coupons .
    '&NEW_TO_FILE=' . $new_to_file .
    $pixel_html .
    '" width="1" height="1" frameborder="0"></iframe>';

echo $pixel_html;
?>