Last active
July 22, 2024 14:41
-
-
Save amdrew/44322e0c35ef307be1bc to your computer and use it in GitHub Desktop.
Passes affiliate referral parameters from the URL of the static HTML page and automatically appends them to any URLs that match the target URL (where AffiliateWP is installed) defined in the JavaScript. Also stores the value in a cookie so will work across multiple static HTML pages on the same site (each page must have the same script). Current…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width" /> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Landing page</title> | |
</head> | |
<body> | |
<h1>Landing page</h1> | |
<p> | |
<a href="http://yoursite.com">Test link One (no trailing slash)</a> | |
</p> | |
<p> | |
<a href="http://yoursite.com/">Test link Two (trailing slash)</a> | |
</p> | |
<p> | |
<a href="http://yoursite.com/pricing">Test link Three (links to a page on the site with no trailing slash)</a> | |
</p> | |
<p> | |
<a href="http://yoursite.com/pricing/">Test link Four (links to a page on the site with trailing slash)</a> | |
</p> | |
<!-- jQuery is required --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> | |
<!-- jQuery cookie is required --> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> | |
<script> | |
jQuery(document).ready(function($) { | |
// Configuration options | |
// the referral variable, eg "ref". Must match the referral variable on your site where AffiliateWP is installed | |
var referral_variable = "ref"; | |
// target URL where AffiliateWP is installed | |
var target_url = "http://yoursite.com"; | |
// name of the cookie. Should not match the name of AffiliateWP's cookies | |
var cookie_name = "affwp_ref_value"; | |
// End Configuration options | |
// get the cookie value | |
var cookie = $.cookie( cookie_name ); | |
// get the value of the referral variable from the query string | |
var ref = affiliatewp_arl_get_query_vars()[referral_variable]; | |
// if ref exists but cookie doesn't, set cookie with value of ref | |
if ( ref && ! cookie ) { | |
var cookie_value = ref; | |
// Set the cookie and expire it after 24 hours | |
$.cookie( cookie_name, cookie_value, { expires: 30, path: '/' } ); | |
} | |
// split up the query string and return the parts | |
function affiliatewp_arl_get_query_vars() { | |
var vars = [], hash; | |
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); | |
for (var i = 0; i < hashes.length; i++) { | |
hash = hashes[i].split('='); | |
vars.push(hash[0]); | |
vars[hash[0]] = hash[1]; | |
} | |
return vars; | |
} | |
// the affiliate ID will usually be the value of the cookie, but on first page load we'll grab it from the query string | |
if ( cookie ) { | |
affiliate_id = cookie; | |
} else { | |
affiliate_id = ref; | |
} | |
// affiliate ID exists, change the URLs on the page | |
if ( affiliate_id ) { | |
// get all the targeted URLs on the page that start with the specific URL | |
var target_urls = $('a[href^="' + target_url + '"]'); | |
console.log( target_urls ); | |
// modify each target URL on the page | |
$(target_urls).each( function() { | |
// get the current href of the link | |
current_url = $(this).attr('href'); | |
console.log( updateQueryStringParameter( current_url, referral_variable, affiliate_id ) ); | |
// append a slash to the URL if it doesn't exist | |
current_url = current_url.replace(/\/?$/, '/'); | |
$(this).attr('href', updateQueryStringParameter( current_url, referral_variable, affiliate_id ) ); | |
}); | |
} | |
function updateQueryStringParameter(uri, key, value) { | |
var re = new RegExp("([?|&])" + key + "=.*?(&|#|$)", "i"); | |
if (uri.match(re)) { | |
return uri.replace(re, '$1' + key + "=" + value + '$2'); | |
} else { | |
var hash = ''; | |
var separator = uri.indexOf('?') !== -1 ? "&" : "?"; | |
if( uri.indexOf('#') !== -1 ){ | |
hash = uri.replace(/.*#/, '#'); | |
uri = uri.replace(/#.*/, ''); | |
} | |
return uri + separator + key + "=" + value + hash; | |
} | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment