javascriptphpprependaffiliate

Automatically prepend affiliate identifier to URLs


I would like to automatically add an affiliate identifier to URLs on my website. For example, I have external URLs such as:

https://www.adairs.com.au/

https://bedthreads.com.au/products/caitlin-robson-footed-bowl

My affiliate identifier varies based on the retailer, such as:

adairs.com.au: https://t.example.com/12345/t/54321?Url=

bedthreads.com.au: https://t.example.com/12121/t/21212?Url=

I need these automatically prepended at the beginning of the external URLs upon click to form:

https://t.example.com/12345/t/54321?Url=https://www.adairs.com.au/

https://t.example.com/12121/t/21212?Url=https://bedthreads.com.au/products/caitlin-robson-footed-bowl

The closest solution I have found was here however I am not very experienced in java and was unable to make it work. Ideally, I would also like to add other automatic affiliate identifiers for other retails in future.

Also, is it possible to add this in PHP? Any help would be greatly appreciated. Thank you.

    // attach a click even to all <a> elements
    $("a").click(function() {
        addAffiliate(this);
    });
    
    // adairs affiliate URL for redirect
    var adairsaffURL = "https://t.example.com/12345/t/54321?Url=";
    // bedthreads affiliate URL for redirect
    var bedthreadsaffURL = "https://t.example.com/12121/t/21212?Url=";
    
    // function called when link is clicked
    function addAffiliate(link) {
        // make sure this link is not to the current site and does not contain the affiliateURL
        if ((link.href).indexOf(adairs.com.au) < 0 && (link.href).indexOf(adairsaffURL) < 0){
            // update the link with the affiliateURL, the url encoded link, and the additional query string
            link.href = affiliateURL + escape(link.href);
        
}else if((link.href).indexOf(bedthreads.com.au) < 0 && (link.href).indexOf(bedthreadsaffURL) < 0){
                link.href = bedthreadsaffURL + escape(link.href);  
        }
        alert(link.href);
        // return true to follow the link
        return true;
    }

Solution

  • As your idea, I updated your code to let it works. Please Run code snippet to see the demo.

    You just need to use .indexOf('adairs.com.au') to check the link belongs to the affiliate program, so you append it into affiliate URL.

    jQuery(document).ready(function($) {
    
      // adairs affiliate URL for redirect
      var adairsaffURL = "https://t.example.com/12345/t/54321?Url=";
      // bedthreads affiliate URL for redirect
      var bedthreadsaffURL = "https://t.example.com/12121/t/21212?Url=";
    
      // function called when link is clicked
      addAffiliate = function(link) {
        
        if (link.hash.substr(1).length > 0) return true;
        var redirectUrl = link.href;
        if (!redirectUrl || !isValidURL(redirectUrl)) return true;
        
        if (redirectUrl.indexOf('adairs.com.au') > 0) {
          redirectUrl = adairsaffURL + escape(redirectUrl);
        } else if (redirectUrl.indexOf('bedthreads.com.au') > 0) {
          redirectUrl = bedthreadsaffURL + escape(redirectUrl);
        }
        link.href = redirectUrl;
        return true;
      }
    
      $("a").click(function(event) {
        addAffiliate(this);
      });
    
      function isValidURL(string) {
        var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
        return (res !== null)
      };
    
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <a href="https://www.adairs.com.au/">test adairs</a>
    <br/><br/>
    <a href="https://bedthreads.com.au/products/caitlin-robson-footed-bowl">test bed threads</a>
    <br/><br/>
    <a href="http://www.google.com">test not match adairs or bedthreads</a>
    <br/><br/>
    <a href="#dsdsd">test no link</a>

    Update: function to add to Wordpress. Put the below into your functions.php in your theme.

    add_action('wp_footer', 'auto_affiliate_url');
    function auto_affiliate_url()
    { 
    ?>
    
    <script>
    jQuery(document).ready(function ($) {
    
          // adairs affiliate URL for redirect
          var adairsaffURL = "https://t.example.com/12345/t/54321?Url=";
          // bedthreads affiliate URL for redirect
          var bedthreadsaffURL = "https://t.example.com/12121/t/21212?Url=";
    
          // function called when link is clicked
          addAffiliate = function(link) {
            
            if (link.hash.substr(1).length > 0) return true;
            var redirectUrl = link.href;
            if (!redirectUrl || !isValidURL(redirectUrl)) return true;
    
            if (redirectUrl.indexOf('adairs.com.au') > 0) {
              redirectUrl = adairsaffURL + escape(redirectUrl);
            } else if (redirectUrl.indexOf('bedthreads.com.au') > 0) {
              redirectUrl = bedthreadsaffURL + escape(redirectUrl);
            }
            link.href = redirectUrl;
            return true;
          }
    
          $("a").click(function(event) {
            addAffiliate(this);
          });
    
          function isValidURL(string) {
            var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
            return (res !== null)
      };
    
    
        });
    </script>
    
    <?php
    }