Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

New UTM & Tags implementation in link builder

Steven Tey6fd714489ec7
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index 2978a25ef5..46a690daca 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -16,7 +16,6 @@ import {
TooltipContent,
useKeyboardShortcut,
useRouterStuff,
- UTMBuilder,
} from "@dub/ui";
import { ArrowTurnLeft } from "@dub/ui/src/icons";
import {
@@ -54,9 +53,9 @@ import { MoreDropdown } from "./more-dropdown";
import { OptionsList } from "./options-list";
import { usePasswordModal } from "./password-modal";
import { QRCodePreview } from "./qr-code-preview";
-import { TagSelect } from "./tag-select";
import { useTargetingModal } from "./targeting-modal";
import { useMetatags } from "./use-metatags";
+import { useUTMModal } from "./utm-modal";
export const LinkModalContext = createContext<{
workspaceId?: string;
@@ -207,13 +206,15 @@ function LinkBuilderInner({
const draftControlsRef = useRef<DraftControlsHandle>(null);
- const { PasswordModal, PasswordButton } = usePasswordModal();
- const { TargetingModal, TargetingButton } = useTargetingModal();
+ const { UTMModal, UTMButton } = useUTMModal();
const { ExpirationModal, ExpirationButton } = useExpirationModal();
+ const { TargetingModal, TargetingButton } = useTargetingModal();
+ const { PasswordModal, PasswordButton } = usePasswordModal();
return (
<>
<PasswordModal />
+ <UTMModal />
<TargetingModal />
<ExpirationModal />
<Modal
@@ -411,16 +412,6 @@ function LinkBuilderInner({
/>
)}
- <UTMBuilder
- url={url}
- setUrl={(url) =>
- setValue("url", url, { shouldDirty: true })
- }
- setValue={(key, value) =>
- setValue(key as any, value, { shouldDirty: true })
- }
- />
-
<div>
<div className="flex items-center gap-2">
<label
@@ -473,7 +464,7 @@ function LinkBuilderInner({
</div>
<div className="flex items-center justify-between gap-2 border-t border-gray-100 bg-gray-50 p-4">
<div className="flex min-w-0 items-center gap-2">
- <TagSelect />
+ <UTMButton />
<div className="flex items-center gap-2 max-sm:hidden">
<ExpirationButton />
<TargetingButton />
diff --git a/apps/web/ui/modals/link-builder/multi-tags-icon.tsx b/apps/web/ui/modals/link-builder/multi-tags-icon.tsx
new file mode 100644
index 0000000000..8c82c3421e
--- /dev/null
+++ b/apps/web/ui/modals/link-builder/multi-tags-icon.tsx
@@ -0,0 +1,60 @@
+import { TagProps } from "@/lib/types";
+import { COLORS_LIST } from "@/ui/links/tag-badge";
+import { cn } from "@dub/utils";
+
+export function MultiTagsIcon({ tags }: { tags: Pick<TagProps, "color">[] }) {
+ return (
+ <svg
+ height="18"
+ width="18"
+ viewBox="0 0 18 18"
+ xmlns="http://www.w3.org/2000/svg"
+ className={cn(
+ "size-4 shrink-0",
+ tags.length > 0 &&
+ COLORS_LIST.find(({ color }) => color === tags[0].color)?.css,
+ "bg-transparent",
+ tags.length <= 1 && "-translate-y-px",
+ )}
+ >
+ <g fill="currentColor">
+ {tags.length > 0 && (
+ <path
+ d="M1.75 4.25H7.336C7.601 4.25 7.856 4.355 8.043 4.543L13.836 10.336C14.617 11.117 14.617 12.383 13.836 13.164L10.664 16.336C9.883 17.117 8.617 17.117 7.836 16.336L2.043 10.543C1.855 10.355 1.75 10.101 1.75 9.836V4.25Z"
+ fill="currentColor"
+ fillOpacity={0.15}
+ stroke="none"
+ />
+ )}
+ <path
+ d="M1.75 4.25H7.336C7.601 4.25 7.856 4.355 8.043 4.543L13.836 10.336C14.617 11.117 14.617 12.383 13.836 13.164L10.664 16.336C9.883 17.117 8.617 17.117 7.836 16.336L2.043 10.543C1.855 10.355 1.75 10.101 1.75 9.836V4.25Z"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+
+ {tags.length > 1 && (
+ <path
+ d="M3.25 1.75V1.25H8.836C9.101 1.25 9.356 1.355 9.543 1.543L15.336 7.336C15.768 7.768 15.961 8.348 15.915 8.913"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ className={cn(
+ COLORS_LIST.find(({ color }) => color === tags[1].color)?.css,
+ "bg-transparent",
+ )}
+ />
+ )}
+ <path
+ d="M5.25 9C5.94036 9 6.5 8.44036 6.5 7.75C6.5 7.05964 5.94036 6.5 5.25 6.5C4.55964 6.5 4 7.05964 4 7.75C4 8.44036 4.55964 9 5.25 9Z"
+ fill="currentColor"
+ stroke="none"
+ />
+ </g>
+ </svg>
+ );
+}
diff --git a/apps/web/ui/modals/link-builder/tag-select.tsx b/apps/web/ui/modals/link-builder/tag-select.tsx
index 26c0123fd8..0251b9ce4f 100644
--- a/apps/web/ui/modals/link-builder/tag-select.tsx
+++ b/apps/web/ui/modals/link-builder/tag-select.tsx
@@ -1,13 +1,11 @@
import useTags from "@/lib/swr/use-tags";
-import { TagProps } from "@/lib/types";
-import { COLORS_LIST } from "@/ui/links/tag-badge";
import { Combobox, useKeyboardShortcut } from "@dub/ui";
-import { cn } from "@dub/utils";
import { useContext, useMemo, useState } from "react";
import { useFormContext } from "react-hook-form";
import { toast } from "sonner";
import { mutate } from "swr";
import { LinkFormData, LinkModalContext } from ".";
+import { MultiTagsIcon } from "./multi-tags-icon";
export function TagSelect() {
const { workspaceId } = useContext(LinkModalContext);
@@ -47,7 +45,7 @@ export function TagSelect() {
availableTags?.map((tag) => ({
label: tag.name,
value: tag.id,
- icon: <TagIcon tags={[tag]} />,
+ icon: <MultiTagsIcon tags={[tag]} />,
meta: {
color: tag.color,
},
@@ -78,7 +76,7 @@ export function TagSelect() {
);
}}
options={options}
- icon={<TagIcon tags={selectedTags.map(({ meta }) => meta)} />}
+ icon={<MultiTagsIcon tags={selectedTags.map(({ meta }) => meta)} />}
side="top"
placeholder="Tags"
searchPlaceholder="Search or add tags..."
@@ -99,60 +97,3 @@ export function TagSelect() {
</Combobox>
);
}
-
-function TagIcon({ tags }: { tags: Pick<TagProps, "color">[] }) {
- return (
- <svg
- height="18"
- width="18"
- viewBox="0 0 18 18"
- xmlns="http://www.w3.org/2000/svg"
- className={cn(
- "size-4 shrink-0",
- tags.length > 0 &&
- COLORS_LIST.find(({ color }) => color === tags[0].color)?.css,
- "bg-transparent",
- tags.length <= 1 && "-translate-y-px",
- )}
- >
- <g fill="currentColor">
- {tags.length > 0 && (
- <path
- d="M1.75 4.25H7.336C7.601 4.25 7.856 4.355 8.043 4.543L13.836 10.336C14.617 11.117 14.617 12.383 13.836 13.164L10.664 16.336C9.883 17.117 8.617 17.117 7.836 16.336L2.043 10.543C1.855 10.355 1.75 10.101 1.75 9.836V4.25Z"
- fill="currentColor"
- fillOpacity={0.15}
- stroke="none"
- />
- )}
- <path
- d="M1.75 4.25H7.336C7.601 4.25 7.856 4.355 8.043 4.543L13.836 10.336C14.617 11.117 14.617 12.383 13.836 13.164L10.664 16.336C9.883 17.117 8.617 17.117 7.836 16.336L2.043 10.543C1.855 10.355 1.75 10.101 1.75 9.836V4.25Z"
- fill="none"
- stroke="currentColor"
- strokeLinecap="round"
- strokeLinejoin="round"
- strokeWidth="1.5"
- />
-
- {tags.length > 1 && (
- <path
- d="M3.25 1.75V1.25H8.836C9.101 1.25 9.356 1.355 9.543 1.543L15.336 7.336C15.768 7.768 15.961 8.348 15.915 8.913"
- fill="none"
- stroke="currentColor"
- strokeLinecap="round"
- strokeLinejoin="round"
- strokeWidth="1.5"
- className={cn(
- COLORS_LIST.find(({ color }) => color === tags[1].color)?.css,
- "bg-transparent",
- )}
- />
- )}
- <path
- d="M5.25 9C5.94036 9 6.5 8.44036 6.5 7.75C6.5 7.05964 5.94036 6.5 5.25 6.5C4.55964 6.5 4 7.05964 4 7.75C4 8.44036 4.55964 9 5.25 9Z"
- fill="currentColor"
- stroke="none"
- />
- </g>
- </svg>
- );
-}
diff --git a/apps/web/ui/modals/link-builder/utm-modal.tsx b/apps/web/ui/modals/link-builder/utm-modal.tsx
new file mode 100644
index 0000000000..e1683e33e0
--- /dev/null
+++ b/apps/web/ui/modals/link-builder/utm-modal.tsx
@@ -0,0 +1,258 @@
+import { TagProps } from "@/lib/types";
+import {
+ Button,
+ InfoTooltip,
+ Modal,
+ SimpleTooltipContent,
+ Tooltip,
+ useKeyboardShortcut,
+ useMediaQuery,
+} from "@dub/ui";
+import {
+ Flag6,
+ Gift,
+ GlobePointer,
+ InputSearch,
+ Page2,
+ SatelliteDish,
+} from "@dub/ui/src";
+import {
+ constructURLFromUTMParams,
+ getParamsFromURL,
+ isValidUrl,
+} from "@dub/utils";
+import {
+ Dispatch,
+ SetStateAction,
+ useCallback,
+ useEffect,
+ useId,
+ useMemo,
+ useRef,
+ useState,
+} from "react";
+import { useFormContext } from "react-hook-form";
+import { LinkFormData } from ".";
+import { MultiTagsIcon } from "./multi-tags-icon";
+
+const UTM_PARAMETERS = [
+ {
+ key: "utm_source",
+ icon: GlobePointer,
+ label: "Source",
+ placeholder: "google",
+ description: "Where the traffic is coming from",
+ },
+ {
+ key: "utm_medium",
+ icon: SatelliteDish,
+ label: "Medium",
+ placeholder: "cpc",
+ description: "How the traffic is coming",
+ },
+ {
+ key: "utm_campaign",
+ icon: Flag6,
+ label: "Campaign",
+ placeholder: "summer_sale",
+ description: "The name of the campaign",
+ },
+ {
+ key: "utm_term",
+ icon: InputSearch,
+ label: "Term",
+ placeholder: "running shoes",
+ description: "The term of the campaign",
+ },
+ {
+ key: "utm_content",
+ icon: Page2,
+ label: "Content",
+ placeholder: "logolink",
+ description: "The content of the campaign",
+ },
+ {
+ key: "ref",
+ icon: Gift,
+ label: "Referral",
+ placeholder: "yoursite.com",
+ description: "The referral of the campaign",
+ },
+] as const;
+
+function UTMModal({
+ showUTMModal,
+ setShowUTMModal,
+}: {
+ showUTMModal: boolean;
+ setShowUTMModal: Dispatch<SetStateAction<boolean>>;
+}) {
+ const { isMobile } = useMediaQuery();
+ const id = useId();
+
+ const { watch, setValue } = useFormContext<LinkFormData>();
+
+ const url = watch("url");
+ const enabledParams = useMemo(() => getParamsFromURL(url), [url]);
+
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ // Hacky fix to focus the input automatically, not sure why autoFocus doesn't work here
+ useEffect(() => {
+ if (inputRef.current && !isMobile) {
+ setTimeout(() => {
+ inputRef.current?.focus();
+ }, 10);
+ }
+ }, []);
+
+ return (
+ <Modal
+ showModal={showUTMModal}
+ setShowModal={setShowUTMModal}
+ className="px-5 py-4 sm:max-w-md"
+ >
+ <div className="flex items-center justify-between">
+ <div className="flex items-center gap-2">
+ <h3 className="text-lg font-medium">UTM Builder</h3>
+ <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="max-md:hidden">
+ <Tooltip
+ content={
+ <div className="px-2 py-1 text-xs text-gray-700">
+ Press <strong className="font-medium text-gray-950">U</strong>{" "}
+ to open this quickly
+ </div>
+ }
+ side="right"
+ >
+ <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-gray-200 font-sans text-xs text-gray-950">
+ U
+ </kbd>
+ </Tooltip>
+ </div>
+ </div>
+
+ <div className="grid gap-y-3 py-4">
+ {UTM_PARAMETERS.map(
+ ({ key, icon: Icon, label, placeholder, description }) => {
+ return (
+ <div key={key} className="group relative">
+ <div className="relative z-10 flex">
+ <div className="flex items-center gap-1.5 rounded-l-md border-y border-l border-gray-300 bg-gray-50 px-3 py-1.5 text-gray-700 sm:min-w-28">
+ <Icon className="size-4 shrink-0" />
+ <label htmlFor={`${id}-${key}`} className="text-sm">
+ {label}
+ </label>
+ </div>
+ <input
+ type="text"
+ id={`${id}-${key}`}
+ placeholder={placeholder}
+ disabled={!isValidUrl(url)}
+ className="h-full min-w-0 grow rounded-r-md border border-gray-300 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500 disabled:cursor-not-allowed disabled:bg-gray-100/50 sm:text-sm"
+ value={enabledParams[key] || ""}
+ onChange={(e) => {
+ setValue(
+ "url",
+ constructURLFromUTMParams(url, {
+ ...enabledParams,
+ [key]: e.target.value,
+ }),
+ );
+ }}
+ />
+ </div>
+ <div className="relative mt-1 cursor-default text-xs text-gray-500">
+ <span className="ml-2 rounded-b-md border border-t-0 border-gray-300 bg-gray-50 px-1.5 pb-1 pt-2 font-mono">
+ {key}
+ </span>
+ <p className="ml-2 inline-flex opacity-0 transition-opacity duration-300 group-hover:opacity-100">
+ → {description}
+ </p>
+ </div>
+ </div>
+ );
+ },
+ )}
+ </div>
+
+ {isValidUrl(url) && (
+ <div className="mt-4 grid gap-y-1">
+ <p className="text-sm text-gray-700">Current URL</p>
+ <div className="overflow-scroll break-words rounded-lg border border-gray-200 bg-gray-50 px-4 py-3 font-mono text-xs text-gray-500">
+ {url}
+ </div>
+ </div>
+ )}
+ </Modal>
+ );
+}
+
+function UTMButton({
+ setShowUTMModal,
+}: {
+ setShowUTMModal: Dispatch<SetStateAction<boolean>>;
+}) {
+ const { watch } = useFormContext<LinkFormData>();
+ const url = watch("url");
+ const enabledParams = useMemo(() => getParamsFromURL(url), [url]);
+
+ useKeyboardShortcut("u", () => setShowUTMModal(true), {
+ modal: true,
+ });
+
+ return (
+ <Button
+ variant="secondary"
+ text="UTM Tags"
+ icon={
+ <MultiTagsIcon
+ tags={
+ [
+ enabledParams.utm_source && { color: "blue" },
+ enabledParams.utm_medium && { color: "green" },
+ enabledParams.utm_campaign && { color: "red" },
+ enabledParams.utm_term && { color: "yellow" },
+ enabledParams.utm_content && { color: "purple" },
+ ].filter(Boolean) as { color: TagProps["color"] }[]
+ }
+ />
+ }
+ className="h-9 w-fit px-2.5 font-medium text-gray-700"
+ onClick={() => setShowUTMModal(true)}
+ />
+ );
+}
+
+export function useUTMModal() {
+ const [showUTMModal, setShowUTMModal] = useState(false);
+
+ const UTMModalCallback = useCallback(() => {
+ return (
+ <UTMModal showUTMModal={showUTMModal} setShowUTMModal={setShowUTMModal} />
+ );
+ }, [showUTMModal, setShowUTMModal]);
+
+ const UTMButtonCallback = useCallback(() => {
+ return <UTMButton setShowUTMModal={setShowUTMModal} />;
+ }, [setShowUTMModal]);
+
+ return useMemo(
+ () => ({
+ setShowUTMModal,
+ UTMModal: UTMModalCallback,
+ UTMButton: UTMButtonCallback,
+ }),
+ [setShowUTMModal, UTMModalCallback, UTMButtonCallback],
+ );
+}
diff --git a/packages/ui/src/index.tsx b/packages/ui/src/index.tsx
index 79a7df5469..5d0b014ef8 100644
--- a/packages/ui/src/index.tsx
+++ b/packages/ui/src/index.tsx
@@ -29,7 +29,6 @@ export * from "./switch";
export * from "./table";
export * from "./toggle-group";
export * from "./tooltip";
-export * from "./utm-builder";
// hooks
export * from "./hooks";
diff --git a/packages/ui/src/utm-builder.tsx b/packages/ui/src/utm-builder.tsx
deleted file mode 100644
index 40fb0df374..0000000000
--- a/packages/ui/src/utm-builder.tsx
+++ /dev/null
@@ -1,216 +0,0 @@
-import { cn, constructURLFromUTMParams, getParamsFromURL } from "@dub/utils";
-import {
- Fragment,
- useCallback,
- useEffect,
- useId,
- useMemo,
- useState,
-} from "react";
-import { AnimatedSizeContainer } from "./animated-size-container";
-import { useKeyboardShortcut } from "./hooks";
-import {
- Flag6,
- GlobePointer,
- InputSearch,
- LinkBroken,
- Page2,
- SatelliteDish,
-} from "./icons";
-import { InfoTooltip, SimpleTooltipContent } from "./tooltip";
-
-const parameters = [
- {
- key: "utm_source",
- icon: GlobePointer,
- label: "Source",
- placeholder: "google",
- shortcut: "S",
- },
- {
- key: "utm_medium",
- icon: SatelliteDish,
- label: "Medium",
- placeholder: "cpc",
- shortcut: "M",
- },
- {
- key: "utm_campaign",
- icon: Flag6,
- label: "Campaign",
- placeholder: "summer_sale",
- shortcut: "C",
- },
- {
- key: "utm_term",
- icon: InputSearch,
- label: "Term",
- placeholder: "running shoes",
- shortcut: "T",
- },
- {
- key: "utm_content",
- icon: Page2,
- label: "Content",
- placeholder: "logolink",
- shortcut: "O",
- },
- {
- key: "ref",
- icon: LinkBroken,
- label: "Referral",
- placeholder: "google",
- shortcut: "R",
- },
-] as const;
-
-export function UTMBuilder({
- url,
- setUrl,
- setValue,
-}: {
- url: string;
- setUrl: (url: string) => void;
- setValue: (key: (typeof parameters)[number]["key"], value: string) => void;
-}) {
- const id = useId();
-
- const params = useMemo(() => getParamsFromURL(url), [url]);
-
- const [toggledParams, setToggledParams] = useState<string[]>(
- parameters.filter(({ key }) => params[key]).map(({ key }) => key),
- );
- const [justToggled, setJustToggled] = useState<string | null>(null);
-
- const enabledParameters = useMemo(() => {
- return parameters.filter(
- ({ key }) => params[key] || toggledParams.includes(key),
- );
- }, [params, toggledParams]);
-
- const toggleParameter = useCallback(
- (key: string) => {
- const enabled = Boolean(params[key]) || toggledParams.includes(key);
-
- if (!enabled) {
- setToggledParams((prev) => [...prev, key]);
- setJustToggled(key);
- } else {
- setToggledParams((prev) => prev.filter((k) => k !== key));
- setValue(key as any, "");
- setUrl(
- constructURLFromUTMParams(url, {
- ...params,
- [key]: "",
- }),
- );
- }
- },
- [params, toggledParams, url],
- );
-
- // Focus the input after toggling (with preventScroll to avoid scrolling the AnimatedSizeContainer)
- useEffect(() => {
- if (justToggled) {
- const input = document.getElementById(`${id}-${justToggled}`);
- if (input) input.focus({ preventScroll: true });
- setJustToggled(null);
- }
- }, [justToggled]);
-
- useKeyboardShortcut(
- parameters.map(({ shortcut }) => shortcut),
- (e) => {
- const parameter = parameters.find(({ shortcut }) => shortcut === e.key);
- if (!parameter) return;
- toggleParameter(parameter.key);
- },
- {
- modal: true,
- },
- );
-
- 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}
- 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-600 bg-gray-100 ring-1 ring-inset ring-gray-600"
- : "hover:bg-gray-50 active:bg-gray-100",
- )}
- onClick={() => toggleParameter(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, placeholder }) => {
- return (
- <Fragment key={key}>
- <div className="flex items-center gap-1.5 rounded-l-md border-y border-l border-gray-300 bg-gray-100 py-2.5 pl-2 pr-3 sm:min-w-36">
- <Icon className="size-4 shrink-0" />
- <label htmlFor={`${id}-${key}`} className="font-mono text-sm">
- {key}
- </label>
- </div>
- <input
- type="text"
- id={`${id}-${key}`}
- placeholder={placeholder}
- className="h-full min-w-0 grow rounded-r-md border border-gray-300 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500 sm:text-sm"
- value={params[key] || ""}
- onChange={(e) => {
- setValue(key, e.target.value);
- setUrl(
- constructURLFromUTMParams(url, {
- ...params,
- [key]: e.target.value,
- }),
- );
- }}
- />
- </Fragment>
- );
- })}
- </div>
- </AnimatedSizeContainer>
- </div>
- );
-}