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.

build tracking setup prompt

Pedro Ladeira20ff62dc250c
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/installation-section.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/installation-section.tsx
index 95290c7368..bfad318343 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/installation-section.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/installation-section.tsx
@@ -2,6 +2,8 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { useWorkspaceStore } from "@/lib/swr/use-workspace-store";
+import { buildTrackingSetup } from "@/lib/tracking/build-tracking-setup";
+import { useMemo } from "react";
import { SectionCard } from "./section-card";
import {
DeveloperGuides,
@@ -12,25 +14,56 @@ import { TrackingSettingsRow } from "./tracking-settings-row";
import { VerifyInstall } from "./verify-install";
export function InstallationSection() {
- const { allowedHostnames } = useWorkspace();
+ const { allowedHostnames, publishableKey } = useWorkspace();
const [savedStack] = useWorkspaceStore<string[]>(
"analyticsSettingsSelectedStack",
);
+ const [siteVisitEnabled] = useWorkspaceStore<boolean>(
+ "analyticsSettingsSiteVisitTrackingEnabled",
+ );
+ const [outboundEnabled] = useWorkspaceStore<boolean>(
+ "analyticsSettingsOutboundDomainTrackingEnabled",
+ );
+ const [conversionEnabled] = useWorkspaceStore<boolean>(
+ "analyticsSettingsConversionTrackingEnabled",
+ );
const stack = savedStack ?? [];
const hostnames = allowedHostnames ?? [];
const ready = isSetupInstructionsReady(stack, hostnames.length > 0);
+ const setup = useMemo(
+ () =>
+ buildTrackingSetup({
+ stack,
+ hostnames,
+ publishableKey: publishableKey ?? null,
+ siteVisitEnabled: Boolean(siteVisitEnabled),
+ outboundEnabled: Boolean(outboundEnabled),
+ conversionEnabled: Boolean(conversionEnabled),
+ }),
+ [
+ stack,
+ hostnames,
+ publishableKey,
+ siteVisitEnabled,
+ outboundEnabled,
+ conversionEnabled,
+ ],
+ );
+
return (
<SectionCard number={2} title="Installation and verification">
<div className="divide-y divide-neutral-200">
<TrackingSettingsRow
heading="Setup instructions"
description="Use the prompts and instructions to install Dub."
- leftExtra={ready ? <DeveloperGuides /> : undefined}
+ leftExtra={
+ ready ? <DeveloperGuides steps={setup.steps} /> : undefined
+ }
leftExtraAlign="end"
>
- <SetupInstructions stack={stack} hasHostname={hostnames.length > 0} />
+ <SetupInstructions setup={setup} ready={ready} />
</TrackingSettingsRow>
<TrackingSettingsRow
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/setup-instructions.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/setup-instructions.tsx
index 24735d85e2..5a4ba6bd5f 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/setup-instructions.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/setup-instructions.tsx
@@ -1,7 +1,9 @@
"use client";
-import { guides, IntegrationGuide, stackItems } from "@/ui/guides/integrations";
-import { GuidesMarkdown } from "@/ui/guides/markdown";
+import {
+ TrackingSetup,
+ TrackingSetupStep,
+} from "@/lib/tracking/build-tracking-setup";
import { BookOpen, Button, CopyButton, Tooltip } from "@dub/ui";
import {
ChatGPTIcon,
@@ -13,10 +15,8 @@ import {
} from "@dub/ui/icons";
import { cn } from "@dub/utils";
import Link from "next/link";
-import { useState } from "react";
-import { useDynamicGuide } from "./use-dynamic-guide";
-const DEVELOPER_GUIDES = [
+const FALLBACK_DEVELOPER_GUIDES = [
{
title: "Client-side SDK install guide",
href: "https://dub.co/docs/sdks/client-side",
@@ -62,10 +62,6 @@ const OPEN_IN_APPS = [
},
] as const;
-function getInstallPrompt(url: string) {
- return `Read from ${url} and implement it.`;
-}
-
function OpenInApps({ prompt }: { prompt: string }) {
return (
<div className="flex items-center gap-2">
@@ -101,23 +97,24 @@ function OpenInApps({ prompt }: { prompt: string }) {
);
}
-function getSelectedStackItems(stackIds: string[]) {
- return stackItems.filter((item) => stackIds.includes(item.id));
-}
+function StepIcon({ step }: { step: TrackingSetupStep }) {
+ if (!step.icon) {
+ return <BookOpen className="size-5 shrink-0 text-neutral-800" />;
+ }
-function getPrimaryGuide(stackIds: string[]): IntegrationGuide | undefined {
- const selected = getSelectedStackItems(stackIds);
- const preferred =
- selected.find((item) => item.type === "client-sdk") ??
- selected.find((item) => item.type === "track-lead") ??
- selected.find((item) => item.type === "track-sale") ??
- selected[0];
+ const Icon = step.icon;
- if (!preferred) {
- return undefined;
+ if (step.iconProps?.fullSize) {
+ return (
+ <Icon
+ className="size-5 shrink-0 overflow-hidden rounded"
+ width="auto"
+ height="100%"
+ />
+ );
}
- return guides.find((guide) => guide.key === preferred.guideKeys[0]);
+ return <Icon className="size-5 shrink-0" />;
}
export function isSetupInstructionsReady(
@@ -127,16 +124,24 @@ export function isSetupInstructionsReady(
return stack.length > 0 && hasHostname;
}
-export function DeveloperGuides() {
+export function DeveloperGuides({ steps }: { steps?: TrackingSetupStep[] }) {
+ const guides =
+ steps && steps.length > 0
+ ? steps.map((step) => ({
+ title: step.label,
+ href: step.url,
+ }))
+ : FALLBACK_DEVELOPER_GUIDES;
+
return (
<div className="flex flex-col gap-1.5">
<h4 className="text-xs font-semibold leading-4 tracking-[-0.02em] text-neutral-600">
Read developer guides
</h4>
<div className="flex flex-col gap-1.5">
- {DEVELOPER_GUIDES.map((guide) => (
+ {guides.map((guide) => (
<Link
- key={guide.href}
+ key={`${guide.title}-${guide.href}`}
href={guide.href}
target="_blank"
rel="noopener noreferrer"
@@ -152,18 +157,12 @@ export function DeveloperGuides() {
}
export function SetupInstructions({
- stack,
- hasHostname,
+ setup,
+ ready,
}: {
- stack: string[];
- hasHostname: boolean;
+ setup: TrackingSetup;
+ ready: boolean;
}) {
- const ready = isSetupInstructionsReady(stack, hasHostname);
- const primaryGuide = getPrimaryGuide(stack);
- const clientSdkItems = getSelectedStackItems(stack).filter(
- (item) => item.type === "client-sdk",
- );
-
if (!ready) {
return (
<div className="flex h-[114px] w-full flex-col items-center justify-center gap-2 rounded-xl bg-neutral-50 py-6">
@@ -178,51 +177,38 @@ export function SetupInstructions({
return (
<div className="flex flex-col gap-3">
- {primaryGuide && <MainPromptCard guide={primaryGuide} />}
+ <MainPromptCard prompt={setup.prompt} />
- {clientSdkItems.length > 0 && (
+ {setup.steps.length > 0 && (
<div className="flex flex-col gap-2">
- {clientSdkItems.map((item) => {
- const guide = guides.find((g) => g.key === item.guideKeys[0]);
- if (!guide) {
- return null;
- }
-
- const Icon = item.icon;
-
- return (
- <div
- key={item.id}
- className="flex h-[52px] w-full items-center justify-between gap-3 rounded-[10px] bg-neutral-50 p-3"
- >
- <div className="flex min-w-0 items-center gap-2.5">
- <Icon className="size-5 shrink-0" />
- <span className="truncate text-sm font-medium text-neutral-800">
- {item.title}
- </span>
- </div>
- <Button
- text="Read install guide"
- variant="secondary"
- className="h-8 w-fit shrink-0 px-2.5"
- onClick={() =>
- window.open(guide.url, "_blank", "noopener,noreferrer")
- }
- />
+ {setup.steps.map((step) => (
+ <div
+ key={`${step.type}-${step.guideKey ?? step.url}`}
+ className="flex h-[52px] w-full items-center justify-between gap-3 rounded-[10px] bg-neutral-50 p-3"
+ >
+ <div className="flex min-w-0 items-center gap-2.5">
+ <StepIcon step={step} />
+ <span className="truncate text-sm font-medium text-neutral-800">
+ {step.label}
+ </span>
</div>
- );
- })}
+ <Button
+ text="Read install guide"
+ variant="secondary"
+ className="h-8 w-fit shrink-0 px-2.5"
+ onClick={() =>
+ window.open(step.url, "_blank", "noopener,noreferrer")
+ }
+ />
+ </div>
+ ))}
</div>
)}
</div>
);
}
-function MainPromptCard({ guide }: { guide: IntegrationGuide }) {
- const { loading, guideMarkdown } = useDynamicGuide({ guide: guide.key });
- const [expanded, setExpanded] = useState(false);
- const prompt = getInstallPrompt(guide.url);
-
+function MainPromptCard({ prompt }: { prompt: string }) {
return (
<div className="flex flex-col gap-3">
<div className="flex items-center justify-between gap-2">
@@ -242,45 +228,10 @@ function MainPromptCard({ guide }: { guide: IntegrationGuide }) {
successMessage="Prompt copied to clipboard"
/>
- <div className="p-4 pb-0">
- {expanded && loading ? (
- <div className="space-y-3 pr-8">
- <div className="h-4 w-1/2 animate-pulse rounded bg-neutral-200" />
- <div className="h-4 w-full animate-pulse rounded bg-neutral-100" />
- <div className="h-4 w-5/6 animate-pulse rounded bg-neutral-100" />
- </div>
- ) : expanded && guideMarkdown ? (
- <div className="pr-8">
- <GuidesMarkdown>{guideMarkdown}</GuidesMarkdown>
- </div>
- ) : (
- <p className="pr-8 font-mono text-[13px] leading-6 text-neutral-800">
- {prompt}
- </p>
- )}
- </div>
-
- <div
- className={cn(
- "pointer-events-none relative flex justify-center px-4 pb-4",
- expanded ? "pt-4" : "pt-5",
- )}
- style={
- expanded
- ? undefined
- : {
- background:
- "linear-gradient(180deg, rgba(255, 255, 255, 0) 0%, #FFFFFF 100%)",
- }
- }
- >
- <Button
- text={expanded ? "Show less" : "View all"}
- variant="secondary"
- className="pointer-events-auto h-8 w-fit px-2.5"
- disabled={!expanded && loading && !guideMarkdown}
- onClick={() => setExpanded((current) => !current)}
- />
+ <div className="p-4">
+ <p className="whitespace-pre-wrap pr-8 font-mono text-[13px] leading-6 text-neutral-800">
+ {prompt}
+ </p>
</div>
</div>
</div>
diff --git a/apps/web/lib/tracking/build-tracking-setup.ts b/apps/web/lib/tracking/build-tracking-setup.ts
new file mode 100644
index 0000000000..8afc63cf59
--- /dev/null
+++ b/apps/web/lib/tracking/build-tracking-setup.ts
@@ -0,0 +1,214 @@
+import {
+ guides,
+ IntegrationType,
+ StackItem,
+ stackItems,
+} from "@/ui/guides/integrations";
+
+const STEP_TYPES: IntegrationType[] = [
+ "client-sdk",
+ "track-lead",
+ "track-sale",
+];
+
+const TYPE_LABELS: Record<IntegrationType, string> = {
+ "client-sdk": "Client-side",
+ "track-lead": "Lead tracking",
+ "track-sale": "Sale tracking",
+};
+
+const FALLBACK_CLIENT_STEP = {
+ type: "client-sdk" as const,
+ title: "Client-side script",
+ url: "https://dub.co/docs/sdks/client-side",
+ guideKey: null,
+};
+
+const ANALYTICS_SCRIPT_BASE = "https://www.dubcdn.com/analytics/script.js";
+
+export type TrackingSetupStep = {
+ type: IntegrationType;
+ typeLabel: string;
+ title: string;
+ label: string;
+ url: string;
+ guideKey: string | null;
+ icon: StackItem["icon"] | null;
+ iconProps?: StackItem["iconProps"];
+};
+
+export type BuildTrackingSetupInput = {
+ stack: string[];
+ hostnames: string[];
+ publishableKey: string | null;
+ siteVisitEnabled: boolean;
+ outboundEnabled: boolean;
+ conversionEnabled: boolean;
+};
+
+export type TrackingSetup = {
+ steps: TrackingSetupStep[];
+ prompt: string;
+};
+
+function getSelectedStackItems(stack: string[]) {
+ return stack
+ .map((id) => stackItems.find((item) => item.id === id))
+ .filter((item): item is StackItem => Boolean(item));
+}
+
+function getGuideForType(item: StackItem, type: IntegrationType) {
+ return item.guideKeys
+ .map((key) => guides.find((guide) => guide.key === key))
+ .find((guide) => guide?.type === type);
+}
+
+function toStep(
+ item: StackItem,
+ type: IntegrationType,
+ guide: NonNullable<ReturnType<typeof getGuideForType>>,
+): TrackingSetupStep {
+ const typeLabel = TYPE_LABELS[type];
+
+ return {
+ type,
+ typeLabel,
+ title: item.title,
+ label: `${typeLabel} — ${item.title}`,
+ url: guide.url,
+ guideKey: guide.key,
+ icon: item.icon,
+ iconProps: item.iconProps,
+ };
+}
+
+export function resolveTrackingSetupSteps(
+ stack: string[],
+): TrackingSetupStep[] {
+ const selected = getSelectedStackItems(stack);
+ const steps: TrackingSetupStep[] = [];
+
+ for (const type of STEP_TYPES) {
+ const explicit = selected.filter((item) => item.type === type);
+
+ if (explicit.length > 0) {
+ for (const item of explicit) {
+ const guide = getGuideForType(item, type);
+ if (guide) {
+ steps.push(toStep(item, type, guide));
+ }
+ }
+ continue;
+ }
+
+ for (const item of selected) {
+ const guide = getGuideForType(item, type);
+ if (guide) {
+ steps.push(toStep(item, type, guide));
+ }
+ }
+ }
+
+ if (!steps.some((step) => step.type === "client-sdk")) {
+ const typeLabel = TYPE_LABELS["client-sdk"];
+ steps.unshift({
+ ...FALLBACK_CLIENT_STEP,
+ typeLabel,
+ label: FALLBACK_CLIENT_STEP.title,
+ icon: null,
+ });
+ }
+
+ return steps;
+}
+
+export function getAnalyticsScriptUrl({
+ siteVisitEnabled,
+ outboundEnabled,
+ conversionEnabled,
+}: Pick<
+ BuildTrackingSetupInput,
+ "siteVisitEnabled" | "outboundEnabled" | "conversionEnabled"
+>) {
+ const segments = [
+ siteVisitEnabled ? "site-visit" : null,
+ outboundEnabled ? "outbound-domains" : null,
+ conversionEnabled ? "conversion-tracking" : null,
+ ].filter(Boolean);
+
+ if (segments.length === 0) {
+ return ANALYTICS_SCRIPT_BASE;
+ }
+
+ return `https://www.dubcdn.com/analytics/script.${segments.join(".")}.js`;
+}
+
+export function composeTrackingSetupPrompt({
+ steps,
+ hostnames,
+ publishableKey,
+ siteVisitEnabled,
+ outboundEnabled,
+ conversionEnabled,
+}: {
+ steps: TrackingSetupStep[];
+} & Omit<BuildTrackingSetupInput, "stack">) {
+ const workspaceLines = [
+ hostnames.length > 0 ? `- Hostnames: ${hostnames.join(", ")}` : null,
+ publishableKey
+ ? `- Publishable key (client-side): ${publishableKey}`
+ : null,
+ `- Analytics script: ${getAnalyticsScriptUrl({
+ siteVisitEnabled,
+ outboundEnabled,
+ conversionEnabled,
+ })}`,
+ ].filter(Boolean);
+
+ const hasDuplicateTypes = STEP_TYPES.some(
+ (type) => steps.filter((step) => step.type === type).length > 1,
+ );
+
+ const stepLines = steps.map((step, index) => {
+ const heading =
+ step.guideKey === null ? step.title : `${step.typeLabel} — ${step.title}`;
+
+ return `${index + 1}. ${heading}\n Read ${step.url} and implement it.`;
+ });
+
+ return [
+ "Install Dub conversion tracking for this workspace.",
+ "",
+ "Workspace:",
+ ...workspaceLines,
+ "",
+ "Implement these steps in order:",
+ ...stepLines,
+ ...(hasDuplicateTypes
+ ? [
+ "",
+ "If multiple guides share a step, use the one that matches this codebase.",
+ ]
+ : []),
+ "",
+ "Use the workspace values above. Do not invent a different publishable key or hostname.",
+ ].join("\n");
+}
+
+export function buildTrackingSetup(
+ input: BuildTrackingSetupInput,
+): TrackingSetup {
+ const steps = resolveTrackingSetupSteps(input.stack);
+
+ return {
+ steps,
+ prompt: composeTrackingSetupPrompt({
+ steps,
+ hostnames: input.hostnames,
+ publishableKey: input.publishableKey,
+ siteVisitEnabled: input.siteVisitEnabled,
+ outboundEnabled: input.outboundEnabled,
+ conversionEnabled: input.conversionEnabled,
+ }),
+ };
+}