www.gurteen.com

Document

Enabling your website to track affiliates

 




I have developed a simple javascript module (see bottom of page) that allows you to enable your website to track affiliates.

Here is how it works:

1. Place the Javascript module at the top of every page that you wish to enable on your website i.e. the pages that affiliates will link to.

2. When an affiliate links to a page on your site they will need to add an 'affiliate ID' parameter to the link

e.g. If the normal URL of a page say describing a product on your site was

http://www.website.com/product

then as an affiliate I would link to that page using the following link:

http://www.website.com/product?affiliate=gurteen

?affiliate=gurteen has been added asa paramater to the end of the url to to indicate that 'gurteen' is the ID of the affiliate linking to the page.

3. When any of the enabled pages are accessed a cookie is automatically stored on the user's PC that contains the ID of the affiliate - taken from the parameter on the link. The cookie name is 'affiliate' and in this example its value would be 'gurteen'

4. Now also include the Javascript module at the top of your Order form page. And within the body of the form include the following code:

<SCRIPT language="JavaScript">
setHiddenAffiliateField()
</script>

This code will read the stored cookie and create a hidden field called 'affiliate' that contains the name of the affiliate (e.g. gurteen) and will be submitted as part of the Order form when it is posted. You thus have a record with each Order form of the affiliate who directed the customer to your site.

For test purposes the following code will create an affiliate field that is not hidden so you can see the value in the browser.

<SCRIPT language="JavaScript">
setAffiliateField()
</script>

5. I have set the cookie life to 30 days which means that if a potential customer visits your site from an affiliated site and does not place an order but returns within 30 days then the hidden affiliate field will still be set to the name of the original affiliate. This value is easily changed in the code if need be.

EASY!


For more information on associate programs and affiliate tracking - see the following links:
http://www.associateprograms.com/search/newsletter033.shtml
http://www.myaffiliateprogram.com/youtrack.asp

<SCRIPT language="JavaScript">

function queryField(opt)
{
var keyloc // The location of the start of "key=value"
var nextkey // The start of the next key
var start // The start of the value
var opts // The options specified by the search string
var optval // The value of the selected option

// Determine the options/search string
opts=self.location.search
// Most keys start after an & and are followed by an = sign
keyloc = opts.indexOf("&" + opt + "=")
// If a string isn't found, indexOf returns -1. So, we try the "first"
// key, which appears right after the initial question mark
if(keyloc == -1) {
keyloc = opts.indexOf("?" + opt + "=")
}
// If, at this point, we still haven't found the key, stop.
if (keyloc == -1) {
return ""
}
// The value normally ends with an ampersand (which marks the start of the next key/value pair)
nextkey = opts.indexOf("&",keyloc+1)
// But sometimes there is no next pair
if (nextkey == -1) {
nextkey = opts.length
}
// Okay, what next? Verify that it's reasonable
if (nextkey < keyloc) {
return ""
}
// Get and return the value
sval = keyloc+2+opt.length
optval = plustospace(unescape(opts.substring(sval,nextkey)))
return optval
} // getOption()

function plustospace(txt)
/* Converts all the plus signs in a string to spaces. */
{
if (txt == "") { return txt }

// Variables
var newtxt="" // The txt without the spaces
var pos=0 // The position of the plus sign
var prev=0 // The position of the previous plus sign
var done=false // sentinel for loop
var tmp // Used for debugging

// Repeatedly find the next + sign, stopping when no more
// are found
// alert("Text is '" + txt + "'") // DEBUG
while (!done) {
pos = txt.indexOf("+",prev)
// tmp = prompt("Plus found at '" + pos + "'", "OK") // DEBUG
// if (tmp != "OK") { done = 1 }// DEBUG
if (prev >= txt.length) {
done = true
}
else if (pos == 0) {
prev=1
newtxt += " "
}
else if ((pos < 0) || (pos == "")) {
// Not found ... exit
done = true
}
else {
// Copy text
if (pos>prev) { newtxt += txt.substring(prev,pos) }
newtxt += " "
// And move on
prev=pos+1
}
}
// Get the last little bit
newtxt += txt.substring(prev,txt.length)
return newtxt
}

function writeCookie(cookie_name, cookie_value, cookie_life)
{
var today = new Date()
var expiry = new Date(today.getTime() + cookie_life * 24*60*60*1000)

var cookie_string =cookie_name + "=" + escape(cookie_value)
if(cookie_life){ cookie_string += "; expires=" + expiry.toGMTString()}
cookie_string += "; path=" + "/"
document.cookie = cookie_string
}

function readCookie(NameOfCookie)
{
if (document.cookie.length > 0)
{
begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1)
{
begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end));
}
}
return null;
}

function setAffiliateField()
// the affiliate field has a fixed name 'affiliate'
{
// get the affiliate parameter from the url if exists
// e.g.
http://www.website.co.uk?affiliate=gurteen

affiliate = queryField("affiliate");

// now drop a transient cookie called 'affiliate' with the affiliate parameter as its value i.e. "gurteen"

if (affiliate == "")
{
// do nowt
}
else
{
writeCookie("affiliate",affiliate,30);
}

// now get the cookie and set the affiliate field

temp = readCookie('affiliate');
if (temp == null) {affiliate = ""} else {affiliate = temp};
document.write('<input type="text" name="affiliate" value="'+affiliate+'">');
document.write("<br><br>Affiliate: " + affiliate)
}

function setHiddenAffiliateField()
// the affiliate field has a fixed name 'affiliate'
{
// get the affiliate parameter from the url if exists
// e.g.
http://www.website.co.uk?affiliate=gurteen

affiliate = queryField("affiliate");

// now drop a transient cookie called 'affiliate' with the affiliate parameter as its value i.e. "gurteen"

if (affiliate == "")
{
// do nowt
}
else
{
writeCookie("affiliate",affiliate,30);
}

// now get the cookie and set the affiliate field

temp = readCookie('affiliate');
if (temp == null) {affiliate = ""} else {affiliate = temp};
document.write('<input type="Hidden" name="affiliate" value="'+affiliate+'">');
}

</SCRIPT>


If you are interested in Knowledge Management, the Knowledge Café or the role of conversation in organizational life then you my be interested in this online book I am writing on Conversational Leadership
David Gurteen


Follow me on Twitter

How to contact me


My Blog

Gurteen Knowledge Community
The Gurteen Knowledge Community
The Gurteen Knowledge Community is a global learning community of over 21,000 people in 160 countries across the world.

The community is for people who are committed to making a difference: people who wish to share and learn from each other and who strive to see the world differently, think differently and act differently.

Membership of the Gurteen Knowledge Community is free.
Knowledge Community




     


home
back
contact
request help
visitor book
Thursday 26 December 2024
03:30 PM GMT