-
Notifications
You must be signed in to change notification settings - Fork 26
/
trackers.js
135 lines (111 loc) · 2.6 KB
/
trackers.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
// More detailed string pattern suffixes, stored by their common root.
const TRACKERS_BY_ROOT = {
// Google's Urchin Tracking Module
'utm_': [
'source',
'medium',
'term',
'campaign',
'content',
'name',
'cid',
'reader',
'viz_id',
'pubreferrer',
'swu'
],
// Adobe Omniture SiteCatalyst
'IC': [
'ID'
],
// Adobe Omniture SiteCatalyst
'ic': [
'id'
],
// Hubspot
'_hs': [
'enc',
'mi'
],
// Marketo
'mkt_': [
'tok'
],
// MailChimp
// https://developer.mailchimp.com/documentation/mailchimp/guides/getting-started-with-ecommerce/
'mc_': [
'cid',
'eid'
],
// comScore Digital Analytix?
// http://www.about-digitalanalytics.com/comscore-digital-analytix-url-campaign-generator
'ns_': [
'source',
'mchannel',
'campaign',
'linkname',
'fee'
],
// Simple Reach
'sr_': [
'share'
],
// Vero
'vero_': [
'conv',
'id'
],
// Non-prefixy and 1-offs
'': [
// Facebook Click Identifier
// http://thisinterestsme.com/facebook-fbclid-parameter/
'fbclid',
// Instagram Share Identifier
'igshid',
'srcid',
// Google Click Identifier
'gclid',
// Some other Google Click thing
'ocid',
// Unknown
'ncid',
// Unknown
'nr_email_referer',
// Generic-ish. Facebook, Product Hunt and others
'ref',
// Alibaba-family 'super position model' tracker:
// https://github.com/newhouse/url-tracking-stripper/issues/38
'spm'
]
};
const ALL_TRACKERS = Object.keys(TRACKERS_BY_ROOT).reduce((trackers, root) => {
TRACKERS_BY_ROOT[root].forEach(suffix => trackers.push(root + suffix));
return trackers;
}, []);
const TRACKER_REGEXES_BY_TRACKER = ALL_TRACKERS.reduce((memo, tracker) => {
memo[tracker] = new RegExp("((^|&)" + tracker + "=[^&#]*)", "ig");
return memo;
}, {});
// Actually strip out the tracking codes/parameters from a URL and return the cleansed URL
function removeTrackersFromUrl(url, trackers) {
if (!url) return url;
const urlPieces = url.split('?');
// If no params, nothing to modify
if (urlPieces.length === 1) {
return url;
}
trackers.forEach(tracker => {
urlPieces[1] = urlPieces[1].replace(TRACKER_REGEXES_BY_TRACKER[tracker], '');
});
// If we've collapsed the URL to the point where there's an '&' against the '?'
// then we need to get rid of that.
while (urlPieces[1].charAt(0) === '&') {
urlPieces[1] = urlPieces[1].substr(1);
}
return urlPieces[1] ? urlPieces.join('?') : urlPieces[0];
}
module.exports = {
ALL_TRACKERS,
removeTrackersFromUrl,
};