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 AF webhook & v3_usage pipe

Steven Tey520c731eb5fd
diff --git a/apps/web/app/(ee)/api/appsflyer/webhook/route.ts b/apps/web/app/(ee)/api/appsflyer/webhook/route.ts
index 4456c23ca2..6da239323a 100644
--- a/apps/web/app/(ee)/api/appsflyer/webhook/route.ts
+++ b/apps/web/app/(ee)/api/appsflyer/webhook/route.ts
@@ -4,6 +4,7 @@ import { isLocalDev } from "@/lib/api/environment";
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { getIP } from "@/lib/api/utils/get-ip";
import { withAxiom } from "@/lib/axiom/server";
+import { appsflyerAmountToDubCents } from "@/lib/integrations/appsflyer/amount-to-dub-cents";
import { APPSFLYER_IP_RANGES } from "@/lib/integrations/appsflyer/constants";
import { isIpInRange } from "@/lib/middleware/utils/is-ip-in-range";
import { trackLeadRequestSchema } from "@/lib/zod/schemas/leads";
@@ -101,10 +102,11 @@ export const GET = withAxiom(async (req) => {
// Track sale event
if (partnerEventId === "sale") {
+ const amountInCents = appsflyerAmountToDubCents(queryParams.amount);
const { eventName, customerExternalId, amount, currency, invoiceId } =
trackSaleRequestSchema.parse({
...queryParams,
- ...(queryParams.amount && { amount: Number(queryParams.amount) }),
+ ...(amountInCents !== undefined && { amount: amountInCents }),
});
await trackSale({
diff --git a/apps/web/lib/integrations/appsflyer/amount-to-dub-cents.ts b/apps/web/lib/integrations/appsflyer/amount-to-dub-cents.ts
new file mode 100644
index 0000000000..9a7282b42f
--- /dev/null
+++ b/apps/web/lib/integrations/appsflyer/amount-to-dub-cents.ts
@@ -0,0 +1,25 @@
+/**
+ * Dub's track-sale API expects `amount` in minor units (e.g. cents for USD).
+ * AppsFlyer postbacks usually send revenue in major units with a decimal (e.g. "0.52").
+ * If the param string contains ".", treat as major units and convert to cents.
+ * Otherwise treat as already in minor units (e.g. "2900" cents).
+ */
+export function appsflyerAmountToDubCents(
+ amount: string | undefined,
+): number | undefined {
+ if (amount == null) {
+ return undefined;
+ }
+ const trimmed = amount.trim();
+ if (trimmed === "") {
+ return undefined;
+ }
+ const n = Number(trimmed);
+ if (!Number.isFinite(n)) {
+ return undefined;
+ }
+ if (trimmed.includes(".")) {
+ return Math.round(n * 100);
+ }
+ return Math.round(n);
+}
diff --git a/packages/tinybird/pipes/v3_usage.pipe b/packages/tinybird/pipes/v3_usage.pipe
index 70151bd6cc..9ff55a557f 100644
--- a/packages/tinybird/pipes/v3_usage.pipe
+++ b/packages/tinybird/pipes/v3_usage.pipe
@@ -112,8 +112,8 @@ SQL >
required=True,
)
}}
- AND created_at >= {{ DateTime(start, '2025-09-03 00:00:00') }}
- AND created_at < {{ DateTime(end, '2025-10-03 00:00:00') }}
+ AND toDateTime64(timestamp, 3) >= {{ DateTime(start, '2025-09-03 00:00:00') }}
+ AND toDateTime64(timestamp, 3) < {{ DateTime(end, '2025-10-03 00:00:00') }}
{% if defined(folderId) %} AND folder_id = {{ folderId }}
{% end %}
{% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}