Back to Blog

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

December 15, 2010

By Matt Mombrea
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:

ASP.NET C#

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

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


PHP

<?php
$discountCode = "";
if (isset($_COOKIE["MyCookieName"]))
{
  $discountCode = $_COOKIE["MyCookieName"];
}
?>

Java

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.

Matt Mombrea

Matt Mombrea

Matt is a longtime entrepreneur and software engineer. He also sits on the board of Computer Science at SUNY Fredonia. Born and raised in Buffalo, Matt is passionate about building and maintaining great businesses in Buffalo. Apart from leading Cypress North, Matt architects our company’s datacenter, engineers dozens of custom applications, and directs the rest of the development team.

See Matt's Most Recent Posts

Share this post

1 comment

Capturing Referral Codes, Affiliate Codes, or Gift Codes with Javascript | Cypress North Blog December 15, 2010Reply

[…] up to your program model for how you use the codes once they are stored but I will be writing a follow up post with some examples of its usage in different languages. In my usage, the cookie value will be read […]

Leave a Reply

Search our blog

Start A Project

Categories

What's next?

Well...you might like one of these

Article

Is Google Analytics 4 a Tactical Move...

There’s something fishy going on with the way that Google...

Read article

Article

How We Build It: Website Process with...

This month, I sat down with Matt Mombrea, Chief Technical...

Read article

Article

How To See Audience Performace Across...

Google Ads makes it really easy to see performance at the...

Read article