Free Affiliate PHP Script to Track Multiple Traffic Sources
The Magic Script Page
Note: if you’re not comfortable editing HTML and PHP, this might not be the best tracking option for you.
To try and make this easier to understand, I created a diagram of how it works:
I’ll cover each of the five pieces of the diagram above in a second.
Basically, the way this works is, any time you’re driving traffic that could possibly lead to a sale… you’ll actually be sending that traffic to the “Magic Script Page”.
This single page tracking script really is magic. It can track a lot of the same metrics that many of the different paid online tracking software platforms charge decent money for.
All it takes is a little a technical know-how to get it uploaded and functioning. Now, since there’s no online user interface to go along with this, it’ll require you to do some manual spreadsheet work to get an overall view of your stats.
Lets take a closer look at how it works.
An Overview of the Mechanics
Ok, lets break down the diagram above.
#1: Drive Traffic
In this example, I demonstrated an ad campaign that’s driving traffic to a squeeze page. Instead of sending that traffic directly to the squeeze page though, I’m sending it to the script page which then forwards it on to its destination.
The script page first checks to see if they’ve already been “cookied” by me before. If not, then it automatically pulls in any data I add on to the end of my URL from my Facebook campaign. Normally things like: traffic source, campaign ID, and ad ID. I’ll explain later on how that all works but it basically takes that data and places cookies on the visitor’s computer so I know what ad they came from. More on that later.
#2: Redirecting to the Script Page
This is the first page in my new subscriber instant upsell funnel. After someone opts in for my lead magnet, they see this first upsell page. If they decide to click through to see the offer, they’re first sent to the script page once again (yes, the same exact script page).
The script page works the same every time no matter where the traffic is coming from. It checks to see if the visitor has already been “cookied”. If they haven’t, data is pulled in from the URL and cookies are then set.
#3: Sending the Visitor to the Destination Page
In this example, since the visitor has already landed on the script page once and cookies have already been set, the script page sees that and sends the visitor on their way to the destination page.
Except for one small difference. Since they’ve already been cookied, the script knows that you’re sending the visitor to an offer page and it appends their unique traffic data to the end of the destination page as a tracking ID.
In my case, I mostly promote Clickbank offers so my visitors’ unique traffic data is combined into one long string of text and added to the end of my Clickbank affiliate link as the tracking ID.
This happens in the background, all in a matter of milliseconds, without the visitor having the slightest clue. They’ll never see a Clickbank link or your tracking ID (unless they look at the bottom of the Clickbank shopping cart) and it won’t create any kind of bottlenecks in your funnels.
#4: Continuing On Through the Funnel
If the visitor decides to pass on the first upsell, they’re taken to the second upsell. Here they can click through to view the offer where they’ll follow the same path as #3, or they can pass again and then they’re shown the third upsell.
#5: Emails Get Redirected to the Script Page Too
This is the coolest part of this entire tracking hack. Since you’re “cookieing” people the moment they click on one of your ads, you don’t need to worry about what happens when you promote products via email.
All of that initial traffic data stays the same. If they decide to buy what you’re promoting, the sale will be attributed to that initial ad that they clicked on. All you have to do is first send your email traffic to the script page so it can append the “cookied” data.
Fun Facts About the Script Page
Here are a few cool features of the Magic Script Page:
- It can be hosted on any domain and any hosting account as long as it can run PHP
- It can be used on an unlimited number of domains
- It can be used to track an unlimited number of domains
- With the addition of a little code snippet you can add a retargeting pixel when a visitor clicks a specific link (you can do this for an unlimited number of links—it’s best used to set a retargeting pixel when a visitor clicks a link to view an offer you’re promoting)
- Since the script page follows the first click attribution model, you can track lifetime sales per visitor (or subscriber) based on the first ad campaign link of yours they clicked on
Getting Started With the Setup
To start, I recommend registering a short domain name that you can use solely for tracking purposes. This is where you’ll upload the one-page tracking script. You can use the same domain as your funnel but then you’re kinda limited to just one domain for your tracking script page.
I like to register a domain name that’s somewhat generic so I can use it across many different affiliate niches.
For example, you could register the domain name ‘wwtrk.com’. I recommend creating a folder on this domain where you can upload the tracking script to.
You could call the folder ‘track’. So, when linking to your tracking script page, the URL would be wwtrk.com/track/. Make sure the tracking script filename is index.php so it loads automatically when someone visits that URL.
You’ll also need to install Google Analytics (which I’ll refer to as GA from now on) on all of your funnel pages — not on your tracking domain though.
We’ll be using GA to pull in traffic stats to use in our tracking spreadsheet. We’ll also use GA to track some click-through events in our new subscriber instant upsell funnel.
Once you have your tracking domain ready and GA installed on every funnel page, it’s time modify the Magic Script Page before you upload it.
Modifying the Magic Script Page
First off, you’ll need to download the Magic Script Page here. It’s inside of a zipped folder but all you need is the one ‘index.php’ file. The folder itself can be discarded.
Modifying the Magic Script Page is actually pretty easy. You will be editing PHP code but it’s pretty straightforward and I’ve included examples right inside the script for you already.
The only thing you need to modify inside the script page are your destination URLs. You can have as many, or as few, as you want. Take a look at the image below:
Inside the Destination Links section, there are three places where you need to modify the code. First, is the number in quotes to the left of the ‘=>’.
Think of these as page numbers when you’re creating your tracking link… “I want to create a link to go to page 1”. To do that, you just put the number 1 in the ‘url’ parameter of your tracking link.
You can see an example tracking link on line 9 in the image above. Everything after the question mark is called a parameter. That’s where you’ll put all of the data you want to track as well as what page you want the visitor to end up on.
Second, inside the Destination Links section, is the actual URL. This is typically your Clickbank hoplink. You can create a hoplink by going to the Clickbank Marketplace, finding the product you want to promote, click the ‘Promote’ button, and then enter your Clickbank affiliate ID.
Be sure to include a trailing ‘?’ at the end of your URL’s, or… if you’re sending visitors to an alternate sales page that already includes a ‘?’, you need to include a ‘&’ at the end of the URL like you see on line 35 in the image above.
Third, it’s a good idea to include the name of the Clickbank product in a comment next to each of your destination links. You start a comment in PHP with two forward slashes like this ‘//’. You can see examples of how to do this on lines 33-35 after each URL.
A few important things to note when modifying this script:
- Make sure the page number and the destination URLs are enclosed in quotes
- Make sure to include a comma after each destination URL (outside of the quotes)
- Don’t edit anything on line 32 or line 36
- If you want to add more destination URLs, just follow the same format as the other links
- If you need to remove a destination URL, be sure to delete the entire line and don’t leave any empty lines inside the $links arrary
When you’re happy with the way the script page looks, go ahead and upload it to the folder on your tracking domain.
To create a tracking domain, use the following format:
The bold items in that URL above are what gets changed. Here’s how one of your tracking links might look using my wwtrk.com example from above:
You can see the data I entered in the URL above, it’s the bold items. Other than the URL parameter, you can literally use whatever you want for the value of the other three parameters.
Remember to keep it short though. Clickbank limits your tracking IDs to 24 characters and the magic script page separates these three parameters with an ‘x’.
So, your actual Clickbank tracking ID for that link above is: fbx0991x0001
I like to use 4-digit numbers for my campaign IDs and my ad IDs to save space in my tracking ID and to somewhat mask my tracking links from spying eyes. Since we’re tracking everything on a spreadsheet anyway, it’s pretty easy to keep track of everything.
You just need to make sure to include those same 4-digit numbers in your campaign name and your ad name inside of your traffic source.
Lastly, once someone has already been “cookied”, meaning they’ve clicked on one of your ads before and opted in to your email list, you can use this same tracking link format to promote more affiliate products to them.
Except now, the only parameter you need to include is the destination URL… because they’re already “cookied” with the previous ad data. When they visit the magic script page the second time, the script knows that they’re already “cookied” and ignores everything but the destination URL.
The visitor’s initial unique ad information will be appended to the end of your destination URL as the Clickbank tracking ID.
Can you use this script for affiliate products outside of Clickbank? Absolutely, but it would require you to edit the code at the bottom of the page so you can change the link structure. If you have an affiliate network you’d like to use it for, let me know and I can code you up a quick version that’ll work for that network.
Compiling The Data
Now that everything is set up and running smoothly, it’s time to gather the data and see what’s working and what isn’t. Like I said before, I just use a really basic spreadsheet for this.
Nothing fancy, just enough columns to keep track of each tracking ID and each traffic campaign. At the end of the day, you want to see which tracking IDs are making you the most money as opposed to where your money is being spent inside the traffic network.
Inside the same spreadsheet, on a separate tab, I usually keep track of all of my campaign IDs and ad IDs. Next to each one I make a note that describes each one so I can quickly glance back and forth to see what’s working and what’s not.
To gather the data in Clickbank, go to ‘Reporting’ and then ‘Transactions’. Choose your date range, make sure refunds, chargebacks, and bounces are all checked and click ‘Go’. Then, I like to sort by tracking ID (TID) so I can quickly tally up the sales/refunds and input them into the spreadsheet.
Tracking CTR For Your Upsell Pages
Lastly, in order to know the CTR for your upsell pages, you’ll need to setup Google Analytics Event Tracking. Once you’ve added the basic analytics code to every page, there’s another block of code you’ll need to copy and paste to the pages you want to track events for.
You’ll also need to edit each outgoing link. Here’s step-by-step instructions on how to set this up. Now, you’ll be able to see your CTR for each upsell page so you can try and improve it.
That’s basically it. I covered a lot here so don’t hesitate to ask if you have any questions, just leave a comment below.