Skip to content

Commit 520c731

Browse files
committed
fix AF webhook & v3_usage pipe
1 parent 4e7e141 commit 520c731

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

apps/web/app/(ee)/api/appsflyer/webhook/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isLocalDev } from "@/lib/api/environment";
44
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
55
import { getIP } from "@/lib/api/utils/get-ip";
66
import { withAxiom } from "@/lib/axiom/server";
7+
import { appsflyerAmountToDubCents } from "@/lib/integrations/appsflyer/amount-to-dub-cents";
78
import { APPSFLYER_IP_RANGES } from "@/lib/integrations/appsflyer/constants";
89
import { isIpInRange } from "@/lib/middleware/utils/is-ip-in-range";
910
import { trackLeadRequestSchema } from "@/lib/zod/schemas/leads";
@@ -101,10 +102,11 @@ export const GET = withAxiom(async (req) => {
101102

102103
// Track sale event
103104
if (partnerEventId === "sale") {
105+
const amountInCents = appsflyerAmountToDubCents(queryParams.amount);
104106
const { eventName, customerExternalId, amount, currency, invoiceId } =
105107
trackSaleRequestSchema.parse({
106108
...queryParams,
107-
...(queryParams.amount && { amount: Number(queryParams.amount) }),
109+
...(amountInCents !== undefined && { amount: amountInCents }),
108110
});
109111

110112
await trackSale({
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Dub's track-sale API expects `amount` in minor units (e.g. cents for USD).
3+
* AppsFlyer postbacks usually send revenue in major units with a decimal (e.g. "0.52").
4+
* If the param string contains ".", treat as major units and convert to cents.
5+
* Otherwise treat as already in minor units (e.g. "2900" cents).
6+
*/
7+
export function appsflyerAmountToDubCents(
8+
amount: string | undefined,
9+
): number | undefined {
10+
if (amount == null) {
11+
return undefined;
12+
}
13+
const trimmed = amount.trim();
14+
if (trimmed === "") {
15+
return undefined;
16+
}
17+
const n = Number(trimmed);
18+
if (!Number.isFinite(n)) {
19+
return undefined;
20+
}
21+
if (trimmed.includes(".")) {
22+
return Math.round(n * 100);
23+
}
24+
return Math.round(n);
25+
}

packages/tinybird/pipes/v3_usage.pipe

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ SQL >
112112
required=True,
113113
)
114114
}}
115-
AND created_at >= {{ DateTime(start, '2025-09-03 00:00:00') }}
116-
AND created_at < {{ DateTime(end, '2025-10-03 00:00:00') }}
115+
AND toDateTime64(timestamp, 3) >= {{ DateTime(start, '2025-09-03 00:00:00') }}
116+
AND toDateTime64(timestamp, 3) < {{ DateTime(end, '2025-10-03 00:00:00') }}
117117
{% if defined(folderId) %} AND folder_id = {{ folderId }}
118118
{% end %}
119119
{% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}

0 commit comments

Comments
 (0)