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.

2.9 KB
Raw Blame
import useWorkspace from "@/lib/swr/use-workspace";
import { LinkFormData } from "@/ui/links/link-builder/link-builder-provider";
import {
  CrownSmall,
  FlaskSmall,
  InfoTooltip,
  Switch,
  TooltipContent,
} from "@dub/ui";
import { memo } from "react";
import { useFormContext, useWatch } from "react-hook-form";
import { useLinkBuilderKeyboardShortcut } from "./use-link-builder-keyboard-shortcut";

// Show new badge for 30 days
const isNew =
  new Date().getTime() - new Date("2025-01-13").getTime() < 30 * 86_400_000;

export const ConversionTrackingToggle = memo(() => {
  const { slug, plan } = useWorkspace();
  const { control, setValue } = useFormContext<LinkFormData>();

  const conversionsEnabled = !!plan && plan !== "free" && plan !== "pro";

  const [trackConversion, testVariants] = useWatch({
    control,
    name: ["trackConversion", "testVariants"],
  });

  // Allow turning conversion tracking off even if the plan no longer supports it
  const isPlanLocked = !conversionsEnabled && !trackConversion;

  useLinkBuilderKeyboardShortcut(
    "c",
    () => setValue("trackConversion", !trackConversion, { shouldDirty: true }),
    { enabled: !isPlanLocked },
  );

  return (
    <label className="flex items-center justify-between">
      <div className="flex items-center gap-2">
        {isNew && (
          <div className="rounded-full border border-green-200 bg-green-100 px-2 py-0.5 text-[0.625rem] uppercase leading-none text-green-900">
            New
          </div>
        )}
        <span className="flex select-none items-center gap-1 text-sm font-medium text-neutral-700">
          Conversion Tracking
          <InfoTooltip
            content={
              "View analytics on conversions from your short links. [Learn more.](https://dub.co/docs/conversions/quickstart)"
            }
          />
        </span>
      </div>
      <Switch
        checked={trackConversion}
        fn={(checked) =>
          setValue("trackConversion", checked, {
            shouldDirty: true,
          })
        }
        disabledTooltip={
          trackConversion && testVariants ? (
            <TooltipContent title="Conversion tracking must be enabled to use A/B testing." />
          ) : isPlanLocked ? (
            <TooltipContent
              title="Conversion tracking is only available on Business plans and above."
              cta="Upgrade to Business"
              href={slug ? `/${slug}/upgrade` : "https://dub.co/pricing"}
              target="_blank"
            />
          ) : undefined
        }
        thumbIcon={
          trackConversion && testVariants ? (
            <span className="flex size-full items-center justify-center">
              <FlaskSmall className="size-2 text-blue-500" />
            </span>
          ) : isPlanLocked ? (
            <CrownSmall className="size-full text-neutral-500" />
          ) : undefined
        }
      />
    </label>
  );
});