Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 07/21/2022 in all areas

  1. I am using some Javascript to add the affiliate ID to every link on my main site, so it passes the affiliate ID when they click an order button that links to Blesta. I've tried creating a cookie named affiliate_code, but it seems to have issues across domains or with subdomains, so I'm not sure it's working well yet. I also added a fetch so that the visit counts towards the affiliate's visit count in their stats. Code I add in the header, if anyone wants to try and use it: <script> function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, '\\$&'); var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, ' ')); } window.addEventListener('load', function() { var anchors = document.getElementsByTagName("a"); let affiliate_id = getParameterByName('a'); if (affiliate_id !== null ) { let blestadomain = '.yourdomain.com'; let daystoexpire = 60; const d = new Date(); d.setTime(d.getTime() + (daystoexpire*24*60*60*1000)); document.cookie = "affiliate_code=" + affiliate_id + "; expires=" + d.toUTCString() + "; ;domain=" + blestadomain + ";path=/"; console.log('Set cookie - affiliate ID: ' + affiliate_id + '; expires: ' + d.toUTCString() + '; domain: ' + blestadomain); for (var i = 0; i < anchors.length; i++) { anchors[i].href = anchors[i].href.indexOf('#') != -1 ? anchors[i].href : anchors[i].href += anchors[i].href.indexOf('?') != -1 ? "&a=" + affiliate_id : "?a=" + affiliate_id; } fetch('https://billing.yourdomain.com/order/forms/a/' + affiliate_id, { mode: "no-cors" }); } }); </script> EDIT: Changed window.load to window.addEventListener because window.load was unreliable on some sites for some reason.
    1 point
×
×
  • Create New...