Capturing Referral Codes, Affiliate Codes, or Gift Codes with Javascript – Part 2

on December 15, 2010 by Downloads, Programming with 1 comment

In the first part of this series I outlined the idea behind the cookie based affiliate or discount code system being developed. In this post I’ll detail how we are using it and give some examples for consuming the code in a few programming languages.

We’re starting with this javascript file to initiate a code cookie:

Grab it on GitHub

Examples of it’s installation and use can be found in Part I.

In our scenario, we have a landing page where a customer will navigate in order to receive some sort of incentive. When the landing page is loaded, a discount code related to the campaign is stored as a cookie using the SaveCode(‘Code’); method in our .js file. Now that the cookie has been stored, it can be retrieved later on when it comes time to process some type of transaction, in this case, signing up for a trial account for our service.

After the customer visits the landing page where the discount is applied, they hopefully continue on to sign up for a trial account. In the back end of our application we have created a Discount table that stores various discount codes and their details that can be associated with an account’s subscription. When the customer creates this new subscription, the application checks to see if any discount codes have been applied by way of the cookie object we set previously. If no cookie is present, the customer is processed as a normal, non-discount subscriber. If a cookie is present, the code is referenced against the Discount table in the database to pull up the details of that discount and apply them.

To cookie can be retrieved in 2 ways:

  1. Read the cookie back using client-side code (JavaScript, VBScript, etc.)
  2. Read the cookie back using server-side code (C#, PHP, Java, etc.)

You can easily retrieve the cookie on the client side by calling the GetCode(); function in our JavaScript file, this will return the stored value as a string.

Retrieving the cookie value on the server side depends on which language and how you are passing the value but the principal is generally the same. Here are a few examples of cookie retrieval:


string discountCode = "";
HttpCookie cookie = Request.Cookies["MyCookieName"];

if (cookie != null)
    discountCode = cookie.Value;


$discountCode = "";
if (isset($_COOKIE["MyCookieName"]))
  $discountCode = $_COOKIE["MyCookieName"];


Cookie[] cookies = request.getCookies();
string discountCode = "";
for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++)
    Cookie cookie1 = cookies[loopIndex];
    if (cookie1.getName().equals("MyCookieName"))
        discountCode = cookie1.getValue();

Once you’ve got your discount code value, it’s up to your system requirements how you use it.

The following two tabs change content below.

Matthew Mombrea

Matt is a longtime entrepreneur and software engineer. He is a founder of Cypress North, and chief technology officer. Matt also contributes to the community as a columnist on

1 Comment

Leave a Reply