Last active
May 30, 2024 01:06
-
-
Save johnmurch/3f850df561d872c4577eb0a23e844294 to your computer and use it in GitHub Desktop.
Add UTM to Googlebot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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