Resource Center

Basic Tracking - Click ID Capture and Storage: PHP

The following code is an example of how to implement a PHP solution for Basic Tracking - Click ID Capture and Storage from Ascend links and storing them for later use in the conversion pixel.  This solution assumes that the parameter passing click IDs to your site is the default value of clickId. This will need to be added to all pages of your site to ensure that they are being captured properly. Before making any changes to test for successful pixel integration, it is important to create a backup for any page or pages that will be modified.

 

<?php
class PJNClick
{
    /*
     * Destination URLs have the format "http://example.com/pagename.php?clickId=<CLICK_ID>""
    */
    private $clickIdUrlParameter = "clickId";
    private $clickIdCookieName = "pjn_click";
    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)) {
            return $_COOKIE[$this->clickIdCookieName];
        }
        else {
            return array();
        }
    }

    /**
     * If a click ID is found in the query string,
     * create a cookie and store the click ID
     */
    function captureClickId()
    {
        $clickId = filter_input(INPUT_GET, $this->clickIdUrlParameter);
        if ($clickId) {
            $duplicate = false;
            $cookieData = $this->getCookieData();
            if ($clickId === $cookieData) {
                $duplicate = true;
            }
            if (!$duplicate) {
                $cookieData = $clickId;
                setrawcookie(
                    $this->clickIdCookieName,
                    $cookieData,
                    time()+$this->maxCookieAge,
                    "/"
                );
            }
        }
    }
}

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

$click = new PJNClick();
$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("pjnclick.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("pjnclick.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 Ascend conversion pixel. It is important to test the pixel on your confirmation page to make sure that everything is working properly.
 

<?php
/* Pepperjam Click storage script
*  This script should be included on every page on your website
*/
include("pjnclick.php");

// Pixel specific data
$program_id       = 123;
$transaction_type = 1; //sale
$promocodes       = array();
$order_id         = 123456;
$amount           = 189.99;
$clickData        = $click->getCookieData();

/*
 * Now, we create and output the iframe.
 */
$pixel_html =
    '<iframe src="https://t.pepperjamnetwork.com/track?'; .
    'PID='          . $program_id .
    '&AMOUNT='      . $amount .
    '&TYPE='        . $transaction_type .
    '&OID='         . $order_id .
    '&CLICK_ID='    . $clickData .
    '&PROMOCODE='  . implode($promocodes, ',') .
    '" width="1" height="1" frameborder="0"></iframe>';

echo $pixel_html;
?>