On this page

I write a newsletter on TailwindWeekly.com where I curate links for developers who like Tailwind CSS and use various sources to get content.

Some of those sources are other newsletters and usually those newsletters add UTM parameters to their links.

I usually delete these parameters and replace them with my own, but sometimes, I miss some. So, I wrote this little snippet as a fallback when that happens.

document.querySelectorAll('a').forEach((a) => {
	if (a.hostname && a.hostname !== location.hostname) {
		const url = new URL(a.href);
		const params = new URLSearchParams(url.search);

		['utm_source', 'utm_campaign', 'utm_medium', 'utm_term', 'utm_content'].forEach(param => params.delete(param));

		params.append('utm_campaign', 'Tailwind+Weekly+Web+Issue');
		params.append('utm_source', 'Tailwind+Weekly+Website');

		url.search = params.toString();

		a.href = url.toString();
	}
})

The snippet goes through all external links on the page, checks if a link already has UTM parameters, and replaces them with the data I want.