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

emile-perron-190221-unsplash
POST
mmombrea-headshot
By Matt Mombrea
Share

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.

POST
mmombrea-headshot
By Matt Mombrea
Share

1 Comment

  1. Author's Headshot
    Capturing Referral Codes, Affiliate Codes, or Gift Codes with Javascript | Cypress North Blog December 15, 2010
    Reply

    [...] 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

Your email address will not be published. Required fields are marked *

Meet the Author

mmombrea-headshot
CTO / Partner

Matthew Mombrea

Matt is our Chief Technology Officer and one of the founders of our agency. He started Cypress North in 2010 with Greg Finn, and now leads our Buffalo office. As the head of our development team, Matt oversees all of our technical strategy and software and systems design efforts.

With more than 19 years of software engineering experience, Matt has the knowledge and expertise to help our clients find solutions that will solve their problems and help them reach their goals. He is dedicated to doing things the right way and finding the right custom solution for each client, all while accounting for long-term maintainability and technical debt.

Matt is a Buffalo native and graduated from St. Bonaventure University, where he studied computer science.

When he’s not at work, Matt enjoys spending time with his kids and his dog. He also likes to golf, snowboard, and roast coffee.