forked from paramt/go.param.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
43 lines (35 loc) · 1.08 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function getCSV(file, callback){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
data = Papa.parse(this.responseText).data;
callback(data);
}
};
xhttp.open("GET", file, true);
xhttp.send();
}
function redirect(data){
utm = `utm_source=${config.shortDomain}&utm_campaign=redirect`;
var currentURL = window.location.pathname.substr(1);
var redirectURL = "";
for(var i = 1; i < data.length - 1; i++){
shortURL = data[i][0];
longURL = data[i][1];
if(currentURL == shortURL){
redirectURL = longURL;
utm += "&utm_medium=short-url";
}
}
if(redirectURL === ""){
redirectURL = config.defaultRedirect;
utm += "&utm_medium=catch-all";
}
// Redirect to the correct URL and add a UTM if config.utm is set to true
if(redirectURL.includes("?")){
window.location = (config.utm) ? redirectURL + "&" + utm : redirectURL;
} else {
window.location = (config.utm) ? redirectURL + "?" + utm : redirectURL;
}
}
getCSV(`https://raw.githubusercontent.com/${config.repo}/master/redirects.csv`, redirect)