-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkShim.dev.js
47 lines (41 loc) · 1.3 KB
/
LinkShim.dev.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
44
45
46
47
var LinkShim = {
// Configuration options
redirect_url: "http://localhost:8888/r?",
tracker_attr: "data-track",
params: {},
changeHref: function(a) {
var $a = $(a);
var params = this.params;
var url = this.redirect_url;
// Add any tracking attributes to the URL
var trackData = $a.attr(this.tracker_attr);
if (trackData) {
url += trackData + "&";
}
// Build params
params.href = $a.attr('href');
for (var f in params) {
url += f + "=" + encodeURIComponent(params[f]) + "&";
}
// Trim the trailing '&' and set the new URL
url = url.slice(0, -1);
$a.attr('href', url);
},
// Manually call to add PageParams
addPageParams: function(new_params) {
if (typeof new_params == 'object') {
for (var f in new_params) {
this.params[f] = new_params[f];
}
}
},
// Call to listen to clicks on objects that contain the tracker_attr
init: function() {
var obj = this;
$("a[" + obj.tracker_attr + "]").on('click', function(e) {
e.preventDefault();
obj.changeHref(this);
window.location = $(this).attr('href');
});
}
};