Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(analytics): fire Meta CompleteRegistration server-side via Conversions API (#4965)

juliawest0a999c6c8c77
diff --git a/apps/web/src/lib/analytics/analytics.ts b/apps/web/src/lib/analytics/analytics.ts
index 38b5de4015..5b16456041 100644
--- a/apps/web/src/lib/analytics/analytics.ts
+++ b/apps/web/src/lib/analytics/analytics.ts
@@ -162,7 +162,8 @@ const createAnalytics = () => {
const sendEvent = (
provider: AnalyticsProvider,
event: EventName,
- data?: Record<string, unknown>
+ data?: Record<string, unknown>,
+ options?: { eventID?: string }
) => {
if (disabled) return;
@@ -181,7 +182,12 @@ const createAnalytics = () => {
const fbqMethod = META_STANDARD_EVENTS.has(event)
? 'track'
: 'trackCustom';
- fbq(fbqMethod, event, enriched);
+ fbq(
+ fbqMethod,
+ event,
+ enriched,
+ options?.eventID ? { eventID: options.eventID } : undefined
+ );
})
.with('posthog', () => {
posthog.capture(event, enriched);
@@ -219,12 +225,18 @@ const createAnalytics = () => {
*
* For the rare event with no standard equivalent, fall back to
* `track(event, data, ['meta-pixel'])` — that path uses `trackCustom`.
+ *
+ * Pass `options.eventID` when the backend fires the same event through the
+ * Conversions API — Meta dedupes browser and server fires that share an
+ * event name + event id, so the pair counts once while keeping the
+ * browser's click-id cookies for attribution.
*/
const trackMeta = (
event: MetaStandardEvent,
- data?: Record<string, unknown>
+ data?: Record<string, unknown>,
+ options?: { eventID?: string }
) => {
- sendEvent('meta-pixel', event, data);
+ sendEvent('meta-pixel', event, data, options);
};
/**
@@ -339,7 +351,11 @@ const createAnalytics = () => {
export type AnalyticsInterface = {
posthog: PostHog;
track: TrackFn;
- trackMeta: (event: MetaStandardEvent, data?: Record<string, unknown>) => void;
+ trackMeta: (
+ event: MetaStandardEvent,
+ data?: Record<string, unknown>,
+ options?: { eventID?: string }
+ ) => void;
trackGoogleConversion: (
action: GoogleConversionAction,
data?: { value?: number; currency?: string; transaction_id?: string }
diff --git a/apps/web/src/lib/analytics/signupCompletion.ts b/apps/web/src/lib/analytics/signupCompletion.ts
index 18d3a02019..0cb326e3b2 100644
--- a/apps/web/src/lib/analytics/signupCompletion.ts
+++ b/apps/web/src/lib/analytics/signupCompletion.ts
@@ -7,14 +7,21 @@
* sends people). The ad-platform conversions were even further off: they fired
* when the (now optional) interactive tutorial modal closed.
*
- * The authoritative product-analytics `sign_up` (PostHog) is emitted
- * server-side from the create-user webhook, so every creation path counts
- * exactly once even if the user never returns to the app. What remains here
- * is the browser-only half: the ad-platform conversions (Meta pixel, Google
- * Ads) plus the GA sign_up event, which need the click-id cookies and consent
- * context that only exist in the browser. The auth service appends
- * `signed_up=true` to the post-auth redirect; this module consumes that param
- * once and fires them, regardless of which page or device started the flow.
+ * The authoritative signup events are emitted server-side from the
+ * create-user webhook, so every creation path counts exactly once even if
+ * the user never returns to the app: `sign_up` (PostHog) and
+ * `CompleteRegistration` (Meta Conversions API). Only a minority of new
+ * accounts ever load the web app with the `signed_up=true` marker, so a
+ * browser-only Meta fire undercounts badly — and Meta's delivery algorithm
+ * starves when spend scales past the trickle it can see.
+ *
+ * What remains here is the browser half: the GA sign_up event and Google Ads
+ * conversion (browser-only), plus a duplicate Meta CompleteRegistration that
+ * exists to contribute the click-id cookies (_fbc/_fbp) only the browser
+ * holds — Meta dedupes it against the server fire via the shared
+ * `signup:{user_id}` event id. The auth service appends `signed_up=true` to
+ * the post-auth redirect; this module consumes that param once and fires
+ * them, regardless of which page or device started the flow.
*/
import type { AnalyticsInterface } from './analytics';
import {
@@ -115,12 +122,22 @@ export function trackSignupCompletion(
'free';
const value = SIGNUP_LEAD_VALUE_BY_TIER[tier] ?? SIGNUP_LEAD_VALUE_DEFAULT;
- analytics.trackMeta('CompleteRegistration', {
- content_name: 'account_created',
- content_category: tier,
- value,
- currency: 'USD',
- });
+ analytics.trackMeta(
+ 'CompleteRegistration',
+ // Must mirror the server-side payload in create_user_webhook.rs exactly:
+ // the create-user webhook fires this same event through the Conversions
+ // API with the same event id, and Meta keeps whichever of the deduped
+ // pair lands first (almost always the server). Accounts are always free
+ // at creation, so both sides report the flat signup value; the tier-based
+ // value below stays on the Google conversion, which is browser-only.
+ {
+ content_name: 'account_created',
+ content_category: 'free',
+ value: SIGNUP_LEAD_VALUE_DEFAULT,
+ currency: 'USD',
+ },
+ { eventID: `signup:${user.id}` }
+ );
analytics.trackGoogleConversion('signup', {
value,
currency: 'USD',
diff --git a/services/authentication_service/src/api/webhooks/user/create_user_webhook.rs b/services/authentication_service/src/api/webhooks/user/create_user_webhook.rs
index d3db690b12..0fe2f613b5 100644
--- a/services/authentication_service/src/api/webhooks/user/create_user_webhook.rs
+++ b/services/authentication_service/src/api/webhooks/user/create_user_webhook.rs
@@ -1,3 +1,4 @@
+use analytics_client::{MetaActionSource, MetaUserData};
use anyhow::Context;
use axum::{
extract::{self, State},
@@ -147,14 +148,21 @@ async fn create_user_webhook(ctx: &ApiContext, req: FusionAuthUserWebhook) -> an
.await
.inspect_err(|e| tracing::error!(error=?e, "unable to mark user as just signed up"));
- // Authoritative product-analytics signup event: fired here so every
- // creation path (SSO, passwordless, iOS) counts exactly once, even if the
- // user never returns to the app. The browser fires the ad-platform
- // conversions instead — it holds the click-id cookies. Uses the same
- // distinct id ("macro|{email}") the app identifies with. Fire-and-forget.
+ // Authoritative signup analytics: fired here so every creation path
+ // (SSO, passwordless, iOS) counts exactly once, even if the user never
+ // returns to the app. Meta's CompleteRegistration fires here too
+ // (Conversions API) — only a minority of new accounts ever load the web
+ // app with the `signed_up=true` marker, so the browser pixel alone
+ // misses most signups. The browser still fires the same event with the
+ // shared `signup:{user_id}` event id purely to contribute click-id
+ // cookies for attribution; Meta dedupes the pair on that id (see the
+ // web app's signupCompletion.ts). Uses the same distinct id
+ // ("macro|{email}") the app identifies with. Fire-and-forget.
tokio::spawn({
let analytics_client = ctx.analytics_client.clone();
let user_id = user_id.clone();
+ let email = email.clone();
+ let ip_address = ip_address.clone();
let verified = req.event.user.verified;
let has_organization = organization_id.is_some();
async move {
@@ -169,6 +177,36 @@ async fn create_user_webhook(ctx: &ApiContext, req: FusionAuthUserWebhook) -> an
)
.await
.inspect_err(|e| tracing::warn!(error=?e, "failed to track PostHog sign_up event"));
+
+ let user_data = MetaUserData {
+ email: Some(email),
+ client_ip_address: Some(ip_address),
+ ..Default::default()
+ };
+ let _ = analytics_client
+ .track_meta(
+ "CompleteRegistration",
+ &user_data,
+ MetaActionSource::Website,
+ Some(&format!("signup:{user_id}")),
+ serde_json::json!({
+ // Must mirror the browser payload in the web app's
+ // signupCompletion.ts: Meta keeps whichever of the
+ // deduped pair lands first, so diverging payloads
+ // would report a value that depends on race timing.
+ // Accounts are always free at creation (value =
+ // SIGNUP_LEAD_VALUE_DEFAULT in leadValues.ts); paid
+ // value is tracked separately via the Stripe events.
+ "content_name": "account_created",
+ "content_category": "free",
+ "value": 20,
+ "currency": "USD",
+ }),
+ )
+ .await
+ .inspect_err(|e| {
+ tracing::warn!(error=?e, "failed to track Meta CompleteRegistration");
+ });
}
});