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.

Connect UTM builder to targeting

Tim Wilson5cc4a9975e8a
diff --git a/apps/web/ui/modals/link-builder/constants.ts b/apps/web/ui/modals/link-builder/constants.ts
index c7e8d2364f..50537a502f 100644
--- a/apps/web/ui/modals/link-builder/constants.ts
+++ b/apps/web/ui/modals/link-builder/constants.ts
@@ -2,8 +2,14 @@ import { LinkWithTagsProps } from "@/lib/types";
import {
CircleHalfDottedClock,
Crosshairs3,
+ Flag6,
+ Gift,
+ GlobePointer,
Incognito,
InputPassword,
+ InputSearch,
+ Page2,
+ SatelliteDish,
SquareChart,
WindowSearch,
} from "@dub/ui/src/icons";
@@ -99,3 +105,48 @@ export const MOBILE_MORE_ITEMS = [
type: "modal",
},
];
+
+export 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;
diff --git a/apps/web/ui/modals/link-builder/targeting-modal.tsx b/apps/web/ui/modals/link-builder/targeting-modal.tsx
index e66cb66d9c..a54318fc70 100644
--- a/apps/web/ui/modals/link-builder/targeting-modal.tsx
+++ b/apps/web/ui/modals/link-builder/targeting-modal.tsx
@@ -8,7 +8,13 @@ import {
useKeyboardShortcut,
} from "@dub/ui";
import { Crosshairs3, Trash } from "@dub/ui/src/icons";
-import { cn, COUNTRIES } from "@dub/utils";
+import {
+ cn,
+ constructURLFromUTMParams,
+ COUNTRIES,
+ getParamsFromURL,
+ isValidUrl,
+} from "@dub/utils";
import {
Dispatch,
Fragment,
@@ -20,6 +26,7 @@ import {
} from "react";
import { useForm, useFormContext } from "react-hook-form";
import { LinkFormData } from ".";
+import { UTM_PARAMETERS } from "./constants";
function TargetingModal({
showTargetingModal,
@@ -63,6 +70,24 @@ function TargetingModal({
iosParent || androidParent || Object.keys(geoParent || {}).length > 0,
);
+ // Get UTM parameters from the parent URL that need to be added on blur
+ const getNewParams = useCallback(
+ (targetURL: string) => {
+ if (!targetURL?.trim() || !isValidUrl(targetURL)) return;
+
+ const parentUrl = getValuesParent("url");
+ const parentParams = getParamsFromURL(parentUrl);
+ const targetParams = getParamsFromURL(targetURL);
+
+ const newParams = UTM_PARAMETERS.filter(
+ ({ key }) => parentParams?.[key] && !targetParams?.[key],
+ ).map(({ key }) => [key, parentParams[key]]);
+
+ return newParams.length ? Object.fromEntries(newParams) : null;
+ },
+ [getValuesParent],
+ );
+
return (
<>
<Modal
@@ -136,7 +161,9 @@ function TargetingModal({
<Fragment key={key}>
<div className="z-[1]">
<Combobox
- selected={{ value: key, label: COUNTRIES[key] }}
+ selected={
+ key ? { value: key, label: COUNTRIES[key] } : null
+ }
setSelected={(option) => {
if (!option) return;
const newGeo = {};
@@ -200,6 +227,22 @@ function TargetingModal({
{ shouldDirty: true },
);
}}
+ onBlur={(e) => {
+ const newParams = getNewParams(e.target.value);
+
+ if (newParams)
+ setValue(
+ `geo`,
+ {
+ ...((geo as object) || {}),
+ [key]: constructURLFromUTMParams(
+ value,
+ newParams,
+ ),
+ },
+ { shouldDirty: true },
+ );
+ }}
/>
<div className="pl-1.5">
<Button
@@ -258,7 +301,18 @@ function TargetingModal({
id={`${id}-ios-url`}
placeholder="https://apps.apple.com/app/1611158928"
className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- {...register("ios")}
+ {...register("ios", {
+ onBlur: (e) => {
+ const newParams = getNewParams(e.target.value);
+
+ if (newParams)
+ setValue(
+ "ios",
+ constructURLFromUTMParams(e.target.value, newParams),
+ { shouldDirty: true },
+ );
+ },
+ })}
/>
</div>
</div>
@@ -287,7 +341,18 @@ function TargetingModal({
id={`${id}-android-url`}
placeholder="https://play.google.com/store/apps/details?id=com.disney.disneyplus"
className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- {...register("android")}
+ {...register("android", {
+ onBlur: (e) => {
+ const newParams = getNewParams(e.target.value);
+
+ if (newParams)
+ setValue(
+ "android",
+ constructURLFromUTMParams(e.target.value, newParams),
+ { shouldDirty: true },
+ );
+ },
+ })}
/>
</div>
</div>
diff --git a/apps/web/ui/modals/link-builder/utm-modal.tsx b/apps/web/ui/modals/link-builder/utm-modal.tsx
index 27f4c8079a..2e84556373 100644
--- a/apps/web/ui/modals/link-builder/utm-modal.tsx
+++ b/apps/web/ui/modals/link-builder/utm-modal.tsx
@@ -7,15 +7,7 @@ import {
useKeyboardShortcut,
useMediaQuery,
} from "@dub/ui";
-import {
- DiamondTurnRight,
- Flag6,
- Gift,
- GlobePointer,
- InputSearch,
- Page2,
- SatelliteDish,
-} from "@dub/ui/src";
+import { DiamondTurnRight } from "@dub/ui/src";
import {
cn,
constructURLFromUTMParams,
@@ -34,51 +26,7 @@ import {
} from "react";
import { useForm, useFormContext } from "react-hook-form";
import { LinkFormData } from ".";
-
-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;
+import { UTM_PARAMETERS } from "./constants";
type UTMModalProps = {
showUTMModal: boolean;
@@ -148,17 +96,90 @@ function UTMModalInner({ setShowUTMModal }: UTMModalProps) {
// Whether to display actual URL parameters instead of labels
const [showParams, setShowParams] = useState(false);
+ // Update targeting URL params if they previously matched the same params of the destination URL
+ const updateTargeting = useCallback(
+ (
+ data: Pick<
+ LinkFormData,
+ | "url"
+ | "utm_source"
+ | "utm_medium"
+ | "utm_campaign"
+ | "utm_term"
+ | "utm_content"
+ >,
+ ) => {
+ const [parentUrl, ios, android, geo] = getValuesParent([
+ "url",
+ "ios",
+ "android",
+ "geo",
+ ]);
+
+ const getNewParams = (targetURL: string) => {
+ const parentParams = getParamsFromURL(parentUrl);
+ const targetParams = getParamsFromURL(targetURL);
+
+ const newParams = UTM_PARAMETERS.filter(
+ ({ key }) => parentParams?.[key] === targetParams?.[key],
+ ).map(({ key }) => [key, data[key] ?? ""]);
+
+ return newParams.length ? Object.fromEntries(newParams) : null;
+ };
+
+ // Update iOS and Android URLs
+ Object.entries({ ios, android }).forEach(([target, targetUrl]) => {
+ if (!targetUrl) return;
+ const newParams = getNewParams(targetUrl);
+ if (newParams)
+ setValueParent(
+ target as "ios" | "android",
+ constructURLFromUTMParams(targetUrl, newParams),
+ {
+ shouldDirty: true,
+ },
+ );
+ });
+
+ // Update geo targeting URLs
+ if (geo && Object.keys(geo).length > 0) {
+ const newGeo = Object.entries(geo).reduce((acc, [key, value]) => {
+ if (!key?.trim() || !value?.trim()) return acc;
+
+ const newParams = getNewParams(value);
+ if (!newParams) return acc;
+
+ return {
+ ...acc,
+ [key]: constructURLFromUTMParams(value, newParams),
+ };
+ }, {});
+
+ if (Object.keys(newGeo).length > 0)
+ setValueParent(
+ "geo",
+ { ...(geo as Record<string, string>), ...newGeo },
+ { shouldDirty: true },
+ );
+ }
+ },
+ [],
+ );
+
return (
<form
onSubmit={(e) => {
e.stopPropagation();
handleSubmit((data) => {
+ updateTargeting(data);
+
setValueParent("url", data.url);
UTM_PARAMETERS.filter((p) => p.key !== "ref").forEach((p) =>
setValueParent(p.key as any, data[p.key], {
shouldDirty: true,
}),
);
+
setShowUTMModal(false);
})(e);
}}