Requests | Blesta

Requests

Service Worker/JavaScript Snippet for Global Affiliate Tracking

Michael K shared this idea 4 years ago
Under Consideration

It would be neat to have some type of service worker/JavaScript snippet that can be implemented in websites outside of Blesta so that affiliate tracking can be global.

For instance, say that your Blesta install is at clients.yourdomain.tld, but you don't want to link straight to the client area for affiliate tracking. By implementing a JavaScript snippet or service worker on the main website at yourdomain.tld, you can make sure no matter what link that's shared that the affiliate gets credit for commission (so long as they provide the query parameter at the end of the URL.

Comments (1)

photo
1

The affiliate module should have a way to add certain URLs, and then it displays the corresponding referral links that redirect to the target URLs. Until then, I used Javascript to add affiliate IDs to all links on my site.

See: https://www.blesta.com/forums/index.php?/topic/14805-handling-referrals-with-a-separate-site-for-marketing/#comment-69242

`

<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.onload = 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.href = anchors.href.indexOf('#') != -1 ? anchors.href : anchors.href += anchors.href.indexOf('?') != -1 ? "&a=" + affiliate_id : "?a=" + affiliate_id; } fetch('https://billing.yourdomain.com/order/forms/a/' + affiliate_id, { mode: "no-cors" }); } } </script> `