mhinze.com archive

this is an archive of the old blog, ended 6/16/08





    18
    Apr

    Hiding Affiliate Links Switcheroo

    michael gray posted an article about hiding affiliate links, but the best tip was in the first comment.

    Link out to the real site in your static link (use nofollow if you want), then use a javascript onload function to replace the links on the fly with the affiliate ones (and even remove the nofollow, if you want to trick the nofollow-highlighers).

    Without javascript it is just recommending the official site. With javascript it’s an affiliate site. Sure you might lose the 18 non-javascript users, but the others will use the affiliate links (check your logs for the numbers). Just make sure to test it across the standard browsers (check your logs).

    not a bad idea at all.  here's the proof of concept in javascript. 

    here i apply an additional class to each link that describes the affiliate link to use.  in the POC i create two arrays: one for the class names and the other for the links.  i would use a server side technology for the production task of filling these arrays.

    also i would use a 301 redirect on the server instead of including my affiliate links anywhere that people or robots can see them.

    i'm using this great getElementsByClassName because it supports multiple classes on your anchor elements.

     

    <a class="lendingtree otherclass" rel="nofollow" href="http://www.lendingtree.com">
    Here is an affiliate link</a>.
    And <a class="myfico otherclass" rel="nofollow" href="http://www.myfico.com">
    here's another one</a>.
    <script type="text/javascript">
    function getElementsByClassName(strClass, strTag, objContElm) {
        strTag = strTag || "*";
        objContElm = objContElm || document;
        var objColl = objContElm.getElementsByTagName(strTag);
        if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
        var arr = new Array();
        var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
        var arrClass = strClass.split(delim);
        for (var i = 0, j = objColl.length; i < j; i++) {
            var arrObjClass = objColl[i].className.split(' ');
            if (delim == ' ' && arrClass.length > arrObjClass.length)
                continue;
            var c = 0;
            comparisonLoop:
                for (var k = 0, l = arrObjClass.length; k < l; k++) {
                    for (var m = 0, n = arrClass.length; m < n; m++) {
                        if (arrClass[m] == arrObjClass[k]) c++;
                        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
                            arr.push(objColl[i]);
                            break comparisonLoop;
                        }
                    }
                }
        }
        return arr;
    }
    // To cover IE 5.0's lack of the push method
    Array.prototype.push = function(value) {
      this[this.length] = value;
    }
    
    // array of classes and links - they need to match up
    var classes = new Array("lendingtree", "myfico");
    var afflinks = new Array("http://example.com?x=123123&y=1123123", "http://example.com?z=asdk#ikjlk");
    
    // for every affiliate link class
    for (var i = 0; i < classes.length; i++) {
    // get the anchor elements that have that class
        var links = getElementsByClassName(classes[i],"a",document);
    // for every anchor (a)
        for (var j = 0; j < links.length; j++) {
    // set the href to our corresponding affiliate link
            links[j].href = afflinks[i];
    // clear the rel attribute
            links[j].rel = "";
        }
    }
    </script>

     

     

    Technorati tags:

    Leave a Reply

    You must be logged in to post a comment.

    © 2007 mhinze.com