The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add UTM builder
Tim Wilson0221114b8fa3
diff --git a/apps/web/ui/modals/add-edit-link-modal-new/index.tsx b/apps/web/ui/modals/add-edit-link-modal-new/index.tsxindex f804730a57..09d4304d63 100644--- a/apps/web/ui/modals/add-edit-link-modal-new/index.tsx+++ b/apps/web/ui/modals/add-edit-link-modal-new/index.tsx@@ -41,6 +41,7 @@ import { mutate } from "swr";import { useDebounce } from "use-debounce";import { LinkPreview } from "./link-preview";import { useMetatags } from "./use-metatags";+import { UTMBuilder } from "./utm-builder";export type LinkFormData = LinkWithTagsProps;@@ -331,6 +332,8 @@ function AddEditLinkModalInner({/>)}+ <UTMBuilder />+<div><div className="flex items-center gap-2"><labeldiff --git a/apps/web/ui/modals/add-edit-link-modal-new/utm-builder.tsx b/apps/web/ui/modals/add-edit-link-modal-new/utm-builder.tsxnew file mode 100644index 0000000000..da132b267f--- /dev/null+++ b/apps/web/ui/modals/add-edit-link-modal-new/utm-builder.tsx@@ -0,0 +1,174 @@+import {+ AnimatedSizeContainer,+ InfoTooltip,+ SimpleTooltipContent,+} from "@dub/ui";+import {+ Flag6,+ GlobePointer,+ InputSearch,+ LinkBroken,+ Page2,+ SatelliteDish,+} from "@dub/ui/src/icons";+import { cn, constructURLFromUTMParams, getParamsFromURL } from "@dub/utils";+import { Fragment, useId, useMemo, useState } from "react";+import { useFormContext } from "react-hook-form";+import { LinkFormData } from ".";++const parameters = [+ { key: "ref", icon: LinkBroken, label: "Referral", placeholder: "google" },+ {+ key: "utm_source",+ icon: GlobePointer,+ label: "Source",+ placeholder: "google",+ },+ {+ key: "utm_medium",+ icon: SatelliteDish,+ label: "Medium",+ placeholder: "cpc",+ },+ {+ key: "utm_campaign",+ icon: Flag6,+ label: "Campaign",+ placeholder: "summer_sale",+ },+ {+ key: "utm_term",+ icon: InputSearch,+ label: "Term",+ placeholder: "running shoes",+ },+ {+ key: "utm_content",+ icon: Page2,+ label: "Content",+ placeholder: "logolink",+ },+];++export function UTMBuilder() {+ const id = useId();++ const { watch, setValue } = useFormContext<LinkFormData>();+ const url = watch("url");++ const isValidUrl = useMemo(() => {+ try {+ new URL(url);+ return true;+ } catch (e) {+ return false;+ }+ }, [url]);++ const params = useMemo(() => getParamsFromURL(url), [url]);++ const [toggledParams, setToggledParams] = useState<string[]>([]);++ const enabledParameters = useMemo(() => {+ return parameters.filter(+ ({ key }) => params[key] || toggledParams.includes(key),+ );+ }, [params, toggledParams]);++ return (+ <div>+ <div className="flex items-center gap-2">+ <span className="block text-sm font-medium text-gray-700">+ UTM Builder+ </span>+ <InfoTooltip+ content={+ <SimpleTooltipContent+ title="Add UTM parameters to your short links for conversion tracking."+ cta="Learn more."+ href="https://dub.co/help/article/utm-builder"+ />+ }+ />+ </div>+ <div className="mt-2 flex flex-wrap items-center gap-2">+ {parameters.map(({ key, icon: Icon, label }) => {+ const enabled = Boolean(params[key]) || toggledParams.includes(key);+ return (+ <button+ key={key}+ type="button"+ aria-pressed={enabled}+ disabled={!isValidUrl}+ className={cn(+ "flex items-center gap-1 px-2.5 py-1.5 text-sm drop-shadow-sm transition-all",+ "rounded-md border border-gray-300 bg-white text-gray-700",+ "disabled:pointer-events-none disabled:opacity-70",+ enabled+ ? "border-gray-200 bg-gray-200"+ : "hover:bg-gray-100 active:bg-gray-200",+ )}+ onClick={() => {+ if (!enabled) setToggledParams((prev) => [...prev, key]);+ else {+ setToggledParams((prev) => prev.filter((k) => k !== key));+ setValue(key as any, "");+ setValue(+ "url",+ constructURLFromUTMParams(url, {+ ...params,+ [key]: "",+ }),+ );+ }+ }}+ >+ <Icon className="size-4" />+ <span>{label}</span>+ </button>+ );+ })}+ </div>+ <AnimatedSizeContainer+ height+ transition={{ ease: "easeInOut", duration: 0.1 }}+ className="-mx-1 px-1"+ >+ <div+ className={cn(+ "grid grid-cols-[min-content_1fr] gap-y-2 text-sm text-gray-700",+ enabledParameters.length > 0 && "pb-1 pt-6",+ )}+ >+ {enabledParameters.map(({ key, icon: Icon, label, placeholder }) => {+ return (+ <Fragment key={key}>+ <div className="flex items-center gap-1.5 rounded-l-md border-y border-l border-gray-300 px-3 py-2.5">+ <Icon className="size-4 shrink-0" />+ <label htmlFor={`${id}-${key}`}>{label}</label>+ </div>+ <input+ type="text"+ id={`${id}-${key}`}+ placeholder={placeholder}+ className="h-full grow rounded-r-md border border-gray-300 text-sm placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500"+ value={params[key] || ""}+ onChange={(e) => {+ setValue(key as any, e.target.value);+ setValue(+ "url",+ constructURLFromUTMParams(url, {+ ...params,+ [key]: e.target.value,+ }),+ );+ }}+ />+ </Fragment>+ );+ })}+ </div>+ </AnimatedSizeContainer>+ </div>+ );+}diff --git a/packages/ui/src/icons/nucleo/flag6.tsx b/packages/ui/src/icons/nucleo/flag6.tsxnew file mode 100644index 0000000000..7eaa4ee514--- /dev/null+++ b/packages/ui/src/icons/nucleo/flag6.tsx@@ -0,0 +1,35 @@+import { SVGProps } from "react";++export function Flag6(props: SVGProps<SVGSVGElement>) {+ return (+ <svg+ height="18"+ width="18"+ viewBox="0 0 18 18"+ xmlns="http://www.w3.org/2000/svg"+ {...props}+ >+ <g fill="currentColor">+ <path+ d="M3.75,3c1.784-.232,3.073,.092,4.021,.625,1.641,.922,1.87,2.249,3.5,3.125,1.254,.674,2.66,.719,3.979,.5-2.075,2.554-3.703,3.051-4.833,3-1.433-.064-2.359-1.021-4.125-.792-1.13,.147-1.995,.701-2.542,1.135"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="3.75"+ x2="3.75"+ y1="1.75"+ y2="16.25"+ />+ </g>+ </svg>+ );+}diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.tsindex 5c96ae343e..75566a224c 100644--- a/packages/ui/src/icons/nucleo/index.ts+++ b/packages/ui/src/icons/nucleo/index.ts@@ -37,6 +37,7 @@ export * from "./filter-bars";export * from "./filter2";export * from "./flag-wavy";export * from "./flag2";+export * from "./flag6";export * from "./gaming-console";export * from "./gear";export * from "./gear2";@@ -52,6 +53,7 @@ export * from "./hexadecagon-star";export * from "./hyperlink";export * from "./icosahedron";export * from "./input-password";+export * from "./input-search";export * from "./invoice-dollar";export * from "./key";export * from "./lines-y";@@ -72,6 +74,7 @@ export * from "./receipt2";export * from "./referred-via";export * from "./refresh2";export * from "./robot";+export * from "./satellite-dish";export * from "./scribble";export * from "./shield-check";export * from "./sliders";diff --git a/packages/ui/src/icons/nucleo/input-search.tsx b/packages/ui/src/icons/nucleo/input-search.tsxnew file mode 100644index 0000000000..9f735b7a7f--- /dev/null+++ b/packages/ui/src/icons/nucleo/input-search.tsx@@ -0,0 +1,45 @@+import { SVGProps } from "react";++export function InputSearch(props: SVGProps<SVGSVGElement>) {+ return (+ <svg+ height="18"+ width="18"+ viewBox="0 0 18 18"+ xmlns="http://www.w3.org/2000/svg"+ {...props}+ >+ <g fill="currentColor">+ <path+ d="M8.25,13.25H3.75c-1.105,0-2-.895-2-2V6.75c0-1.105,.895-2,2-2H14.25c1.105,0,2,.895,2,2v1.5"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <circle+ cx="13"+ cy="12"+ fill="none"+ r="2.25"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="14.59"+ x2="16.25"+ y1="13.59"+ y2="15.25"+ />+ </g>+ </svg>+ );+}diff --git a/packages/ui/src/icons/nucleo/satellite-dish.tsx b/packages/ui/src/icons/nucleo/satellite-dish.tsxnew file mode 100644index 0000000000..641c3bb45c--- /dev/null+++ b/packages/ui/src/icons/nucleo/satellite-dish.tsx@@ -0,0 +1,51 @@+import { SVGProps } from "react";++export function SatelliteDish(props: SVGProps<SVGSVGElement>) {+ return (+ <svg+ height="18"+ width="18"+ viewBox="0 0 18 18"+ xmlns="http://www.w3.org/2000/svg"+ {...props}+ >+ <g fill="currentColor">+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="8"+ x2="10"+ y1="10"+ y2="8"+ />+ <path+ d="M10.25,4.75c1.657,0,3,1.343,3,3"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <path+ d="M10.25,1.75c3.314,0,6,2.686,6,6"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <path+ d="M12.419,14.419c-2.441,2.441-6.398,2.441-8.839,0s-2.441-6.398,0-8.839L12.419,14.419Z"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ </g>+ </svg>+ );+}