Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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.)

When the referred visitor arrives with the affiliate code appended to the URL in the browser's address bar, JavaScript extracts the affiliate code. The affiliate code can then be inserted into the page with JavaScript. I'll describe how to insert the affiliate code into URLs and forms.

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.

  1. 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.

  2. 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.

That's how to extract the affiliate code from a URL in the browser's address bar and optionally append it to links and/or insert it into form fields in the web page.

There are three parts to the system. The first is required. The others are optional. Extracting the affiliate code, appending the affiliate code to links, and inserting the affiliate code into form fields.

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC