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): track user payment status (#5101)

teo4cdee938d142
diff --git a/apps/web/src/lib/analytics/analytics.ts b/apps/web/src/lib/analytics/analytics.ts
index 5b16456041..875bd90e52 100644
--- a/apps/web/src/lib/analytics/analytics.ts
+++ b/apps/web/src/lib/analytics/analytics.ts
@@ -12,6 +12,7 @@ import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { getPlatform } from '@core/util/platform';
import { PostHog } from 'posthog-js';
import { match } from 'ts-pattern';
+import { getPlanAnalyticsProperties } from './planProperties';
/**
* Resolves the user's device context for analytics enrichment.
@@ -26,6 +27,8 @@ import { match } from 'ts-pattern';
*/
const DEVICE_PROPERTY = 'macro_device' as const;
const ENVIRONMENT_PROPERTY = 'macro_environment' as const;
+const PLAN_TIER_AT_EVENT_PROPERTY = 'plan_tier_at_event' as const;
+const HAS_PAID_ACCESS_AT_EVENT_PROPERTY = 'has_paid_access_at_event' as const;
function getEnvironment(): 'dev' | 'prod' | 'unknown' {
if (PROD_MODE_ENV) return 'prod';
@@ -289,12 +292,27 @@ const createAnalytics = () => {
}
};
+ const setPlanProperties = (licenseStatus: string | undefined) => {
+ if (disabled) return;
+
+ try {
+ const properties = getPlanAnalyticsProperties(licenseStatus);
+
+ posthog.setPersonProperties(properties.person);
+ posthog.register(properties.event);
+ } catch (e) {
+ console.error('[Analytics] Failed to set plan properties:', e);
+ }
+ };
+
const reset = () => {
if (disabled) return;
try {
gtag('config', GA_ID, { user_id: undefined });
+ posthog.unregister(PLAN_TIER_AT_EVENT_PROPERTY);
+ posthog.unregister(HAS_PAID_ACCESS_AT_EVENT_PROPERTY);
posthog.reset();
} catch (e) {
console.error('[Analytics] Failed to reset:', e);
@@ -343,6 +361,7 @@ const createAnalytics = () => {
trackMeta,
trackGoogleConversion,
identify,
+ setPlanProperties,
reset,
pageView,
};
@@ -361,6 +380,7 @@ export type AnalyticsInterface = {
data?: { value?: number; currency?: string; transaction_id?: string }
) => void;
identify: (userID: string, info: Partial<UserIdentifyInfo>) => void;
+ setPlanProperties: (licenseStatus: string | undefined) => void;
reset: () => void;
pageView: (pageTitle: string, opts?: PageViewOptions) => void;
};
diff --git a/apps/web/src/lib/analytics/planProperties.test.ts b/apps/web/src/lib/analytics/planProperties.test.ts
new file mode 100644
index 0000000000..5c63dadf00
--- /dev/null
+++ b/apps/web/src/lib/analytics/planProperties.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from 'vitest';
+import { getPlanAnalyticsProperties } from './planProperties';
+
+describe('getPlanAnalyticsProperties', () => {
+ it.each(['active', 'trialing'])('%s grants premium access', (status) => {
+ expect(getPlanAnalyticsProperties(status)).toEqual({
+ person: {
+ plan_tier: 'premium',
+ has_paid_access: true,
+ },
+ event: {
+ plan_tier_at_event: 'premium',
+ has_paid_access_at_event: true,
+ },
+ });
+ });
+
+ it.each([
+ 'inactive',
+ 'past_due',
+ 'canceled',
+ undefined,
+ ])('%s does not grant premium access', (status) => {
+ expect(getPlanAnalyticsProperties(status)).toEqual({
+ person: {
+ plan_tier: 'free',
+ has_paid_access: false,
+ },
+ event: {
+ plan_tier_at_event: 'free',
+ has_paid_access_at_event: false,
+ },
+ });
+ });
+});
diff --git a/apps/web/src/lib/analytics/planProperties.ts b/apps/web/src/lib/analytics/planProperties.ts
new file mode 100644
index 0000000000..6d6b7e6d49
--- /dev/null
+++ b/apps/web/src/lib/analytics/planProperties.ts
@@ -0,0 +1,39 @@
+/** The product plan exposed to PostHog. */
+export type AnalyticsPlanTier = 'free' | 'premium';
+
+/** PostHog properties derived from the user's authoritative license status. */
+export type PlanAnalyticsProperties = {
+ person: {
+ plan_tier: AnalyticsPlanTier;
+ has_paid_access: boolean;
+ };
+ event: {
+ plan_tier_at_event: AnalyticsPlanTier;
+ has_paid_access_at_event: boolean;
+ };
+};
+
+/**
+ * Maps the auth service's license status to stable PostHog properties.
+ *
+ * `licenseStatus` represents premium entitlement, including personal, team,
+ * and enterprise access. Unknown statuses fail closed to the free plan.
+ */
+export function getPlanAnalyticsProperties(
+ licenseStatus: string | undefined
+): PlanAnalyticsProperties {
+ const hasPaidAccess =
+ licenseStatus === 'active' || licenseStatus === 'trialing';
+ const planTier = hasPaidAccess ? 'premium' : 'free';
+
+ return {
+ person: {
+ plan_tier: planTier,
+ has_paid_access: hasPaidAccess,
+ },
+ event: {
+ plan_tier_at_event: planTier,
+ has_paid_access_at_event: hasPaidAccess,
+ },
+ };
+}
diff --git a/apps/web/src/routes/Root.tsx b/apps/web/src/routes/Root.tsx
index d908afea9b..9024b1e971 100644
--- a/apps/web/src/routes/Root.tsx
+++ b/apps/web/src/routes/Root.tsx
@@ -495,6 +495,7 @@ function UserInfoSideEffects() {
systemThemeEffect();
let identified = false;
+ let syncedPlanKey: string | undefined;
createEffect(
on(userInfo, (user) => {
// Keep Datadog log user context in sync with auth state: set on every
@@ -508,7 +509,10 @@ function UserInfoSideEffects() {
clearDatadogUser();
}
- if (!user || !user.authenticated) return;
+ if (!user || !user.authenticated) {
+ syncedPlanKey = undefined;
+ return;
+ }
if (!posthog.instance._isIdentified() && !identified) {
identified = true;
@@ -522,6 +526,12 @@ function UserInfoSideEffects() {
});
}
+ const planKey = `${user.id}:${user.licenseStatus}`;
+ if (syncedPlanKey !== planKey) {
+ syncedPlanKey = planKey;
+ analytics.setPlanProperties(user.licenseStatus);
+ }
+
// Fires sign_up + ad conversions once when the auth service flagged this
// session as a freshly created account (signed_up=true redirect param).
trackSignupCompletion(analytics, { id: user.id });
diff --git a/apps/web/vitest.config.ts b/apps/web/vitest.config.ts
index 4b7b14a8d2..d41e53cf58 100644
--- a/apps/web/vitest.config.ts
+++ b/apps/web/vitest.config.ts
@@ -164,6 +164,7 @@ export default defineConfig({
include: [
'src/components/**/*.{test,spec}.{ts,tsx}',
'src/features/**/*.{test,spec}.{ts,tsx}',
+ 'src/lib/analytics/**/*.{test,spec}.{ts,tsx}',
'src/routes/**/*.{test,spec}.{ts,tsx}',
],
name: 'app',