An Overview on How to Create Redirects and Tracking Codes

An Overview on How to Create Redirects and Tracking Codes
Page content

Getting Started Creating Redirects and Tracking Codes

What is it about wanting to know how to create redirects and tracking codes? Well there could be countless reasons but for one, as far as racking codes go, someone running an e-commerce website may want to know the quality of traffic coming in from particular referrals. In other cases, one would like to track the progress of users browsing through a shopping cart for a better understanding of visitor behavior and trends.

As for redirects you may not want to link directly to affiliate links or other web services because if the link changes and you have several placements of the link you run the risk of a maintenance nightmare. Having a redirect can allow you to manage these links in a single location. Even simple implementations such as this online web poll can be tracked to avoid abuse.

How to Create a Redirect

To create the redirect code you need to create a file on your server such as a PHP file to handle the redirect function. The PHP file can contain other code as well but as a bare minimum it should have PHP’s built-in redirect command. The contents of the redirect file can be as follows:

<?php

$link = “https://www.your-affiliate.com/?id=your_unique_code”;

header(“Location: “. $link);

exit();

?>

How to Create a Tracking Code

Setting up a tracking code program involves several components which would be beyond the scope of this article to cover in detail. With this in mind, this article assumes you are pretty much comfortable with programming in a scripting language such as PHP.

To give an illustration of the process behind processing tracking codes, I will use a sample. Suppose you have a link like the one below:

https://www.my-website.com/affiliate.php?id=rAnDomcodE

Where rAnDomcodE is a unique code that can be generated once using PHP’s in-built rand() and srand() functions. Once this number is generated, it can be stored in a database entry linking it with the affiliates user details.

When a user accesses your website using the affiliate link, the details contained in the id portion of the link which represents rAnDomcodE above can be used to create a unique user session by using the unique rAnDomcodE.

<?php

$internal_session = session_name(“rAnDomcodE”);

?>

Details on the session functions can be found by going to this link,

This code will track every subsequent page the user visits. It must therefore be called on every page on the website, while storing each referring page in a log on the database. The general steps on tracking users is as follows:

  1. Collect Affiliates contact and payment information using a web form and store it in a database.
  2. Generate a random code unique to this affiliate, append it to a link and avail it to the affiliate.
  3. The affiliate posts the link on their website.
  4. A user clicks on the affiliate link and is referred to your website.
  5. Receive the id on the affiliate link on your website and check the corresponding affiliate name in the database.
  6. If found, create a session named after the affiliate’s random code.
  7. Track the users’ movement through your website using PHP’s in-built session handlers and save each referring page into a log to track progress up to a preset goal.

Here is an idea of the kind of code that can be involved on the server end in this tutorial on creating a Hit Counter.

This makes it sound straightforward and simple but there may be some considerable code involved setting up this particular scenario. Let us not forget there are several other situations which may cause you to want to know how to create redirects and tracking codes, but whichever the situation, the idea and process flow behind redirects and tracking codes is generally as described above.

References

Source: author’s own experience.

PHP Manual, https://www.php.net/manual/en/index.php

Image provided by writer