Transfer Affiliate Codes From URLs to Links and Forms
When your affiliates send traffic to your site, their affiliate code can be inserted into links and forms at the web page when the referred visitor arrives. We'll call this the "landing page."
Link URLs on the landing page such as http://example.com/page.php can become http://example.com/page.php?AF33 or http://example.com/page.php?affiliate=AF33
Hidden form fields can have the affiliate code inserted. The result may be something like
<input type="hidden" name="affilate" value="AF33">
(This article's example affiliate code is "AF33". When an example name for the affiliate code value is required, "affiliate" is used.)
As the referred site visitor arrives at the landing page, JavaScript extracts the affiliate code from the URL. JavaScript can also used to append the affiliate code to links in the web page and/or to insert the affiliate code into form fields.
No cookies are required for it to work. (Cookies may be required elsewhere for your affiliate software to do its job. But no cookies are required to transfer affiliate codes into links and forms on the landing page.)
Reasons for doing this include:
-
Cookies sometimes don't survive. This method can track an affiliate code from page to page within the website, whether or not a cookie is set.
-
Cookies don't work when the referred visitor goes to another of your domains. With this method, the affiliate code can be tracked from domain to domain as well as from page to page.
-
The affiliate code inserted into forms can be used within the form processor's capabilities. Examples of use include storing the affiliate code in a database and using the affiliate code in an autoresponder series.
This method uses JavaScript. To use PHP instead of JavaScript to accomplish similar ends, see Visitor Affiliate Codes in Links and Forms.
The Web Page URL
The landing page URL needs to contain the affiliate code so the JavaScript can extract it.
The affiliate code follows a "?" or a "#" character in the URL. If there's an "=" character somewhere after the ? or #, the affiliate code is considered to be whatever followed =.
Each of these URL examples contain affiliate code "AF33":
http://example.com/page.html?AF33 http://example.com/page.html#AF33 http://example.com/page.html?affiliate=AF33 http://example.com/page.html#affiliate=AF33
Use either ? or #, not both.
There are differences between using ? and # in the URL.
-
Using ? in the URL:
-
Search engines may consider URLs with different affiliate codes to be different URLs.
-
If it's a PHP web page, the PHP code has independent access to the affiliate code in the URL.
-
-
Using # in the URL:
-
Search engines are unlikely to consider URLs with different affiliate codes to be different URLs
-
If it's a PHP web page, the PHP code can't access the affiliate code in the URL.
-
JavaScript extracts the affiliate code from the URL. The affiliate code can then be inserted into links and forms.
The Affiliate Code Extractor
This JavaScript will extract the affiliate code. It may be put into the head area of the web page source code or within the body area somewhere above the first point where the affiliate code will be used.
<script>
function ExtractAffiliateCode()
{
var DefaultAffiliateCode = "mydefaultcode"; // may be blank.
var ql = document.URL.indexOf("?");
if(ql < 0) { ql = document.URL.indexOf("#"); }
if(ql < 0) { return DefaultAffiliateCode; }
var affcode = document.URL.substr(ql+1);
if( (ql=affcode.indexOf("=")) >= 0 ) { affcode = affcode.substr(ql+1); }
return affcode;
}
ExtractedAffiliateCode = ExtractAffiliateCode();
</script>
In case your implementations requires an affiliate code of some kind in order to work correctly, a default affiliate code may be specified (colored blue in the above code). The default affiliate code may be changed. And it may be made blank by removing it from between the quotation marks.
If your implementation always requires an affiliate code, then specify a default affiliate code. Otherwise, it's optional.
Appending Affiliate Codes to a Link
The extracted affiliate code can be appended to the link URL with an onclick attribute in the link "a" tag.
As an example, let's use this link:
<a href="http://example.com/somewhere.php"> Click here </a>
To append the affiliate code to the link URL, add this onclick attribute:
onclick="this.href+='?'+ExtractedAffiliateCode; return true;"
To use # instead of ?, change the blue ? to a # character. A name or key followed by an = character may be between the blue ? and the affiliate code.
These onclick attributes are all valid:
onclick="this.href+='?'+ExtractedAffiliateCode; return true;" onclick="this.href+='#'+ExtractedAffiliateCode; return true;" onclick="this.href+='?affiliate='+ExtractedAffiliateCode; return true;" onclick="this.href+='#affiliate='+ExtractedAffiliateCode; return true;"
Here's the example link with the onclick attribute inserted:
<a onclick="this.href+='?'+ExtractedAffiliateCode; return true;" href="http://example.com/somewhere.php"> Click here </a>
Inserting Affiliate Codes Into a Form
The extracted affiliate code can be inserted into form fields.
Let's use this hidden field as an example:
<input type="hidden" id="affiliate-code-field" name="aff" value="">
One or more form fields may contain the affiliate code. Each field to contain the affiliate code needs a unique id value. In the example, it's colored blue.
To insert the extracted affiliate code into the field, put this JavaScript somewhere below the form:
<script>
document.getElementById("affiliate-code-field").value=ExtractedAffiliateCode;
</script>
The colored blue id value needs to be identical to the id value of the form field where the affiliate code is to be inserted.
If you have more than one form field that needs the affiliate code inserted, repeat the document.getElementById(... line in the above JavaScript as often as necessary.
In summary, before affiliate codes can be used they must be extracted from the URL.
Then, the affiliate code can be used. This article described how to append it to a URL and how to insert it into a form field.
Other things can be done with the affiliate code. Use JavaScript to insert or publish it wherever you wish to have it.
(This article first appeared in Possibilities ezine.)
Will Bontrager