Track Affiliate Codes Between Domains
When a site visitor arrives from an affiliate's link, the affiliate code may be carried to your other domains. And back again.
Why? So the affiliate gets credit, and makes money, and sends more traffic to you.
It can work when cookies are turned off. Even when cookies are turned on, they can't normally be transferred from one domain to another.
There is a way to carry an affiliate code to another domain, and page to page within the same domain:
-
The affiliate code can be inserted into links that navigate to other pages or click to your other domains.
-
Form fields can be pre-populated with the affiliate code so it's available at the other end no matter which domain the form submits to.
-
If the other domain's page is in an iframe, the affiliate code may be part of the URL that loads the iframe with content.
This article describes how to get affiliate codes from one domain to another. And how to keep the affiliate code with the site visitor as they navigate from page to page — even when the visitor's browser doesn't accept cookies.
Implementation requires PHP, so your web page file name needs to end with ".php".
Here's an overview of the implementation steps:
-
The PHP script (below) is included in your web page with a line of PHP code.
-
Put a PHP snippet in links, form field values, wherever you want to insert the affiliate code.
Detailed instructions follow the source code of the PHP script.
The PHP script extracts the affiliate code from the URL, if it's available. If unavailable, it looks for a cookie with the affiliate code.
The PHP Script
Here's the source code for the PHP script. It's an affiliate code extracting script. Customization notes follow.
<?php /* Affiliate Code Extractor Version 1.0 October 31, 2015 https://www.willmaster.com/ Copyright 2015 Will Bontrager Software LLC */ /* Customization */ // Two steps: // Step 1 -- // In a URL, after the "?" sign, information generally is // specified as "name=value" or only "value". The name // is on the left of the "=" sign and the value is on // the right. If there's no "=" sign, there's or name // and there is only a value. // Below, at the PHP variable name $Name, specify the name // that holds the affiliate code in the URL. // As an example, in http://example.com/page.php?aff=code // the name of the variable is "aff" (the name on the // left of the "=" sign). // If there is no name, then it's specified as blank. // Example URL: http://example.com/page.php?code $Name = "aff"; // Step 2 -- // If the affilate code might be stored in a cookie, specify // the cookie name here. Otherwise, make the cookie name // blank. $CookieName = "affiliate_code"; /* End of Customization */ $AffiliateCode = ''; $Name = preg_match('/\w/',$Name) ? trim($Name) : ''; $CookieName = preg_match('/\w/',$CookieName) ? trim($CookieName) : false; if( $Name and isset($_GET[$Name]) ) { $AffiliateCode = $_GET[$Name]; } elseif( empty($Name) and isset($_SERVER['QUERY_STRING']) ) { $AffiliateCode = urldecode($_SERVER['QUERY_STRING']); } elseif( ! empty($_COOKIE[$CookieName]) ) { $AffiliateCode = $_COOKIE[$CookieName]; } ?>
Customization notes —
Two places need to be customized.
-
The name for the affiliate code.
Some URLs have a "?" character followed by information. The information generally is of the format name=value, with multiple name=value sets separated with a "&" character.
Sometimes, however, there's only the value. In those cases, there's no "=" because without a name the name and value don't need to be separated.
What this customization needs is the name in the name=value set that contains the affiliate code.
Here are come examples with the name colored green.
http://example.com/page.php?aff=code
http://example.com/page.php?aff=code&testing=yes
http://example.com/page.php?testing=yes&aff=code
http://example.com/page.php?code
The last example has no green colored name because there is no name part.
At the line in the PHP script, in the customization area, that begins with the word $Name (colored blue in the above source code), specify the name part of the name=value set for the affiliate code.
If there is no name part, specify "" for the $Name value. Example:
$Name = "";
-
The cookie name, if applicable.
If the affiliate code is also stored in a cookie, specify the cookie name in the PHP script, in the customization area, that begins with the word $CookieName (colored red in the above source code).
If there is no cookie for the affiliate code, specify "" for the $CookieName value. Example:
$CookieName = "";
Name the PHP script file AffiliateCodeExtractor.php or other preferred name that ends with ".php". Then upload the script to your server. Make a note of its location, as you'll need it in the Implementation step.
Each domain that will participate in tracking affilate codes will need its own copy of AffiliateCodeExtractor.php installed.
Implementation
At this point, you've customized and uploaded the cookie extracting PHP script. In the examples that follow, I'll assume you named the file AffiliateCodeExtractor.php
Every page that will participate in tracking affilate codes between domains or between pages of a domain will need to include AffiliateCodeExtractor.php in the page. It's done with this code:
<?php include_once($_SERVER["DOCUMENT_ROOT"]."/php/AffiliateCodeExtractor.php"); ?>
Update "/php/AffiliateCodeExtractor.php" (colored blue) to the location of the PHP script you uploaded in the previous step.
Now, all that's left is inserting the affiliate code into every participating page wherever you want the code to be. It's done with this PHP snippet:
<?php echo($AffiliateCode); ?>
Insert the above snippet in links, form field values, wherever you want to insert the affiliate code. Examples follow.
The examples assume the affiliate code is "abc123". The examples are in two parts, the result and the code to produce the result.
Example: Affiliate code in the web page content.
Your affiliate code is abc123.
Your affiliate code is <b><u><?php echo($AffiliateCode); ?></u></b>.
Example: Affiliate code in a link URL.
<a href="http://example.com/page.php?aff=<?php echo($AffiliateCode); ?>">Click here</a>
Example: Affiliate code in hidden field of a form.
<form method="post" action="script.php" enctype="multipart/form-data">
<input type="hidden" name="affCode" value="<?php echo($AffiliateCode); ?>">
<input type="email" name="email" placeholder="Type email here.">
<br><input type="submit">
</form>
Example: Affiliate code in the URL of a page in an iframe.
<iframe src="https://example.com/page.php?aff=<?php echo($AffiliateCode); ?>" style="width:2in; height:1in;">
</iframe>
Verifying
To verify the affiliate codes are inserted correctly and where you want them, load the page into your browser from an affiliate URL and then view the web page source code.
If the web page source code is okay, click on links or submit forms with the affiliate code and, when arriving at the destination pages, verify affiliate codes are inserted as they're supposed to be.
(This article first appeared in Possibilities ezine.)
Will Bontrager