Skip to content

Instantly share code, notes, and snippets.

@johnmurch
Last active May 30, 2024 01:06
Show Gist options
  • Save johnmurch/3f850df561d872c4577eb0a23e844294 to your computer and use it in GitHub Desktop.
Save johnmurch/3f850df561d872c4577eb0a23e844294 to your computer and use it in GitHub Desktop.
Add UTM to Googlebot
const userAgent = navigator.userAgent;
const googleBotRegex = /Google/i;
if (googleBotRegex.test(userAgent)) {
const url = new URL(window.location.href);
url.searchParams.set('utm_source', 'googlebot');
url.searchParams.set('utm_medium', 'crawler');
url.searchParams.set('utm_campaign', 'bot_campaign');
// Update the URL without reloading the page
window.history.replaceState(null, '', url.toString());
console.log("UTM CHECK", url.toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment