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.

Fix Google Ads integration

Steven Tey3f026918d7d2
diff --git a/apps/web/app/(ee)/api/google-ads/conversion-actions/route.ts b/apps/web/app/(ee)/api/google-ads/conversion-actions/route.ts
index 58e335cf0b..7395c04e51 100644
--- a/apps/web/app/(ee)/api/google-ads/conversion-actions/route.ts
+++ b/apps/web/app/(ee)/api/google-ads/conversion-actions/route.ts
@@ -2,7 +2,7 @@ import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import {
GoogleAdsApi,
- inferLoginCustomerId,
+ getLoginCustomerIdCandidates,
} from "@/lib/integrations/google-ads/api";
import { googleAdsOAuthProvider } from "@/lib/integrations/google-ads/oauth";
import { googleAdsSettingsSchema } from "@/lib/integrations/google-ads/schema";
@@ -54,23 +54,47 @@ export const GET = withWorkspace(
});
}
- const loginCustomerId = inferLoginCustomerId({
+ const candidates = getLoginCustomerIdCandidates({
customers: currentSettings.customers,
selectedCustomerId: customerId,
});
- const googleAdsApi = new GoogleAdsApi({
- accessToken: token.access_token,
- loginCustomerId,
- customerId,
- });
+ let lastError: unknown;
+
+ for (const loginCustomerId of candidates) {
+ try {
+ const googleAdsApi = new GoogleAdsApi({
+ accessToken: token.access_token,
+ loginCustomerId,
+ customerId,
+ });
+
+ const conversionActions =
+ await googleAdsApi.listUploadClickConversionActions(customerId);
+
+ return NextResponse.json({
+ conversionActions,
+ loginCustomerId,
+ });
+ } catch (error) {
+ lastError = error;
- const conversionActions =
- await googleAdsApi.listUploadClickConversionActions(customerId);
+ const message = error instanceof Error ? error.message : String(error);
+
+ if (!message.includes("USER_PERMISSION_DENIED")) {
+ throw error;
+ }
+ }
+ }
+
+ if (lastError instanceof Error) {
+ throw lastError;
+ }
- return NextResponse.json({
- conversionActions,
- loginCustomerId,
+ throw new DubApiError({
+ code: "forbidden",
+ message:
+ "The Google Ads user does not have permission to access this account.",
});
},
{
diff --git a/apps/web/lib/integrations/google-ads/api.ts b/apps/web/lib/integrations/google-ads/api.ts
index b2783d6dfb..ee8751c2b4 100644
--- a/apps/web/lib/integrations/google-ads/api.ts
+++ b/apps/web/lib/integrations/google-ads/api.ts
@@ -135,7 +135,41 @@ const dataManagerFetch = async <T>({
};
const formatApiErrorDetail = (data: any, rawText: string) => {
- return data?.error?.message ?? JSON.stringify(data) ?? rawText;
+ const payload = Array.isArray(data) ? data[0] : data;
+ const googleAdsError = payload?.error?.details?.[0]?.errors?.[0]?.message;
+
+ return (
+ googleAdsError ??
+ payload?.error?.message ??
+ (data ? JSON.stringify(data) : rawText)
+ );
+};
+
+const normalizeGoogleAdsCustomerId = (customerId: string) =>
+ customerId.replace(/-/g, "").replace(/^customers\//, "");
+
+const isGoogleAdsPermissionDenied = (error: unknown) => {
+ const message = error instanceof Error ? error.message : String(error);
+ return message.includes("USER_PERMISSION_DENIED");
+};
+
+type SearchStreamRow = {
+ customer?: {
+ id?: string | number;
+ descriptiveName?: string;
+ manager?: boolean;
+ };
+ conversionAction?: {
+ id?: string | number;
+ resourceName?: string;
+ name?: string;
+ };
+ customerClient?: {
+ id?: string | number;
+ resourceName?: string;
+ descriptiveName?: string;
+ manager?: boolean;
+ };
};
const searchStream = async ({
@@ -146,23 +180,10 @@ const searchStream = async ({
customerId: string;
query: string;
}) => {
- const normalizedCustomerId = customerId.replace(/-/g, "");
+ const normalizedCustomerId = normalizeGoogleAdsCustomerId(customerId);
const response = await googleAdsFetch<
- {
- results?: {
- customer?: {
- id?: string;
- descriptiveName?: string;
- manager?: boolean;
- };
- conversionAction?: {
- id?: string;
- resourceName?: string;
- name?: string;
- };
- }[];
- }[]
+ { results?: SearchStreamRow[] }[] | { results?: SearchStreamRow[] }
>({
...options,
path: `customers/${normalizedCustomerId}/googleAds:searchStream`,
@@ -170,7 +191,8 @@ const searchStream = async ({
body: { query },
});
- return response.flatMap((batch) => batch.results ?? []);
+ const batches = Array.isArray(response) ? response : [response];
+ return batches.flatMap((batch) => batch.results ?? []);
};
export class GoogleAdsApi {
@@ -191,6 +213,42 @@ export class GoogleAdsApi {
});
const resourceNames = response.resourceNames ?? [];
+ const customersById = new Map<
+ string,
+ z.infer<typeof googleAdsCustomerSchema>
+ >();
+
+ const toCustomer = ({
+ id,
+ resourceName,
+ descriptiveName,
+ manager,
+ loginCustomerId,
+ }: {
+ id: string;
+ resourceName?: string;
+ descriptiveName?: string;
+ manager?: boolean;
+ loginCustomerId?: string | null;
+ }) => {
+ const normalizedId = normalizeGoogleAdsCustomerId(id);
+
+ return googleAdsCustomerSchema.parse({
+ id: normalizedId,
+ resourceName: resourceName ?? `customers/${normalizedId}`,
+ descriptiveName: descriptiveName || `Account ${normalizedId}`,
+ manager: manager ?? false,
+ loginCustomerId: loginCustomerId ?? (manager ? normalizedId : null),
+ });
+ };
+
+ const setCustomer = (customer: z.infer<typeof googleAdsCustomerSchema>) => {
+ const id = normalizeGoogleAdsCustomerId(customer.id);
+
+ if (!customersById.has(id)) {
+ customersById.set(id, customer);
+ }
+ };
const fetchCustomer = async ({
resourceName,
@@ -199,7 +257,7 @@ export class GoogleAdsApi {
resourceName: string;
loginCustomerId?: string | null;
}) => {
- const customerId = resourceName.replace("customers/", "");
+ const customerId = normalizeGoogleAdsCustomerId(resourceName);
const results = await searchStream({
...this.options,
@@ -211,15 +269,24 @@ export class GoogleAdsApi {
const customer = results[0]?.customer;
- return googleAdsCustomerSchema.parse({
- id: customer?.id?.toString() ?? customerId,
+ if (!customer) {
+ throw new Error(
+ `[Google Ads API] No customer data returned for ${customerId}`,
+ );
+ }
+
+ const id = customer.id?.toString() ?? customerId;
+
+ return toCustomer({
+ id,
resourceName,
- descriptiveName: customer?.descriptiveName ?? `Account ${customerId}`,
- manager: customer?.manager ?? false,
+ descriptiveName: customer.descriptiveName,
+ manager: customer.manager,
+ loginCustomerId: loginCustomerId ?? (customer.manager ? id : null),
});
};
- const initialResults = await Promise.all(
+ const directResults = await Promise.all(
resourceNames.map(async (resourceName) => {
try {
return {
@@ -227,10 +294,12 @@ export class GoogleAdsApi {
customer: await fetchCustomer({ resourceName }),
};
} catch (error) {
- console.error(
- `[Google Ads API] Failed to fetch customer ${resourceName.replace("customers/", "")}`,
- error,
- );
+ if (!isGoogleAdsPermissionDenied(error)) {
+ console.error(
+ `[Google Ads API] Failed to fetch customer ${normalizeGoogleAdsCustomerId(resourceName)}`,
+ error,
+ );
+ }
return {
resourceName,
@@ -240,45 +309,100 @@ export class GoogleAdsApi {
}),
);
- const managerAccounts = initialResults
- .map((result) => result.customer)
- .filter((customer): customer is z.infer<typeof googleAdsCustomerSchema> =>
- Boolean(customer?.manager),
- );
+ for (const { customer } of directResults) {
+ if (customer) {
+ setCustomer(customer);
+ }
+ }
- const loginCustomerId =
- managerAccounts.length === 1 ? managerAccounts[0].id : null;
+ // Only directly accessible managers are valid login-customer-id values.
+ const managerAccounts = [...customersById.values()].filter(
+ (customer) => customer.manager,
+ );
- const customers = await Promise.all(
- initialResults.map(async ({ resourceName, customer }) => {
- if (customer) {
- return customer;
- }
+ const failedResourceNames = directResults
+ .filter((result) => result.customer === null)
+ .map((result) => result.resourceName);
- if (!loginCustomerId) {
- return null;
- }
+ for (const manager of managerAccounts) {
+ const remainingResourceNames = failedResourceNames.filter(
+ (resourceName) =>
+ !customersById.has(normalizeGoogleAdsCustomerId(resourceName)),
+ );
- const customerId = resourceName.replace("customers/", "");
+ if (remainingResourceNames.length === 0) {
+ break;
+ }
+
+ await Promise.all(
+ remainingResourceNames.map(async (resourceName) => {
+ const customerId = normalizeGoogleAdsCustomerId(resourceName);
+
+ try {
+ setCustomer(
+ await fetchCustomer({
+ resourceName,
+ loginCustomerId: manager.id,
+ }),
+ );
+ } catch (error) {
+ if (!isGoogleAdsPermissionDenied(error)) {
+ console.error(
+ `[Google Ads API] Failed to fetch customer ${customerId} with login-customer-id ${manager.id}`,
+ error,
+ );
+ }
+ }
+ }),
+ );
+ }
+ await Promise.all(
+ managerAccounts.map(async (manager) => {
try {
- return await fetchCustomer({ resourceName, loginCustomerId });
+ const results = await searchStream({
+ ...this.options,
+ customerId: manager.id,
+ loginCustomerId: manager.id,
+ query:
+ "SELECT customer_client.id, customer_client.descriptive_name, customer_client.manager, customer_client.resource_name FROM customer_client WHERE customer_client.status = ENABLED",
+ });
+
+ for (const result of results) {
+ const client = result.customerClient;
+
+ if (!client?.id) {
+ continue;
+ }
+
+ const normalizedId = normalizeGoogleAdsCustomerId(
+ client.id.toString(),
+ );
+
+ if (customersById.has(normalizedId)) {
+ continue;
+ }
+
+ setCustomer(
+ toCustomer({
+ id: normalizedId,
+ resourceName: `customers/${normalizedId}`,
+ descriptiveName: client.descriptiveName,
+ manager: client.manager,
+ loginCustomerId: manager.id,
+ }),
+ );
+ }
} catch (error) {
console.error(
- `[Google Ads API] Failed to fetch customer ${customerId} with login-customer-id ${loginCustomerId}`,
+ `[Google Ads API] Failed to list client accounts for manager ${manager.id}`,
error,
);
-
- return null;
}
}),
);
- // Only keep accounts we could actually read (skip permission-denied ones).
- return customers.filter(
- (customer): customer is z.infer<typeof googleAdsCustomerSchema> =>
- customer !== null,
- );
+ return [...customersById.values()];
}
async listUploadClickConversionActions(customerId: string) {
@@ -372,8 +496,9 @@ export class GoogleAdsApi {
}
}
-// Resolves the login-customer-id header: use the selected account if it's a
-// manager, otherwise the sole accessible manager account (or null if ambiguous).
+// Resolves the login-customer-id header for a selected account.
+// Prefers the manager that actually granted access; does not guess a sole MCC
+// because that 403s when the client isn't under that manager.
export const inferLoginCustomerId = ({
customers,
selectedCustomerId,
@@ -381,6 +506,7 @@ export const inferLoginCustomerId = ({
customers: {
id: string;
manager: boolean;
+ loginCustomerId?: string | null;
}[];
selectedCustomerId: string;
}) => {
@@ -389,19 +515,70 @@ export const inferLoginCustomerId = ({
(customer) => customer.id.replace(/-/g, "") === normalizedSelectedId,
);
- if (selectedCustomer?.manager) {
- return normalizedSelectedId;
+ if (!selectedCustomer) {
+ return null;
}
- const managerAccounts = customers.filter((customer) => customer.manager);
+ if (selectedCustomer.loginCustomerId) {
+ return selectedCustomer.loginCustomerId.replace(/-/g, "");
+ }
- if (managerAccounts.length === 1) {
- return managerAccounts[0].id.replace(/-/g, "");
+ if (selectedCustomer.manager) {
+ return normalizedSelectedId;
}
return null;
};
+// login-customer-id candidates to try when the stored/inferred value 403s.
+export const getLoginCustomerIdCandidates = ({
+ customers,
+ selectedCustomerId,
+}: {
+ customers: {
+ id: string;
+ manager: boolean;
+ loginCustomerId?: string | null;
+ }[];
+ selectedCustomerId: string;
+}) => {
+ const managerIds = customers
+ .filter((customer) => {
+ if (!customer.manager) {
+ return false;
+ }
+
+ const id = customer.id.replace(/-/g, "");
+ const loginCustomerId = customer.loginCustomerId?.replace(/-/g, "");
+
+ // Sub-managers discovered via hierarchy aren't valid login-customer-ids.
+ return !loginCustomerId || loginCustomerId === id;
+ })
+ .map((customer) => customer.id.replace(/-/g, ""));
+
+ const candidates: (string | null)[] = [
+ inferLoginCustomerId({ customers, selectedCustomerId }),
+ null,
+ ...managerIds,
+ ];
+
+ const seen = new Set<string>();
+ const unique: (string | null)[] = [];
+
+ for (const candidate of candidates) {
+ const key = candidate ?? "";
+
+ if (seen.has(key)) {
+ continue;
+ }
+
+ seen.add(key);
+ unique.push(candidate);
+ }
+
+ return unique;
+};
+
// Formats a date as RFC 3339 for Data Manager API event uploads.
const formatGoogleAdsEventTimestamp = (input: string | Date) => {
const date = typeof input === "string" ? new Date(input) : input;
diff --git a/apps/web/lib/integrations/google-ads/schema.ts b/apps/web/lib/integrations/google-ads/schema.ts
index 93920941e6..1f07a88022 100644
--- a/apps/web/lib/integrations/google-ads/schema.ts
+++ b/apps/web/lib/integrations/google-ads/schema.ts
@@ -14,6 +14,8 @@ export const googleAdsCustomerSchema = z.object({
resourceName: z.string(),
descriptiveName: z.string(),
manager: z.boolean(),
+ // Manager account to send as login-customer-id. Null means direct access.
+ loginCustomerId: z.string().nullish(),
});
export const googleAdsSettingsSchema = z.object({
diff --git a/apps/web/lib/integrations/google-ads/update-google-ads-settings.ts b/apps/web/lib/integrations/google-ads/update-google-ads-settings.ts
index 417f090ccd..0123053c34 100644
--- a/apps/web/lib/integrations/google-ads/update-google-ads-settings.ts
+++ b/apps/web/lib/integrations/google-ads/update-google-ads-settings.ts
@@ -21,6 +21,7 @@ export const updateGoogleAdsSettingsAction = authActionClient
const {
customerId,
customerName,
+ loginCustomerId: submittedLoginCustomerId,
leadConversionAction,
saleConversionAction,
} = parsedInput;
@@ -67,7 +68,8 @@ export const updateGoogleAdsSettingsAction = authActionClient
}
const resolvedLoginCustomerId = customerId
- ? inferLoginCustomerId({
+ ? submittedLoginCustomerId?.replace(/-/g, "") ||
+ inferLoginCustomerId({
customers: currentSettings.customers,
selectedCustomerId: customerId,
})