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.

14.6 KB
Raw Blame
import { nFormatter } from "../../functions";
import { INFINITY_NUMBER } from "../misc";

export type PlanFeature = {
  id?: string;
  text: string;
  tooltip?: {
    title: string;
    cta?: string;
    href?: string;
  };
};

export type PlanDetails = {
  name: string;
  price: {
    monthly: number | null;
    yearly: number | null;
    ids?: string[];
  };
  limits: {
    links: number;
    clicks: number;
    domains: number;
    tags: number;
    folders: number;
    users: number;
    ai: number;
    api: number;
    analyticsApi: number;
    retention: string;
  };
  tiers?: {
    [key: number]: {
      price: {
        monthly: number | null;
        yearly: number | null;
        ids: string[];
      };
      limits: {
        links: number;
        clicks: number;
      };
    };
  };
  featureTitle?: string;
  features?: PlanFeature[];
};

const LEGACY_PRO_PRICE_IDS = [
  "price_1LodNLAlJJEpqkPVQSrt33Lc", // monthly 2023
  "price_1LodNLAlJJEpqkPVRxUyCQgZ", // yearly 2023
  "price_1OTcQBAlJJEpqkPViGtGEsbb", // monthly 2024 (test)
  "price_1OYJeBAlJJEpqkPVLjTsjX0E", // monthly 2024 (prod)
  "price_1OTcQBAlJJEpqkPVYlCMqdLL", // yearly 2024 (test)
  "price_1OYJeBAlJJEpqkPVnPGEZeb0", // yearly 2024 (prod)
];

const PRO_TIER_PRICE_IDS = {
  2: [
    "price_1SQtg3AlJJEpqkPVVhDSyd9u", // yearly 2025 (prod)
    "price_1SQtg3AlJJEpqkPVNHYhTRy7", // monthly 2025 (prod)
    "price_1SQ8hiAlJJEpqkPVIy8pfvAC", // yearly 2025 (test)
    "price_1TrQKGAlJJEpqkPVXz5gGere", // monthly 2025 (test)
    "price_1TrSHvAlJJEpqkPVOv8okJ4L", // yearly 2026 (prod)
    "price_1TrOzqAlJJEpqkPVXBwEbPo4", // yearly 2026 (test)
  ],
};

const NEW_PRO_PRICE_IDS = [
  "price_1R8XtyAlJJEpqkPV5WZ4c0jF", //  yearly 2025
  "price_1R8XtEAlJJEpqkPV4opVvVPq", // monthly 2025 (prod)
  "price_1R8XxZAlJJEpqkPVqGi0wOqD", // yearly 2025 (test),
  "price_1R7oeBAlJJEpqkPVh6q5q3h8", // monthly 2025 (test),
  "price_1TrSHYAlJJEpqkPVtJ0mnJMQ", // yearly 2026 (prod)
  "price_1TrOyDAlJJEpqkPVxoOc1eet", // yearly 2026 (test)
  ...Object.values(PRO_TIER_PRICE_IDS).flat(),
];

const LEGACY_BUSINESS_PRICE_IDS = [
  "price_1LodLoAlJJEpqkPV9rD0rlNL", // monthly 2023
  "price_1OZgmnAlJJEpqkPVOj4kV64R", // yearly 2023
  "price_1OzNlmAlJJEpqkPV7s9HXNAC", // monthly 2024 (test)
  "price_1OzNmXAlJJEpqkPVYO89lTdx", // yearly 2024 (test)
  "price_1OzOFIAlJJEpqkPVJxzc9irl", // monthly 2024 (prod)
  "price_1OzOXMAlJJEpqkPV9ERrjjbw", // yearly 2024 (prod)
];

const BUSINESS_TIER_PRICE_IDS = {
  2: [
    "price_1SQtdxAlJJEpqkPV4kNkRHZr", // yearly 2025 (prod)
    "price_1SQtdxAlJJEpqkPV7cvJTv4g", // monthly 2025 (prod)
    "price_1SQ8iwAlJJEpqkPVEGmKd6Lg", // yearly 2025 (test)
    "price_1TrQMNAlJJEpqkPVREg73wkW", // monthly 2025 (test)
    "price_1TrSJkAlJJEpqkPVtUyuZ7Z2", // yearly 2026 (prod)
    "price_1TrQGrAlJJEpqkPVmBqGRdsP", // yearly 2026 (test)
  ],
};

export const NEW_BUSINESS_PRICE_IDS = [
  "price_1R3j01AlJJEpqkPVXuG1eNzm", //  yearly 2025 (prod)
  "price_1R6JedAlJJEpqkPVMUkfjch4", // monthly 2025 (prod)
  "price_1R8XypAlJJEpqkPVdjzOcYUC", // yearly 2025 (test),
  "price_1R7ofLAlJJEpqkPV3MlgDpyx", // monthly 2025 (test),
  "price_1TrSIiAlJJEpqkPVUNjLPUxY", // yearly 2026 (prod)
  "price_1TrQBOAlJJEpqkPVCOGYIwVy", // yearly 2026 (test)
  ...Object.values(BUSINESS_TIER_PRICE_IDS).flat(),
];

const ADVANCED_TIER_PRICE_IDS = {
  2: [
    "price_1SQtg6AlJJEpqkPVAJdrStq7", // yearly 2025 (prod)
    "price_1SQtg6AlJJEpqkPVaZNisQdm", // monthly 2025 (prod)
    "price_1SQ8jlAlJJEpqkPV6EanvSXl", // yearly 2025 (test)
    "price_1TrQU1AlJJEpqkPVmgLd5cR1", // monthly 2025 (test)
    "price_1TrSKzAlJJEpqkPVHkhc8cUx", // yearly 2026 (prod)
    "price_1TrQTSAlJJEpqkPV1cqueTie", // yearly 2026 (test)
  ],
  3: [
    "price_1SQtg8AlJJEpqkPVvQVU7uQ3", // yearly 2025 (prod)
    "price_1SQtg8AlJJEpqkPV4Nks8MkS", // monthly 2025 (prod)
    "price_1SQ8lqAlJJEpqkPVzBaioV3I", // yearly 2025 (test)
    "price_1TrQXfAlJJEpqkPV7RXjqb74", // monthly 2025 (test)
    "price_1TrSLRAlJJEpqkPVOHrNc2hI", // yearly 2026 (prod)
    "price_1TrQXMAlJJEpqkPVIzWw9iHW", // yearly 2026 (test)
  ],
};

const ADVANCED_PRICE_IDS = [
  "price_1R8Xw4AlJJEpqkPV6nwdink9", //  yearly 2025 (prod)
  "price_1R3j0qAlJJEpqkPVkfGNXRwb", // monthly 2025 (prod)
  "price_1R8XztAlJJEpqkPVnHmIU2tf", // yearly 2025 (test),
  "price_1R7ofzAlJJEpqkPV0L2TwyJo", // monthly 2025 (test),
  "price_1TrSKLAlJJEpqkPV2QG2AFUQ", // yearly 2026 (prod)
  "price_1TrQN3AlJJEpqkPVqX1Rt9el", // yearly 2026 (test)
  ...Object.values(ADVANCED_TIER_PRICE_IDS).flat(),
];

export const PLANS: PlanDetails[] = [
  {
    name: "Free",
    price: {
      monthly: 0,
      yearly: 0,
    },
    limits: {
      links: 25,
      clicks: 1_000,
      domains: 3,
      tags: 5,
      folders: 0,
      users: 1,
      ai: 10,
      api: 60,
      analyticsApi: 0, // analytics API is not available on the Free plan
      retention: "30-day",
    },
  },
  {
    name: "Pro",
    price: {
      monthly: 30,
      yearly: 27,
      ids: [...LEGACY_PRO_PRICE_IDS, ...NEW_PRO_PRICE_IDS],
    },
    limits: {
      links: 1_000,
      clicks: 50_000,
      domains: 10,
      tags: 25,
      folders: 3,
      users: 3,
      ai: 1_000,
      api: 600,
      analyticsApi: 2,
      retention: "1-year",
    },
    tiers: {
      2: {
        price: {
          monthly: 60,
          yearly: 54,
          ids: PRO_TIER_PRICE_IDS[2],
        },
        limits: {
          links: 5_000,
          clicks: 150_000,
        },
      },
    },
    featureTitle: "Everything in Free, plus:",
    features: [
      { id: "clicks", text: "50K tracked events/mo" },
      { id: "links", text: "1K new links/mo" },
      { id: "retention", text: "1-year analytics retention" },
      { id: "domains", text: "10 domains" },
      { id: "users", text: "3 users" },
      {
        id: "advanced",
        text: "Advanced link features",
        tooltip: "ADVANCED_LINK_FEATURES",
      },
      {
        id: "ai",
        text: "Unlimited AI credits",
        tooltip: {
          title:
            "Subject to fair use policy – you will be notified if you exceed the limit, which are high enough for frequent usage.",
          cta: "Learn more.",
          href: "https://dub.co/blog/introducing-dub-ai",
        },
      },
      {
        id: "dotlink",
        text: "Free .link domain",
        tooltip: {
          title:
            "All our paid plans come with a free .link custom domain, which helps improve click-through rates.",
          cta: "Learn more.",
          href: "https://dub.co/help/article/free-dot-link-domain",
        },
      },
      {
        id: "folders",
        text: "Link folders",
        tooltip: {
          title:
            "Organize and manage access to your links on Dub using folders.",
          cta: "Learn more.",
          href: "https://dub.co/help/article/link-folders",
        },
      },
      {
        id: "deeplinks",
        text: "Deep links",
        tooltip: {
          title:
            "Redirect users to a specific page within your mobile application using deep links.",
          cta: "Learn more.",
          href: "https://dub.co/docs/concepts/deep-links/quickstart",
        },
      },
    ] as PlanFeature[],
  },
  {
    name: "Business",
    price: {
      monthly: 90,
      yearly: 81,
      ids: [...LEGACY_BUSINESS_PRICE_IDS, ...NEW_BUSINESS_PRICE_IDS],
    },
    limits: {
      links: 10_000,
      clicks: 250_000,
      domains: 100,
      tags: INFINITY_NUMBER,
      folders: 20,
      users: 10,
      ai: 1_000,
      api: 1_200,
      analyticsApi: 4,
      retention: "3-year",
    },
    tiers: {
      2: {
        price: {
          monthly: 180,
          yearly: 162,
          ids: BUSINESS_TIER_PRICE_IDS[2],
        },
        limits: {
          links: 25_000,
          clicks: 600_000,
        },
      },
    },
    featureTitle: "Everything in Pro, plus:",
    features: [
      {
        id: "clicks",
        text: "250K tracked events/mo",
      },
      {
        id: "links",
        text: "10K new links/mo",
      },
      {
        id: "retention",
        text: "3-year analytics retention",
      },
      {
        id: "users",
        text: "10 users",
      },
      {
        id: "customerinsights",
        text: "Customer insights",
        tooltip: {
          title:
            "Get real-time insights into your customers' behavior and preferences.",
          cta: "Learn more.",
          href: "https://dub.co/help/article/customer-insights",
        },
      },
      {
        id: "events",
        text: "Real-time events stream",
        tooltip: {
          title:
            "Get more data on your link clicks and QR code scans with a detailed, real-time stream of events in your workspace",
          cta: "Learn more.",
          href: "https://dub.co/help/article/real-time-events-stream",
        },
      },
      {
        id: "webhooks",
        text: "Event webhooks",
        tooltip: {
          title:
            "Get real-time notifications when a link is clicked or a QR code is scanned using webhooks.",
          cta: "Learn more.",
          href: "https://dub.co/docs/webhooks/introduction",
        },
      },
      {
        id: "tests",
        text: "A/B testing",
      },
    ] as PlanFeature[],
  },
  {
    name: "Advanced",
    price: {
      monthly: 300,
      yearly: 270,
      ids: ADVANCED_PRICE_IDS,
    },
    limits: {
      links: 50_000,
      clicks: 1_000_000,
      domains: 250,
      tags: INFINITY_NUMBER,
      folders: 50,
      users: 20,
      ai: 1_000,
      api: 3_000,
      analyticsApi: 8,
      retention: "5-year",
    },
    tiers: {
      2: {
        price: {
          monthly: 600,
          yearly: 540,
          ids: ADVANCED_TIER_PRICE_IDS[2],
        },
        limits: {
          links: 150_000,
          clicks: 2_000_000,
        },
      },
      3: {
        price: {
          monthly: 900,
          yearly: 810,
          ids: ADVANCED_TIER_PRICE_IDS[3],
        },
        limits: {
          links: 300_000,
          clicks: 3_500_000,
        },
      },
    },
    featureTitle: "Everything in Business, plus:",
    features: [
      {
        id: "clicks",
        text: "1M tracked events/mo",
      },
      {
        id: "links",
        text: "50K new links/mo",
      },
      {
        id: "retention",
        text: "5-year analytics retention",
      },
      {
        id: "users",
        text: "20 users",
      },
      {
        id: "slack",
        text: "Priority email support",
      },
    ] as PlanFeature[],
  },
  {
    name: "Enterprise",
    price: {
      monthly: null,
      yearly: null,
    },
    limits: {
      links: 500_000,
      clicks: 5_000_000,
      domains: 250,
      tags: INFINITY_NUMBER,
      folders: INFINITY_NUMBER,
      users: 30,
      ai: 1_000,
      api: 3_000,
      analyticsApi: 16,
      retention: "Unlimited",
    },
  },
];

export const FREE_PLAN = PLANS.find((plan) => plan.name === "Free")!;
export const PRO_PLAN = PLANS.find((plan) => plan.name === "Pro")!;
export const BUSINESS_PLAN = PLANS.find((plan) => plan.name === "Business")!;
export const ADVANCED_PLAN = PLANS.find((plan) => plan.name === "Advanced")!;
export const ENTERPRISE_PLAN = PLANS.find(
  (plan) => plan.name === "Enterprise",
)!;

export const SELF_SERVE_PAID_PLANS = PLANS.filter((p) =>
  ["Pro", "Business", "Advanced"].includes(p.name),
);

export const FREE_WORKSPACES_LIMIT = 2;

const enrichPlanWithTierData = (
  planDetails: PlanDetails,
  planTier: number,
): PlanDetails => {
  const tierData =
    planDetails.tiers && planTier > 1 ? planDetails.tiers[planTier] : undefined;
  const tierLimits = tierData?.limits ?? planDetails.limits;

  return {
    ...planDetails,
    limits: {
      ...planDetails.limits,
      ...tierLimits,
    },
    price: {
      ...planDetails.price,
      ...tierData?.price,
    },
    features: planDetails.features?.map((feature) => ({
      ...feature,
      text:
        feature.id === "clicks"
          ? `${nFormatter(tierLimits.clicks)} tracked events/mo`
          : feature.id === "links"
            ? `${nFormatter(tierLimits.links)} new links/mo`
            : feature.text,
    })),
  };
};

export const getPlanAndTierFromPriceId = ({
  priceId,
}: {
  priceId: string;
}): { plan: PlanDetails | null; planTier: number } => {
  const planDetails = PLANS.find((plan) => plan.price.ids?.includes(priceId));
  if (!planDetails) return { plan: null, planTier: 1 };

  const planTier = planDetails.tiers
    ? Number(
        Object.entries(planDetails.tiers).find(([_, { price }]) =>
          price.ids.includes(priceId),
        )?.[0],
      ) || 1
    : 1;

  return {
    plan: enrichPlanWithTierData(planDetails, planTier),
    planTier,
  };
};

export const getPlanDetails = ({
  plan,
  planTier = 1,
}: {
  plan: string;
  planTier?: number;
}) => {
  const planDetails = PLANS.find(
    // to account for old Business plans (e.g. "Business Plus")
    (p) => p.name.toLowerCase() === plan.split(" ")[0].toLowerCase(),
  )!;

  return enrichPlanWithTierData(planDetails, planTier);
};

export const getNextPlan = (plan?: string | null) => {
  if (!plan) return PRO_PLAN;
  // to account for old Business plans (e.g. "Business Plus")
  const currentPlan = plan.toLowerCase().split(" ")[0];
  return PLANS[
    Math.min(
      // returns the next plan, or the last plan if the current plan is the last plan
      PLANS.findIndex((p) => p.name.toLowerCase() === currentPlan) + 1,
      PLANS.length - 1,
    )
  ];
};

export const isDowngradePlan = ({
  currentPlan,
  newPlan,
  currentTier,
  newTier,
}: {
  currentPlan: string;
  newPlan: string;
  currentTier?: number;
  newTier?: number;
}) => {
  const currentPlanIndex = PLANS.findIndex(
    (p) => p.name.toLowerCase() === currentPlan.toLowerCase(),
  );
  const newPlanIndex = PLANS.findIndex(
    (p) => p.name.toLowerCase() === newPlan.toLowerCase(),
  );
  return (
    currentPlanIndex > newPlanIndex ||
    (currentPlanIndex === newPlanIndex && (currentTier ?? 1) > (newTier ?? 1))
  );
};

export const getSuggestedPlan = ({
  events,
  links,
}: {
  events?: number;
  links?: number;
}): { plan: PlanDetails; planTier: number } => {
  let match: { plan: PlanDetails; planTier: number } | null = null;

  for (const p of PLANS) {
    const matchingTier = [
      1,
      ...Object.keys(p.tiers ?? {})
        .map(Number)
        .filter((tier) => tier >= 2),
    ].find((tier) => {
      const limits =
        tier === 1 ? p.limits : p.tiers?.[tier]?.limits ?? p.limits;
      return limits.clicks >= (events ?? 0) && limits.links >= (links ?? 0);
    });

    if (matchingTier !== undefined) {
      match = {
        plan: enrichPlanWithTierData(p, matchingTier),
        planTier: matchingTier,
      };

      break;
    }
  }

  return match ?? { plan: ENTERPRISE_PLAN, planTier: 1 };
};