Skip to content

Instantly share code, notes, and snippets.

@stevestmartin
Last active April 10, 2019 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevestmartin/7dcd7766c8bbc55e40ae151824f6964f to your computer and use it in GitHub Desktop.
Save stevestmartin/7dcd7766c8bbc55e40ae151824f6964f to your computer and use it in GitHub Desktop.
var ignoredTabs = {};
var ignoredHosts = {};
var requestAllowance = 10000;
var standdownPeriod = 3600000;
// only a partial list (we currently have a few dozen).
var affiliateHosts = [
".ojrq.net",
".evyy.net",
"affiliate.rakuten.com",
"linksynergy"
];
safari.application.addEventListener("beforeNavigate", function (e) {
// check each navigation event to see if it is from an affiliate channel
// if it is then store the tab the redirected happened in with the time
// till it expires (we no longer consider the page that you landed on having
// originated from an affiliate source)
if(isAffiliateTraffic(e.url)) {
ignoredTabs[e.target] = e.timeStamp + requestAllowance;
}
});
safari.application.addEventListener("navigate", function(e) {
// check that the page you landed on is not an affiliate link or came
// through a redirect chain that contained an affiliate link.
if(ignoredTabs[e.target] || isAffiliateTraffic(e.url)) {
ignoredTabs[e.target] = e.timeStamp + requestAllowance;
// ignore the host for a period of time if it came through an affiliate
// redirect chain.
var host = new URL(e.url).host;
ignoredHosts[host] = e.timeStamp + standdownPeriod;
}
var tabIgnored = ignoredTabs[e.target] && ignoredTabs[e.target] > e.timeStamp ? true : false;
var hostIgnored = ignoredHosts[host] && ignoredHosts[host] > e.timeStamp ? true : false;
if(!tabIgnored && !hostIgnored) {
// logic here
}
});
function isAffiliateTraffic(url) {
var parsedURL = new URL(url, true);
// check for afsrc and u1 parameters these are an industry standard.
if (parsedURL.query.afsrc || parsedURL.query.u1) {
return true;
} else {
// check paid_links config file to see if host name contains a match
// can be a full or partial host name or just a keyword/phrase within it
for (var i = 0; i < affiliateHosts.length; i++) {
if (parsedURL.host.indexOf(affiliateHosts[i]) >= 0) {
return true;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment