0

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;
    }
Machavity
  • 30,841
  • 27
  • 92
  • 100
KaiH
  • 3
  • 2

1 Answers1

0

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
}
Tuan Dao
  • 2,647
  • 1
  • 10
  • 20
  • Thank you, that code looks like it should work. I've tried implementing it into my website but with no luck unfortunately. – KaiH Sep 22 '21 at 05:14
  • I've tried adding it into footer.php with as well as: add_action('wp_footer', 'auto_affiliate_url'); function auto_affiliate_url() { ?> // javscript code here – KaiH Sep 22 '21 at 05:22
  • @KaiH I updated the function to add into wordpress, please put it into your functions.php in your theme folder. – Tuan Dao Sep 22 '21 at 05:48
  • That's working perfectly now. Thank you for the help, it's very much appreciated! – KaiH Sep 22 '21 at 06:18
  • Sorry to bother you, but I've come across an issue where my pop ups no longer open after implementing this code. I'm using WordPress/Elementor. Any idea how I might avoid this issue? – KaiH Sep 26 '21 at 04:41
  • @KaiH Is there any errors in console? – Tuan Dao Sep 26 '21 at 05:18
  • I've checked and there are no errors in console. The below is appended to the URL however, whereas normally the URL would remain unchanged and the popup would display on button click. #elementor-action%3Aaction%3Dpopup%3Aopen%26settings%3DeyJpZCI6IjM3NjgiLCJ0b2dnbGUiOmZhbHNlfQ%3D%3D – KaiH Sep 26 '21 at 07:44
  • @KaiH I updated the code, you can try it again with the `function isValidURL(string)`. Please let me know if it works or not. – Tuan Dao Sep 26 '21 at 08:23
  • Still no luck unfortunately, having the same issue. – KaiH Sep 26 '21 at 09:02
  • @KaiH could you show that code for me? – Tuan Dao Sep 26 '21 at 09:03
  • What would be the best way to show you? I have used the same code as your second example, updated for use in Wordpress, but with the `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)` after the `$("a").click(function(event) { addAffiliate(this); });` – KaiH Sep 26 '21 at 10:14
  • @KaiH can you add your website URL here? – Tuan Dao Sep 26 '21 at 11:31
  • Here is a page of my website with a button to trigger a pop-up, "add custom gift": https://simpleregistry.com.au/gift-ideas/ – KaiH Sep 26 '21 at 11:42
  • @KaiH I add `redirectUrl.indexOf(window.location.hostname)` in the redirect condition. I've tested on your site. Hope it helps you. – Tuan Dao Sep 26 '21 at 13:17
  • Thank you very much for the help. This has fixed the popups, however the affiliate URLs are no longer prepended when clicking on links. – KaiH Sep 27 '21 at 10:46
  • @KaiH Updated the condition, `redirectUrl.indexOf(window.location.hostname) > 0`. Please try to use it. Hope it helps you. – Tuan Dao Sep 27 '21 at 13:21
  • Still having the same issue, fixed popup issue but no prepended affiliate URLs. – KaiH Sep 27 '21 at 15:05
  • @KaiH updated another version. – Tuan Dao Sep 27 '21 at 15:23
  • No change, unfortunately. – KaiH Sep 27 '21 at 22:07