Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(analytics): meta ids not forwarded through checkout (#2471)
dev-rbe02ef434594a
diff --git a/js/app/packages/service-clients/service-auth/client.ts b/js/app/packages/service-clients/service-auth/client.tsindex 2223627db7..af53ff70e4 100644--- a/js/app/packages/service-clients/service-auth/client.ts+++ b/js/app/packages/service-clients/service-auth/client.ts@@ -421,7 +421,11 @@ export const authServiceClient = {successUrl: string;cancelUrl: string;discount?: string | null;- gaClientId?: string | null;+ metadata?: {+ gaClientId?: string | null;+ fbp?: string | null;+ fbc?: string | null;+ };tier?: string;}) {return mapOk(@@ -431,7 +435,7 @@ export const authServiceClient = {successUrl: args.successUrl,cancelUrl: args.cancelUrl,discount: args.discount ?? undefined,- gaClientId: args.gaClientId ?? undefined,+ metadata: args.metadata,tier: args.tier ?? undefined,}),}),diff --git a/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadata.ts b/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadata.tsnew file mode 100644index 0000000000..ea322cee93--- /dev/null+++ b/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadata.ts@@ -0,0 +1,21 @@+/**+ * Generated by orval v7.13.0 🍺+ * Do not edit manually.+ * authentication_service+ * OpenAPI spec version: 0.1.0+ */+import type { CheckoutSessionMetadataFbc } from './checkoutSessionMetadataFbc';+import type { CheckoutSessionMetadataFbp } from './checkoutSessionMetadataFbp';+import type { CheckoutSessionMetadataGaClientId } from './checkoutSessionMetadataGaClientId';++/**+ * Tracking metadata for conversion attribution+ */+export interface CheckoutSessionMetadata {+ /** Meta (Facebook) click ID from _fbc cookie */+ fbc?: CheckoutSessionMetadataFbc;+ /** Meta (Facebook) browser ID from _fbp cookie */+ fbp?: CheckoutSessionMetadataFbp;+ /** Google Analytics client ID for conversion tracking */+ gaClientId?: CheckoutSessionMetadataGaClientId;+}diff --git a/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadataFbc.ts b/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadataFbc.tsnew file mode 100644index 0000000000..6fc96056d6--- /dev/null+++ b/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadataFbc.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.13.0 🍺+ * Do not edit manually.+ * authentication_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * Meta (Facebook) click ID from _fbc cookie+ */+export type CheckoutSessionMetadataFbc = string | null;diff --git a/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadataFbp.ts b/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadataFbp.tsnew file mode 100644index 0000000000..8984ca2ac8--- /dev/null+++ b/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadataFbp.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.13.0 🍺+ * Do not edit manually.+ * authentication_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * Meta (Facebook) browser ID from _fbp cookie+ */+export type CheckoutSessionMetadataFbp = string | null;diff --git a/js/app/packages/service-clients/service-auth/generated/schemas/createCheckoutSessionRequestGaClientId.ts b/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadataGaClientId.tssimilarity index 73%rename from js/app/packages/service-clients/service-auth/generated/schemas/createCheckoutSessionRequestGaClientId.tsrename to js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadataGaClientId.tsindex 6196a931f9..f3ba879001 100644--- a/js/app/packages/service-clients/service-auth/generated/schemas/createCheckoutSessionRequestGaClientId.ts+++ b/js/app/packages/service-clients/service-auth/generated/schemas/checkoutSessionMetadataGaClientId.ts@@ -8,4 +8,4 @@/*** Google Analytics client ID for conversion tracking*/-export type CreateCheckoutSessionRequestGaClientId = string | null;+export type CheckoutSessionMetadataGaClientId = string | null;diff --git a/js/app/packages/service-clients/service-auth/generated/schemas/createCheckoutSessionRequest.ts b/js/app/packages/service-clients/service-auth/generated/schemas/createCheckoutSessionRequest.tsindex e0eec51b82..de50f9c660 100644--- a/js/app/packages/service-clients/service-auth/generated/schemas/createCheckoutSessionRequest.ts+++ b/js/app/packages/service-clients/service-auth/generated/schemas/createCheckoutSessionRequest.ts@@ -4,8 +4,9 @@* authentication_service* OpenAPI spec version: 0.1.0*/++import type { CheckoutSessionMetadata } from './checkoutSessionMetadata';import type { CreateCheckoutSessionRequestDiscount } from './createCheckoutSessionRequestDiscount';-import type { CreateCheckoutSessionRequestGaClientId } from './createCheckoutSessionRequestGaClientId';import type { StripeProductTier } from './stripeProductTier';/**@@ -16,8 +17,8 @@ export interface CreateCheckoutSessionRequest {cancelUrl: string;/** Optional discount/promo code to apply */discount?: CreateCheckoutSessionRequestDiscount;- /** Google Analytics client ID for conversion tracking */- gaClientId?: CreateCheckoutSessionRequestGaClientId;+ /** Tracking metadata for conversion attribution */+ metadata?: CheckoutSessionMetadata;/** The URL to redirect to on successful checkout */successUrl: string;/** The tier, defaults to haiku */diff --git a/js/app/packages/service-clients/service-auth/generated/schemas/index.ts b/js/app/packages/service-clients/service-auth/generated/schemas/index.tsindex ae60be3116..f7631f2170 100644--- a/js/app/packages/service-clients/service-auth/generated/schemas/index.ts+++ b/js/app/packages/service-clients/service-auth/generated/schemas/index.ts@@ -7,10 +7,13 @@export * from './aBGroup';export * from './appleLoginRequest';+export * from './checkoutSessionMetadata';+export * from './checkoutSessionMetadataFbc';+export * from './checkoutSessionMetadataFbp';+export * from './checkoutSessionMetadataGaClientId';export * from './createAccountMergeRequest';export * from './createCheckoutSessionRequest';export * from './createCheckoutSessionRequestDiscount';-export * from './createCheckoutSessionRequestGaClientId';export * from './createInProgressLinkResponse';export * from './createPortalSessionRequest';export * from './createTeamRequest';diff --git a/js/app/packages/service-clients/service-auth/openapi.json b/js/app/packages/service-clients/service-auth/openapi.jsonindex fa56de575c..2ca3b35b1e 100644--- a/js/app/packages/service-clients/service-auth/openapi.json+++ b/js/app/packages/service-clients/service-auth/openapi.json@@ -3061,6 +3061,24 @@}}},+ "CheckoutSessionMetadata": {+ "type": "object",+ "description": "Tracking metadata for conversion attribution",+ "properties": {+ "fbc": {+ "type": ["string", "null"],+ "description": "Meta (Facebook) click ID from _fbc cookie"+ },+ "fbp": {+ "type": ["string", "null"],+ "description": "Meta (Facebook) browser ID from _fbp cookie"+ },+ "gaClientId": {+ "type": ["string", "null"],+ "description": "Google Analytics client ID for conversion tracking"+ }+ }+ },"CreateAccountMergeRequest": {"type": "object","required": ["email"],@@ -3084,9 +3102,9 @@"type": ["string", "null"],"description": "Optional discount/promo code to apply"},- "gaClientId": {- "type": ["string", "null"],- "description": "Google Analytics client ID for conversion tracking"+ "metadata": {+ "$ref": "#/components/schemas/CheckoutSessionMetadata",+ "description": "Tracking metadata for conversion attribution"},"successUrl": {"type": "string",diff --git a/js/app/packages/service-clients/service-stripe/client.ts b/js/app/packages/service-clients/service-stripe/client.tsindex e9573bbc84..abd5a939bb 100644--- a/js/app/packages/service-clients/service-stripe/client.ts+++ b/js/app/packages/service-clients/service-stripe/client.ts@@ -2,6 +2,16 @@ import { isOk } from '@core/util/maybeResult';import { registerClient } from '@core/util/mockClient';import { authServiceClient } from '@service-auth/client';+/**+ * Gets Meta _fbp (browser ID) and _fbc (click ID) values from cookies set by the Meta Pixel.+ */+function getMetaIds(): { fbp: string | undefined; fbc: string | undefined } {+ const cookies = document.cookie;+ const fbp = cookies.match(/(?:^|; )_fbp=([^;]*)/)?.[1];+ const fbc = cookies.match(/(?:^|; )_fbc=([^;]*)/)?.[1];+ return { fbp, fbc };+}+/*** Gets the Google Analytics client ID using gtag.* Returns a promise that resolves with the client ID or undefined if GA is blocked/unavailable.@@ -34,12 +44,17 @@ export const stripeServiceClient = {tier?: string) => {const gaClientId = await getGaClientId();+ const { fbp, fbc } = getMetaIds();const result = await authServiceClient.createCheckoutSession({successUrl: `${window.location.origin}/app/?subscriptionSuccess=true${type ? `&type=${type}` : ''}`,cancelUrl: `${window.location.origin}/app`,discount: discount ?? null,- gaClientId: gaClientId ?? null,+ metadata: {+ gaClientId: gaClientId ?? null,+ fbp: fbp ?? null,+ fbc: fbc ?? null,+ },tier,});diff --git a/rust/cloud-storage/authentication_service/src/api/user/stripe.rs b/rust/cloud-storage/authentication_service/src/api/user/stripe.rsindex ac76a5eddb..5578635252 100644--- a/rust/cloud-storage/authentication_service/src/api/user/stripe.rs+++ b/rust/cloud-storage/authentication_service/src/api/user/stripe.rs@@ -58,6 +58,18 @@ pub enum StripeProductTier {Opus,}+/// Tracking metadata for conversion attribution+#[derive(Debug, Default, Deserialize, ToSchema)]+#[serde(rename_all = "camelCase")]+pub struct CheckoutSessionMetadata {+ /// Google Analytics client ID for conversion tracking+ pub ga_client_id: Option<String>,+ /// Meta (Facebook) browser ID from _fbp cookie+ pub fbp: Option<String>,+ /// Meta (Facebook) click ID from _fbc cookie+ pub fbc: Option<String>,+}+/// Request body for creating a Stripe checkout session#[derive(Debug, Deserialize, ToSchema)]#[serde(rename_all = "camelCase")]@@ -68,8 +80,9 @@ pub struct CreateCheckoutSessionRequest {pub cancel_url: String,/// Optional discount/promo code to applypub discount: Option<String>,- /// Google Analytics client ID for conversion tracking- pub ga_client_id: Option<String>,+ /// Tracking metadata for conversion attribution+ #[serde(default)]+ pub metadata: CheckoutSessionMetadata,/// The tier, defaults to haiku#[serde(default)]pub tier: StripeProductTier,@@ -166,9 +179,15 @@ pub async fn create_checkout_session(// Build subscription metadata from optional tracking fieldslet mut metadata = std::collections::HashMap::new();- if let Some(ga_client_id) = req.ga_client_id {+ if let Some(ga_client_id) = req.metadata.ga_client_id {metadata.insert("ga_client_id".to_string(), ga_client_id);}+ if let Some(fbp) = req.metadata.fbp {+ metadata.insert("fbp".to_string(), fbp);+ }+ if let Some(fbc) = req.metadata.fbc {+ metadata.insert("fbc".to_string(), fbc);+ }// Only set subscription_data if we have metadata to includelet subscription_data =diff --git a/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs b/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rsindex 32eff865f2..1b4a49187f 100644--- a/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs+++ b/rust/cloud-storage/authentication_service/src/api/webhooks/user/stripe_webhook.rs@@ -221,14 +221,18 @@ async fn handle_customer_subscription_event(None};- // Extract GA client ID from subscription metadata for analytics tracking+ // Extract tracking IDs from subscription metadata for analyticslet ga_client_id = subscription.metadata.get("ga_client_id").cloned();+ let fbp = subscription.metadata.get("fbp").cloned();+ let fbc = subscription.metadata.get("fbc").cloned();tracing::info!(email=%email.as_ref(),subscription_id,subscription_status,ga_client_id=?ga_client_id,+ fbp=?fbp,+ fbc=?fbc,"processing stripe subscription");@@ -250,6 +254,8 @@ async fn handle_customer_subscription_event(&team_id,SubscriptionTrackingData {ga_client_id: ga_client_id.clone(),+ fbp: fbp.clone(),+ fbc: fbc.clone(),email: email.as_ref().to_string(),value_cents: subscription_value,currency: subscription_currency,@@ -377,6 +383,8 @@ async fn handle_customer_subscription_event(subscription_id,SubscriptionTrackingData {ga_client_id,+ fbp,+ fbc,email: email.as_ref().to_string(),value_cents: subscription_value,currency: subscription_currency,@@ -477,6 +485,8 @@ struct SubscriptionEvent {#[derive(Debug, Clone)]struct SubscriptionTrackingData {ga_client_id: Option<String>,+ fbp: Option<String>,+ fbc: Option<String>,email: String,value_cents: Option<i64>,currency: Option<String>,@@ -523,7 +533,11 @@ fn track_stripe_subscription(value: value_cents as f64 / 100.0,currency: currency.to_uppercase(),};- let user_data = MetaUserData::with_email(&data.email);+ let user_data = MetaUserData {+ email: Some(data.email.clone()),+ fbp: data.fbp.clone(),+ fbc: data.fbc.clone(),+ };let event_id = Some(subscription_id.as_str());tracing::info!(