Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ HUBSPOT_CLIENT_SECRET=
INTERCOM_CLIENT_ID=
INTERCOM_CLIENT_SECRET=

# Google Ads integration
GOOGLE_ADS_CLIENT_ID=
GOOGLE_ADS_CLIENT_SECRET=
GOOGLE_ADS_DEVELOPER_TOKEN=

# E2E Playwright Tests
E2E_PARTNER_EMAIL=
E2E_PARTNER_PASSWORD=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { withCron } from "@/lib/cron/with-cron";
import { syncGoogleAdsInstalledWorkspaceSet } from "@/lib/integrations/google-ads/installed-workspaces";
import { logAndRespond } from "../../utils";

export const dynamic = "force-dynamic";

// GET /api/cron/google-ads/sync-installed-workspaces
// Rebuild the Redis set of workspaces with Google Ads installed.
// Runs every minute (* * * * *)
export const GET = withCron(async () => {
const synced = await syncGoogleAdsInstalledWorkspaceSet();

return logAndRespond(
`Synced ${synced} workspace(s) with Google Ads installed.`,
);
});
133 changes: 133 additions & 0 deletions apps/web/app/(ee)/api/google-ads/callback/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { getSession } from "@/lib/auth";
import { encrypt } from "@/lib/encryption";
import {
GoogleAdsApi,
inferLoginCustomerId,
} from "@/lib/integrations/google-ads/api";
import { googleAdsInstalledWorkspaces } from "@/lib/integrations/google-ads/installed-workspaces";
import { googleAdsOAuthProvider } from "@/lib/integrations/google-ads/oauth";
import {
googleAdsAuthTokenSchema,
googleAdsSettingsSchema,
} from "@/lib/integrations/google-ads/schema";
import { installIntegration } from "@/lib/integrations/install";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import { prisma } from "@/lib/prisma";
import { WorkspaceProps } from "@/lib/types";
import { GOOGLE_ADS_INTEGRATION_ID } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { redirect } from "next/navigation";

export const dynamic = "force-dynamic";

// GET /api/google-ads/callback - OAuth callback from Google Ads
export const GET = async (req: Request) => {
let workspace:
| (Pick<WorkspaceProps, "id" | "slug" | "users"> & { plan: string })
| null = null;

try {
const session = await getSession();

if (!session?.user.id) {
throw new DubApiError({
code: "unauthorized",
message: "Unauthorized. Please login to continue.",
});
}

const { token, contextId: workspaceId } =
await googleAdsOAuthProvider.exchangeCodeForToken<string>(req);

workspace = await prisma.project.findUniqueOrThrow({
where: {
id: workspaceId,
},
select: {
id: true,
slug: true,
plan: true,
users: {
where: {
userId: session.user.id,
},
select: {
role: true,
defaultFolderId: true,
},
},
},
});

if (workspace.users.length === 0) {
throw new DubApiError({
code: "bad_request",
message: "You are not a member of this workspace.",
});
}

if (workspace.users[0].role !== "owner") {
throw new DubApiError({
code: "bad_request",
message: "Only workspace owners can install integrations.",
});
}

if (!getPlanCapabilities(workspace.plan).canInstallAdvancedIntegrations) {
throw new DubApiError({
code: "forbidden",
message:
"Google Ads integration is only available on Advanced and Enterprise plans.",
});
}

const credentials = googleAdsAuthTokenSchema.parse({
...token,
created_at: Date.now(),
access_token: encrypt(token.access_token),
refresh_token: encrypt(token.refresh_token),
});

const googleAdsApi = new GoogleAdsApi({
accessToken: token.access_token,
});

const customers = await googleAdsApi.listAccessibleCustomers();

let customerName: string | null = null;
let customerId: string | null = null;
let loginCustomerId: string | null = null;

// Just one customer, so we can use the first one
if (customers.length === 1) {
customerName = customers[0].descriptiveName;
customerId = customers[0].id;
loginCustomerId = inferLoginCustomerId({
customers,
selectedCustomerId: customerId,
});
}

const settings = googleAdsSettingsSchema.parse({
customers,
customerId,
customerName,
loginCustomerId,
});

await installIntegration({
integrationId: GOOGLE_ADS_INTEGRATION_ID,
userId: session.user.id,
workspaceId,
credentials,
settings,
});

waitUntil(googleAdsInstalledWorkspaces.add(workspaceId));
} catch (error) {
return handleAndReturnErrorResponse(error);
}

redirect(`/${workspace.slug}/settings/integrations/google-ads`);
};
80 changes: 80 additions & 0 deletions apps/web/app/(ee)/api/google-ads/conversion-actions/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import {
GoogleAdsApi,
inferLoginCustomerId,
} from "@/lib/integrations/google-ads/api";
import { googleAdsOAuthProvider } from "@/lib/integrations/google-ads/oauth";
import { googleAdsSettingsSchema } from "@/lib/integrations/google-ads/schema";
import { prisma } from "@/lib/prisma";
import { GOOGLE_ADS_INTEGRATION_ID } from "@dub/utils";
import { NextResponse } from "next/server";
import * as z from "zod/v4";

// GET /api/google-ads/conversion-actions - List UPLOAD_CLICKS conversion actions for a customer
export const GET = withWorkspace(
async ({ workspace, searchParams }) => {
const { customerId } = z
.object({
customerId: z.string().min(1),
})
.parse(searchParams);

const installedIntegration = await prisma.installedIntegration.findFirst({
where: {
integrationId: GOOGLE_ADS_INTEGRATION_ID,
projectId: workspace.id,
},
});

if (!installedIntegration) {
throw new DubApiError({
code: "bad_request",
message: "Google Ads integration is not installed on your workspace.",
});
}

const token =
await googleAdsOAuthProvider.getAccessToken(installedIntegration);

const currentSettings = googleAdsSettingsSchema.parse(
installedIntegration.settings ?? {},
);

const normalizedCustomerId = customerId.replace(/-/g, "");
const selectedCustomer = currentSettings.customers.find(
(customer) => customer.id.replace(/-/g, "") === normalizedCustomerId,
);

if (!selectedCustomer) {
throw new DubApiError({
code: "bad_request",
message:
"The selected Google Ads account is not available for this workspace. Please reconnect the integration.",
});
}

const loginCustomerId = inferLoginCustomerId({
customers: currentSettings.customers,
selectedCustomerId: customerId,
});

const googleAdsApi = new GoogleAdsApi({
accessToken: token.access_token,
loginCustomerId,
customerId,
});

const conversionActions =
await googleAdsApi.listUploadClickConversionActions(customerId);

return NextResponse.json({
conversionActions,
loginCustomerId,
});
},
{
requiredPermissions: ["integrations.write"],
requiredPlan: ["advanced", "enterprise"],
},
);
21 changes: 21 additions & 0 deletions apps/web/app/(ee)/api/google-ads/upload-conversion/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { withCron } from "@/lib/cron/with-cron";
import { googleAdsConversionUploadSchema } from "@/lib/integrations/google-ads/schema";
import { uploadGoogleAdsConversion } from "@/lib/integrations/google-ads/upload-conversion";
import { logAndRespond } from "../../cron/utils";

export const dynamic = "force-dynamic";

// POST /api/google-ads/upload-conversion - Upload a conversion to Google Ads
export const POST = withCron(async ({ rawBody }) => {
const payload = googleAdsConversionUploadSchema.parse(JSON.parse(rawBody));

const { message, status } = await uploadGoogleAdsConversion(payload);

if (status === "failed") {
return logAndRespond(message, { status: 500, logLevel: "error" });
}

return logAndRespond(message, {
logLevel: status === "skipped" ? "warn" : "info",
});
});
6 changes: 5 additions & 1 deletion apps/web/app/api/integrations/uninstall/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { googleAdsInstalledWorkspaces } from "@/lib/integrations/google-ads/installed-workspaces";
import { slackOAuthProvider } from "@/lib/integrations/slack/oauth";
import { prisma } from "@/lib/prisma";
import { SLACK_INTEGRATION_ID } from "@dub/utils";
import { GOOGLE_ADS_INTEGRATION_ID, SLACK_INTEGRATION_ID } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";

Expand Down Expand Up @@ -54,6 +55,9 @@ export const DELETE = withWorkspace(
...(integrationId === SLACK_INTEGRATION_ID
? [slackOAuthProvider.uninstall(installation)]
: []),
...(integrationId === GOOGLE_ADS_INTEGRATION_ID
? [googleAdsInstalledWorkspaces.remove(workspace.id)]
: []),
]),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getIntegrationInstallUrl } from "@/lib/actions/get-integration-install-
import { clientAccessCheck } from "@/lib/client-access-check";
import { installAppsFlyerAction } from "@/lib/integrations/appsflyer/install";
import { AppsFlyerSettings } from "@/lib/integrations/appsflyer/ui/settings";
import { GoogleAdsSettings } from "@/lib/integrations/google-ads/ui/settings";
import { HubSpotSettings } from "@/lib/integrations/hubspot/ui/settings";
import { SegmentSettings } from "@/lib/integrations/segment/ui/settings";
import { SlackSettings } from "@/lib/integrations/slack/ui/settings";
Expand Down Expand Up @@ -48,6 +49,7 @@ import {
DUB_WORKSPACE_ID,
formatDate,
getDomainWithoutWWW,
GOOGLE_ADS_INTEGRATION_ID,
SEGMENT_INTEGRATION_ID,
SLACK_INTEGRATION_ID,
STRIPE_INTEGRATION_ID,
Expand All @@ -69,6 +71,7 @@ const integrationSettings = {
[HUBSPOT_INTEGRATION_ID]: HubSpotSettings,
[STRIPE_INTEGRATION_ID]: StripeIntegrationSettings,
[APPSFLYER_INTEGRATION_ID]: AppsFlyerSettings,
[GOOGLE_ADS_INTEGRATION_ID]: GoogleAdsSettings,
};

export default function IntegrationPageClient({
Expand All @@ -77,14 +80,14 @@ export default function IntegrationPageClient({
integration: InstalledIntegrationInfoProps;
}) {
const { id: workspaceId, slug, plan, role, stripeConnectId } = useWorkspace();
const { isMobile } = useMediaQuery();
const [openPopover, setOpenPopover] = useState(false);

const permissionsError = clientAccessCheck({
action: "integrations.write",
role,
}).error;
const { isMobile } = useMediaQuery();

const [openPopover, setOpenPopover] = useState(false);
const { execute, isPending } = useAction(getIntegrationInstallUrl, {
onSuccess: ({ data }) => {
if (!data?.url) {
Expand Down Expand Up @@ -352,6 +355,7 @@ export default function IntegrationPageClient({
HUBSPOT_INTEGRATION_ID,
APPSFLYER_INTEGRATION_ID,
INTERCOM_INTEGRATION_ID,
GOOGLE_ADS_INTEGRATION_ID,
].includes(integration.id) &&
!canInstallAdvancedIntegrations ? (
<TooltipContent
Expand Down
10 changes: 10 additions & 0 deletions apps/web/lib/actions/get-integration-install-url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use server";

import * as z from "zod/v4";
import { googleAdsOAuthProvider } from "../integrations/google-ads/oauth";
import { isGoogleAdsAllowedWorkspace } from "../integrations/google-ads/utils";
import { hubSpotOAuthProvider } from "../integrations/hubspot/oauth";
import { intercomOAuthProvider } from "../integrations/intercom/oauth";
import { slackOAuthProvider } from "../integrations/slack/oauth";
Expand Down Expand Up @@ -32,6 +34,14 @@ export const getIntegrationInstallUrl = authActionClient
url = await hubSpotOAuthProvider.generateAuthUrl(workspace.id);
} else if (integrationSlug === "intercom") {
url = await intercomOAuthProvider.generateAuthUrl(workspace.id);
} else if (integrationSlug === "google-ads") {
if (!isGoogleAdsAllowedWorkspace(workspace.id)) {
throw new Error(
"Google Ads integration is not available for this workspace",
);
}

url = await googleAdsOAuthProvider.generateAuthUrl(workspace.id);
} else {
throw new Error("Invalid integration slug");
}
Expand Down
15 changes: 14 additions & 1 deletion apps/web/lib/api/conversions/track-lead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createId } from "@/lib/api/create-id";
import { createOrGetCustomer } from "@/lib/api/customers/create-or-get-customer";
import { DubApiError } from "@/lib/api/errors";
import { includeTags } from "@/lib/api/links/include-tags";
import { queueGoogleAdsConversionUpload } from "@/lib/integrations/google-ads/upload-conversion";
import { generateRandomName } from "@/lib/names";
import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";
import { sendPartnerPostback } from "@/lib/postback/send-partner-postback";
Expand All @@ -17,7 +18,7 @@ import {
trackLeadResponseSchema,
} from "@/lib/zod/schemas/leads";
import { nanoid, R2_URL } from "@dub/utils";
import { Link } from "@prisma/client";
import { EventType, Link } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import * as z from "zod/v4";
import { syncPartnerLinksStats } from "../partners/sync-partner-links-stats";
Expand Down Expand Up @@ -352,6 +353,18 @@ export const trackLead = async ({
workspace,
}),

queueGoogleAdsConversionUpload({
workspaceId: workspace.id,
eventType: EventType.lead,
eventId: leadEventId,
conversionDateTime: new Date().toISOString(),
conversionCount: eventQuantity ?? undefined,
click: {
id: clickData.click_id,
url: clickData.url,
},
}),

...(link.partnerId
? [
sendPartnerPostback({
Expand Down
Loading
Loading