UTM Link
vinkas-ui/utm-linkFreeComponent
A link component that automatically adds UTM parameters to the href on click
Live preview
live · rendered by the registry
Rendered by vinkas-ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://ui.vinkas.com/r/utm-link.jsonSource
1"use client"23import Link from "next/link"45type UTMLinkProps = {6 href: string7 children: React.ReactNode8 source?: string9 medium?: string10 campaign?: string11} & React.ComponentProps<typeof Link>1213export default function UTMLink({14 href,15 children,16 source,17 medium,18 campaign,19 onMouseDown,20 onClick,21 ...props22}: UTMLinkProps) {23 const applySearchParams = (e: React.MouseEvent<HTMLAnchorElement>) => {24 if (e.defaultPrevented) return25 if (e.button !== 0 && e.type !== "click") return26 if (!source && !medium && !campaign) return2728 const hrefAttr = e.currentTarget.getAttribute("href")29 if (!hrefAttr || hrefAttr.startsWith("#")) {30 return31 }3233 const url = new URL(e.currentTarget.href)3435 if (url.protocol !== "http:" && url.protocol !== "https:") return3637 if (38 (!source || url.searchParams.has("utm_source")) &&39 (!medium || url.searchParams.has("utm_medium")) &&40 (!campaign || url.searchParams.has("utm_campaign"))41 ) {42 return43 }4445 if (source && !url.searchParams.has("utm_source")) {46 url.searchParams.set("utm_source", source)47 }48 if (medium && !url.searchParams.has("utm_medium")) {49 url.searchParams.set("utm_medium", medium)50 }51 if (campaign && !url.searchParams.has("utm_campaign")) {52 url.searchParams.set("utm_campaign", campaign)53 }5455 e.currentTarget.href = url.toString()56 }5758 return (59 <Link60 href={href}61 onMouseDown={(e) => {62 onMouseDown?.(e)63 applySearchParams(e)64 }}65 onClick={(e) => {66 onClick?.(e)67 if (e.detail === 0) applySearchParams(e)68 }}69 {...props}>70 {children}71 </Link>72 )73}
Files it writes
More from vinkas-ui
All 7 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Copyright Textcopyright-text | vinkas-ui | Components | Free | no deps | |
| Input Copyableinput-copyable | vinkas-ui | Components | Free | 1 dep | |
| Markdown Textmarkdown-text | vinkas-ui | Components | Free | 1 dep | |
| Relative Timerelative-time | vinkas-ui | Components | Free | no deps · 2 files |
