Resource Center

Itemized Tracking - Click ID Capture and Storage: Node.js

The following code is an example of how to implement a Node.js solution for Itemized Tracking - Click ID Capture and Storage  from Pepperjam 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 as middleware to express.js 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.
 

const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();

const clickIdUrlParameter = 'clickId';
const clickIdCookieName = 'pjn_click';
const lookBackPeriodInDays = 60;

const maxCookieAge = lookBackPeriodInDays * 24 * 60 * 60000;

app.use(cookieParser());

const PJNClickMiddleware = (req, res, next) => {
    /**
     * Returns true if the querystring contains a click ID
     */
    hasClickId = (req) => {
        return !!req.query[clickIdUrlParameter];
    }

    /**
     * Returns the clickId value
     */
    getClickIdvalue = (req) => {
        return req.query[clickIdUrlParameter];
    }

    /**
     * Creates the cookie
     */
    createCookie = (req, res) => {
        let duplicate = false;
        let cookieValue = req.cookies[clickIdCookieName];
        let clickId = getClickIdvalue(req);
        if (clickId === cookieValue) {
            duplicate = true;
        }
        if (!duplicate) {
            res.cookie(clickIdCookieName, getClickIdvalue(req), {
                maxAge: maxCookieAge
            });
        }
    }

    // Main Execution
    if (hasClickId(req)) {
        createCookie(req, res);
    }
    next();
}

app.use(PJNClickMiddleware);

app.get('/', (req, res) => {
    res.send('<h1>Welcome to our Basic Online Store!</h1>');
});

app.get('/item1', (req, res) => {
    res.send("<h2>Item 1</h2>");
});

app.get('/thankyou', (req, res) => {
    let program_id = 123,
        transaction_type = 1, // sale
        integration_type = 'ITEMIZED',
        promocodes = [123, 456],
        amount = 500,
        orderId = 123456,
        order_output = '',
        order_items = {
            "item1":
                {
                    id: 'SKU-922320',
                    quantity: 2,
                    price: 25000.99
                },
            "item2":
                {
                    id: 'SKU-9233',
                    quantity: 1,
                    price: 189.99
                }
        };

    let
        cookieValue = req.cookies[clickIdCookieName],
        clickData = "";

    let x = 1;
    for (var key in order_items) {
        if (order_items.hasOwnProperty(key)) {
            order_output += '&ITEM' + x + '=' + order_items[key].id;
            order_output += '&AMOUNT' + x + '=' + order_items[key].price;
            order_output += '&QTY' + x + '=' + order_items[key].quantity;
            x++;
        }
    }

    let
        output = '<h1>Thank You For Shopping!';
    output += '<iframe height=1 width=1 src="https://t.pepperjamnetwork.com/track?';
    output += 'PID=' + program_id;
    output += '&INT=' + integration_type;
    output += '&AMOUNT=' + amount;
    output += '&TYPE=' + transaction_type;
    output += '&OID=' + orderId;
    output += '&CLICK_ID=' + cookieValue;
    output += '&PROMOCODE=' + promocodes.toString();
    output += order_output;
    output += "\"\\>"

    cookie = req.cookies[clickIdCookieName]
    res.send(output);
});