Skip to content

Commit c591a62

Browse files
committed
fix(params): not all marked params get deleted
1 parent 510d7c5 commit c591a62

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

__tests__/removeTrackingParamsFromLinks.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ test("removes utm_content", (t) => {
3434
);
3535
});
3636

37+
test.only("removes whole utm bunch", (t) => {
38+
t.is(
39+
removeTrackingParamsFromLinks(
40+
"https://www.test.com/?partner=rss&utm_source=rss&utm_campaign=rss+test&utm_medium=feed&utm_content=rss"
41+
),
42+
"https://www.test.com/?partner=rss"
43+
);
44+
});
45+
46+
test("removes whole piwik bunch", (t) => {
47+
t.is(
48+
removeTrackingParamsFromLinks(
49+
"https://www.test.com/?piwik_kwd=rss&piwik_campaign=rss+test"
50+
),
51+
"https://www.test.com/"
52+
);
53+
});
54+
3755
test("removes fbclid", (t) => {
3856
t.is(
3957
removeTrackingParamsFromLinks("https://www.test.com/?fbclid=something"),

index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ const TRACKING_PARAMS = require("./data/params");
33
module.exports = function removeParams($link, allowlist = []) {
44
const url = new URL($link);
55

6-
for (const [key] of url.searchParams) {
6+
const toDelete = [];
7+
8+
for (const [key] of url.searchParams.entries()) {
79
const matchedParam = TRACKING_PARAMS.find(({ name }) => name === key);
810

911
if (matchedParam && !allowlist.includes(matchedParam.company)) {
10-
url.searchParams.delete(key);
12+
// don't delete immediately
13+
// otherwise the loop would skip over the next entry
14+
toDelete.push(key);
1115
}
1216
}
1317

18+
toDelete.forEach((key) => url.searchParams.delete(key));
19+
1420
return url.toString();
1521
};

0 commit comments

Comments
 (0)