AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(analytics): activation, retention, unit-economics and experiment views (#14290)
Nicholas Tindlee524713659e1
diff --git a/autogpt_platform/analytics/queries/agent_health.sql b/autogpt_platform/analytics/queries/agent_health.sqlnew file mode 100644index 0000000000..017843e52e--- /dev/null+++ b/autogpt_platform/analytics/queries/agent_health.sql@@ -0,0 +1,136 @@+-- =============================================================+-- View: analytics.agent_health+-- Looker source alias: (new) | Charts: 0+-- =============================================================+-- DESCRIPTION+-- One row per agent a user has in their library (not deleted or+-- archived): is it running, is it idle, is it failing. This is+-- where "agent_idle" and "agent_fail" come from. An agent that a+-- user added but never ran, or that stopped running, is the+-- earliest churn signal we have.+--+-- SOURCE TABLES+-- platform.LibraryAgent — the user's agents (lastRunAt)+-- platform.AgentGraph — agent name+-- platform.AgentGraphExecution — run outcomes (root, non-dry runs)+-- platform.ActivityEvent — schedule.created per graph (objectId)+--+-- OUTPUT COLUMNS+-- user_id TEXT Owner+-- graph_id TEXT Agent graph UUID+-- library_agent_id TEXT LibraryAgent UUID+-- agent_name TEXT Display name+-- is_created_by_user BOOLEAN Built by the user (vs. added from marketplace)+-- added_at TIMESTAMPTZ When it entered the library+-- last_run_at TIMESTAMPTZ LibraryAgent.lastRunAt: last run of any kind,+-- simulations included (nullable)+-- last_completed_at TIMESTAMPTZ Last successful run+-- last_failed_at TIMESTAMPTZ Last failed run+-- runs_total BIGINT Lifetime root runs+-- runs_7d / runs_30d BIGINT Runs in the last 7 / 30 days+-- completed_30d BIGINT COMPLETED runs, last 30 days+-- failed_30d BIGINT FAILED runs, last 30 days+-- scheduled_runs_30d BIGINT Runs fired by a schedule, last 30 days+-- webhook_runs_30d BIGINT Runs fired by a webhook, last 30 days+-- fail_rate_30d FLOAT failed / (completed + failed), last 30 days+-- schedules_created_total BIGINT schedule.created events for this graph+-- days_since_last_run INT NULL if never run+-- never_run BOOLEAN No root, non-dry run ever (a Simulate alone+-- bumps last_run_at but keeps never_run true)+-- idle_7d / idle_30d BOOLEAN Has run before, but not in the last 7 / 30 days+-- failing BOOLEAN >= 3 failures and >= 50% fail rate in 30 days+--+-- WINDOW+-- All library agents; run windows are rolling 7 / 30 days+--+-- EXAMPLE QUERIES+-- -- Users with at least one agent that went idle this month+-- SELECT COUNT(DISTINCT user_id) FROM analytics.agent_health WHERE idle_30d;+--+-- -- Share of library agents that never ran+-- SELECT AVG(CASE WHEN never_run THEN 1 ELSE 0 END) FROM analytics.agent_health;+--+-- -- Failing agents to reach out about+-- SELECT user_id, agent_name, failed_30d, fail_rate_30d, last_failed_at+-- FROM analytics.agent_health WHERE failing ORDER BY failed_30d DESC;+-- =============================================================++WITH agents AS (+ SELECT+ la."userId" AS user_id,+ la."agentGraphId" AS graph_id,+ la."id" AS library_agent_id,+ COALESCE(la."name", g."name") AS agent_name,+ la."isCreatedByUser" AS is_created_by_user,+ la."createdAt" AS added_at,+ la."lastRunAt" AS last_run_at+ FROM platform."LibraryAgent" la+ LEFT JOIN platform."AgentGraph" g+ ON g."id" = la."agentGraphId" AND g."version" = la."agentGraphVersion"+ WHERE la."isDeleted" = FALSE+ AND la."isArchived" = FALSE+),+runs AS (+ SELECT+ ge."userId" AS user_id,+ ge."agentGraphId" AS graph_id,+ COUNT(*) AS runs_total,+ COUNT(*) FILTER (WHERE ge."createdAt" > NOW() - INTERVAL '7 days') AS runs_7d,+ COUNT(*) FILTER (WHERE ge."createdAt" > NOW() - INTERVAL '30 days') AS runs_30d,+ COUNT(*) FILTER (WHERE ge."createdAt" > NOW() - INTERVAL '30 days'+ AND ge."executionStatus" = 'COMPLETED') AS completed_30d,+ COUNT(*) FILTER (WHERE ge."createdAt" > NOW() - INTERVAL '30 days'+ AND ge."executionStatus" = 'FAILED') AS failed_30d,+ COUNT(*) FILTER (WHERE ge."createdAt" > NOW() - INTERVAL '30 days'+ AND ge."triggerSource" = 'schedule') AS scheduled_runs_30d,+ COUNT(*) FILTER (WHERE ge."createdAt" > NOW() - INTERVAL '30 days'+ AND ge."triggerSource" = 'webhook') AS webhook_runs_30d,+ MAX(ge."createdAt") FILTER (WHERE ge."executionStatus" = 'COMPLETED') AS last_completed_at,+ MAX(ge."createdAt") FILTER (WHERE ge."executionStatus" = 'FAILED') AS last_failed_at+ FROM platform."AgentGraphExecution" ge+ WHERE ge."isDeleted" = FALSE+ AND ge."parentGraphExecutionId" IS NULL+ AND COALESCE(ge."stats"::jsonb->>'is_dry_run', 'false') <> 'true'+ GROUP BY 1, 2+),+schedules AS (+ SELECT+ "userId" AS user_id,+ "objectId" AS graph_id,+ COUNT(*) FILTER (WHERE "eventType" = 'schedule.created') AS schedules_created_total+ FROM platform."ActivityEvent"+ WHERE "category" = 'SCHEDULE' AND "objectId" IS NOT NULL+ GROUP BY 1, 2+)+SELECT+ a.user_id,+ a.graph_id,+ a.library_agent_id,+ a.agent_name,+ a.is_created_by_user,+ a.added_at,+ a.last_run_at,+ r.last_completed_at,+ r.last_failed_at,+ COALESCE(r.runs_total, 0) AS runs_total,+ COALESCE(r.runs_7d, 0) AS runs_7d,+ COALESCE(r.runs_30d, 0) AS runs_30d,+ COALESCE(r.completed_30d, 0) AS completed_30d,+ COALESCE(r.failed_30d, 0) AS failed_30d,+ COALESCE(r.scheduled_runs_30d, 0) AS scheduled_runs_30d,+ COALESCE(r.webhook_runs_30d, 0) AS webhook_runs_30d,+ COALESCE(r.failed_30d, 0)::float+ / NULLIF(COALESCE(r.completed_30d, 0) + COALESCE(r.failed_30d, 0), 0)+ AS fail_rate_30d,+ COALESCE(s.schedules_created_total, 0) AS schedules_created_total,+ (CURRENT_DATE - a.last_run_at::date) AS days_since_last_run,+ COALESCE(r.runs_total, 0) = 0 AS never_run,+ a.last_run_at IS NOT NULL AND a.last_run_at < NOW() - INTERVAL '7 days' AS idle_7d,+ a.last_run_at IS NOT NULL AND a.last_run_at < NOW() - INTERVAL '30 days' AS idle_30d,+ COALESCE(r.failed_30d, 0) >= 3+ AND COALESCE(r.failed_30d, 0)::float+ / NULLIF(COALESCE(r.completed_30d, 0) + COALESCE(r.failed_30d, 0), 0) >= 0.5+ AS failing+FROM agents a+LEFT JOIN runs r ON r.user_id = a.user_id AND r.graph_id = a.graph_id+LEFT JOIN schedules s ON s.user_id = a.user_id AND s.graph_id = a.graph_iddiff --git a/autogpt_platform/analytics/queries/chat_turn.sql b/autogpt_platform/analytics/queries/chat_turn.sqlnew file mode 100644index 0000000000..a745e26420--- /dev/null+++ b/autogpt_platform/analytics/queries/chat_turn.sql@@ -0,0 +1,76 @@+-- =============================================================+-- View: analytics.chat_turn+-- Looker source alias: (new) | Charts: 0+-- =============================================================+-- DESCRIPTION+-- One row per user-role chat message (last 90 days): the copilot+-- equivalent of a graph execution. This is where "run_autopilot"+-- and "run_expert" come from for Looker, and it is the SQL twin of+-- the PostHog events of the same names.+--+-- Dream/daydream sessions (memory maintenance, not the user) are+-- excluded. Sessions with origin 'automation' are model-authored+-- (scheduled follow-ups fired into a fresh chat); their user-role+-- opener is kept but flagged so human activity can be isolated.+--+-- SOURCE TABLES+-- platform.ChatMessage — messages (role, sequence, model)+-- platform.ChatSession — owner, expert scope, origin/kind metadata+--+-- OUTPUT COLUMNS+-- id TEXT Message UUID+-- createdAt TIMESTAMPTZ When the turn was sent+-- sessionId TEXT Chat session UUID+-- userId TEXT Session owner+-- organizationId TEXT Org the session belongs to (nullable)+-- expertId TEXT Expert the session is scoped to (NULL = Autopilot)+-- surface TEXT 'autopilot' | 'expert'+-- sessionOrigin TEXT 'interactive' | 'automation' (legacy rows -> 'interactive')+-- isHumanTurn BOOLEAN sessionOrigin <> 'automation'+-- sourcePlatform TEXT 'slack' | 'telegram' | 'discord' | NULL (= web chat)+-- sequence INT Position of the message in the session+-- isFirstTurnInSession BOOLEAN First user turn of the session (looks past the window)+-- sessionCreatedAt TIMESTAMPTZ When the session was created+--+-- WINDOW+-- Rolling 90 days (createdAt > CURRENT_DATE - 90 days)+--+-- EXAMPLE QUERIES+-- -- Daily human copilot turns by surface+-- SELECT DATE_TRUNC('day', "createdAt") AS day, surface, COUNT(*)+-- FROM analytics.chat_turn+-- WHERE "isHumanTurn"+-- GROUP BY 1, 2 ORDER BY 1;+--+-- -- Users who talked to an expert this week+-- SELECT COUNT(DISTINCT "userId") FROM analytics.chat_turn+-- WHERE surface = 'expert' AND "isHumanTurn"+-- AND "createdAt" >= DATE_TRUNC('week', CURRENT_DATE);+-- =============================================================++SELECT+ m."id" AS id,+ m."createdAt" AS "createdAt",+ m."sessionId" AS "sessionId",+ s."userId" AS "userId",+ s."organizationId" AS "organizationId",+ s."expertId" AS "expertId",+ CASE WHEN s."expertId" IS NULL THEN 'autopilot' ELSE 'expert' END+ AS surface,+ COALESCE(s."metadata"::jsonb->>'origin', 'interactive') AS "sessionOrigin",+ COALESCE(s."metadata"::jsonb->>'origin', 'interactive') <> 'automation'+ AS "isHumanTurn",+ s."metadata"::jsonb->>'source_platform' AS "sourcePlatform",+ m."sequence" AS sequence,+ NOT EXISTS (+ SELECT 1 FROM platform."ChatMessage" p+ WHERE p."sessionId" = m."sessionId"+ AND p."role" = 'user'+ AND p."sequence" < m."sequence"+ ) AS "isFirstTurnInSession",+ s."createdAt" AS "sessionCreatedAt"+FROM platform."ChatMessage" m+JOIN platform."ChatSession" s ON s."id" = m."sessionId"+WHERE m."role" = 'user'+ AND m."createdAt" > CURRENT_DATE - INTERVAL '90 days'+ AND COALESCE(s."metadata"::jsonb->>'kind', 'normal') <> 'dream'diff --git a/autogpt_platform/analytics/queries/experiment_assignment.sql b/autogpt_platform/analytics/queries/experiment_assignment.sqlnew file mode 100644index 0000000000..f7a2968c0f--- /dev/null+++ b/autogpt_platform/analytics/queries/experiment_assignment.sql@@ -0,0 +1,46 @@+-- =============================================================+-- View: analytics.experiment_assignment+-- Looker source alias: (new) | Charts: 0+-- =============================================================+-- DESCRIPTION+-- One row per (user, experiment): the A/B/C arm the user was+-- bucketed into the first time the experiment was evaluated for+-- them. Join on user_id to split any other view (retention, tasks,+-- unit economics) by arm. PostHog owns significance testing; this+-- is the durable copy so Looker can segment by variant too.+--+-- SOURCE TABLES+-- platform.ExperimentAssignment — first-seen arm per user/experiment+--+-- OUTPUT COLUMNS+-- user_id TEXT User UUID+-- experiment_key TEXT PostHog flag key (e.g. 'subscription-pricing-page-initial-state')+-- variant TEXT Arm label ('control', 'yearly-pro', ...)+-- source TEXT 'posthog' | 'backend'+-- assigned_at TIMESTAMPTZ First time the arm was observed for this user+--+-- WINDOW+-- Full history (assignments are small and never rewritten)+--+-- EXAMPLE QUERIES+-- -- Arm sizes per experiment+-- SELECT experiment_key, variant, COUNT(*) AS users+-- FROM analytics.experiment_assignment+-- GROUP BY 1, 2 ORDER BY 1, 2;+--+-- -- Activation rate by arm+-- SELECT a.variant,+-- AVG(CASE WHEN l.activated THEN 1 ELSE 0 END) AS activation_rate+-- FROM analytics.experiment_assignment a+-- JOIN analytics.user_lifecycle l ON l.user_id = a.user_id+-- WHERE a.experiment_key = 'subscription-pricing-page-initial-state'+-- GROUP BY 1;+-- =============================================================++SELECT+ "userId" AS user_id,+ "experimentKey" AS experiment_key,+ "variant" AS variant,+ "source" AS source,+ "createdAt" AS assigned_at+FROM platform."ExperimentAssignment"diff --git a/autogpt_platform/analytics/queries/graph_execution.sql b/autogpt_platform/analytics/queries/graph_execution.sqlindex e5e1b8f2f5..a5653a0a54 100644--- a/autogpt_platform/analytics/queries/graph_execution.sql+++ b/autogpt_platform/analytics/queries/graph_execution.sql@@ -33,6 +33,16 @@-- correctness_score FLOAT AI correctness score (if available)-- possibly_ai BOOLEAN True if agent has sensitive_action_safe_mode enabled-- groupedErrorMessage TEXT Scrubbed error string (IDs/URLs replaced with wildcards)+-- expertId TEXT Hired expert the run is attributed to (nullable)+-- triggerSource TEXT How the run started: manual | api | schedule | webhook |+-- copilot | subgraph | admin. NULL for rows created+-- before the column existed (Sept 2026).+-- triggerRef TEXT What started it: schedule id, webhook id, chat session id,+-- 'api_key'/'oauth', or the UI surface ('library', 'builder')+-- failureReason TEXT Structured failure reason (e.g. insufficient_balance)+-- isDryRun BOOLEAN LLM-simulated run; exclude from task counts+-- isSubgraphRun BOOLEAN Nested run started by a parent run; exclude from task counts+-- endedAt TIMESTAMPTZ When the run reached a terminal state (nullable)---- WINDOW-- Rolling 90 days (createdAt > CURRENT_DATE - 90 days)@@ -99,7 +109,19 @@ SELECT'\1\2/...', 'gi'),'[a-zA-Z0-9_:-]*\d[a-zA-Z0-9_:-]*', '*', 'g'- ) AS groupedErrorMessage+ ) AS groupedErrorMessage,+ -- Columns below were appended later; CREATE OR REPLACE VIEW only allows+ -- adding columns at the end, so keep new ones here. They are quoted so+ -- the column names stay camelCase as documented (an unquoted alias folds+ -- to lowercase); groupedErrorMessage above predates this and cannot be+ -- renamed in place.+ ge."expertId" AS "expertId",+ ge."triggerSource" AS "triggerSource",+ ge."triggerRef" AS "triggerRef",+ ge."stats"::jsonb->>'failure_reason' AS "failureReason",+ COALESCE((ge."stats"::jsonb->>'is_dry_run')::boolean, FALSE) AS "isDryRun",+ ge."parentGraphExecutionId" IS NOT NULL AS "isSubgraphRun",+ ge."endedAt" AS "endedAt"FROM platform."AgentGraphExecution" geLEFT JOIN platform."AgentGraph" gON ge."agentGraphId" = g."id"diff --git a/autogpt_platform/analytics/queries/retention_task_weekly.sql b/autogpt_platform/analytics/queries/retention_task_weekly.sqlnew file mode 100644index 0000000000..311c8cf1ad--- /dev/null+++ b/autogpt_platform/analytics/queries/retention_task_weekly.sql@@ -0,0 +1,113 @@+-- =============================================================+-- View: analytics.retention_task_weekly+-- Looker source alias: (new) | Charts: 0+-- =============================================================+-- DESCRIPTION+-- Weekly cohort retention where "active" means the user did a+-- task on any surface: a human-started agent run OR a human turn+-- in an Autopilot/expert chat. Cohort anchor = week of the user's+-- first task. This supersedes retention_execution_weekly (agent+-- runs only) now that Autopilot and experts are mainline; use both+-- side by side to see how much retention the copilot surfaces add.+--+-- Automated work (schedule fires, webhook runs, scheduled+-- follow-up turns) does NOT count as activity here: this view+-- measures people coming back, not agents running. Runs created+-- before triggerSource existed are counted as human.+--+-- Only includes cohorts from the last 180 days, up to week 12.+--+-- SOURCE TABLES+-- platform.AgentGraphExecution — agent runs (triggerSource)+-- platform.ChatMessage / ChatSession — copilot turns+--+-- OUTPUT COLUMNS+-- Same pattern as retention_login_weekly:+-- cohort_week_start, cohort_label, cohort_label_n, user_lifetime_week,+-- cohort_users, active_users_bounded, retained_users_unbounded,+-- retention_rate_bounded, retention_rate_unbounded, cohort_users_w0+--+-- EXAMPLE QUERIES+-- -- Week-1 task retention per cohort+-- SELECT cohort_label, retention_rate_bounded+-- FROM analytics.retention_task_weekly+-- WHERE user_lifetime_week = 1 ORDER BY cohort_week_start;+--+-- -- Retention curve, all cohorts combined+-- SELECT user_lifetime_week,+-- SUM(active_users_bounded)::float / NULLIF(SUM(cohort_users_w0), 0) AS avg_retention+-- FROM analytics.retention_task_weekly GROUP BY 1 ORDER BY 1;+-- =============================================================++WITH params AS (SELECT 12::int AS max_weeks, (CURRENT_DATE - INTERVAL '180 days') AS cohort_start),+events AS (+ SELECT e."userId"::text AS user_id, e."createdAt"::timestamptz AS created_at,+ DATE_TRUNC('week', e."createdAt")::date AS week_start+ FROM platform."AgentGraphExecution" e+ WHERE e."userId" IS NOT NULL+ AND e."isDeleted" = FALSE+ AND e."parentGraphExecutionId" IS NULL+ AND COALESCE(e."stats"::jsonb->>'is_dry_run', 'false') <> 'true'+ -- Copilot-started runs are represented by the chat turn that asked for+ -- them (counted below), so they are not a second task here.+ AND (e."triggerSource" IS NULL OR e."triggerSource" IN ('manual', 'api'))+ UNION ALL+ SELECT s."userId"::text, m."createdAt"::timestamptz,+ DATE_TRUNC('week', m."createdAt")::date+ FROM platform."ChatMessage" m+ JOIN platform."ChatSession" s ON s."id" = m."sessionId"+ WHERE m."role" = 'user'+ AND COALESCE(s."metadata"::jsonb->>'kind', 'normal') <> 'dream'+ AND COALESCE(s."metadata"::jsonb->>'origin', 'interactive') <> 'automation'+),+first_task AS (+ SELECT user_id, MIN(created_at) AS first_task_at,+ DATE_TRUNC('week', MIN(created_at))::date AS cohort_week_start+ FROM events GROUP BY 1+ HAVING MIN(created_at) >= (SELECT cohort_start FROM params)+),+activity_weeks AS (SELECT DISTINCT user_id, week_start FROM events),+user_week_age AS (+ SELECT aw.user_id, ft.cohort_week_start,+ ((aw.week_start - DATE_TRUNC('week', ft.first_task_at)::date) / 7)::int AS user_lifetime_week+ FROM activity_weeks aw JOIN first_task ft USING (user_id)+ WHERE aw.week_start >= DATE_TRUNC('week', ft.first_task_at)::date+),+bounded_counts AS (+ SELECT cohort_week_start, user_lifetime_week, COUNT(DISTINCT user_id) AS active_users_bounded+ FROM user_week_age WHERE user_lifetime_week >= 0 GROUP BY 1, 2+),+last_active AS (+ SELECT cohort_week_start, user_id, MAX(user_lifetime_week) AS last_active_week FROM user_week_age GROUP BY 1, 2+),+unbounded_counts AS (+ SELECT la.cohort_week_start, gs AS user_lifetime_week, COUNT(*) AS retained_users_unbounded+ FROM last_active la+ CROSS JOIN LATERAL generate_series(0, LEAST(la.last_active_week, (SELECT max_weeks FROM params))) gs+ GROUP BY 1, 2+),+cohort_sizes AS (SELECT cohort_week_start, COUNT(DISTINCT user_id) AS cohort_users FROM first_task GROUP BY 1),+cohort_caps AS (+ SELECT cs.cohort_week_start, cs.cohort_users,+ LEAST((SELECT max_weeks FROM params),+ GREATEST(0, ((DATE_TRUNC('week', CURRENT_DATE)::date - cs.cohort_week_start) / 7)::int)) AS cap_weeks+ FROM cohort_sizes cs+),+grid AS (+ SELECT cc.cohort_week_start, gs AS user_lifetime_week, cc.cohort_users+ FROM cohort_caps cc CROSS JOIN LATERAL generate_series(0, cc.cap_weeks) gs+)+SELECT+ g.cohort_week_start,+ TO_CHAR(g.cohort_week_start, 'IYYY-"W"IW') AS cohort_label,+ TO_CHAR(g.cohort_week_start, 'IYYY-"W"IW') || ' (n=' || g.cohort_users || ')' AS cohort_label_n,+ g.user_lifetime_week, g.cohort_users,+ COALESCE(b.active_users_bounded, 0) AS active_users_bounded,+ COALESCE(u.retained_users_unbounded, 0) AS retained_users_unbounded,+ CASE WHEN g.cohort_users > 0 THEN COALESCE(b.active_users_bounded, 0)::float / g.cohort_users END AS retention_rate_bounded,+ CASE WHEN g.cohort_users > 0 THEN COALESCE(u.retained_users_unbounded, 0)::float / g.cohort_users END AS retention_rate_unbounded,+ CASE WHEN g.user_lifetime_week = 0 THEN g.cohort_users ELSE 0 END AS cohort_users_w0+FROM grid g+LEFT JOIN bounded_counts b ON b.cohort_week_start = g.cohort_week_start AND b.user_lifetime_week = g.user_lifetime_week+LEFT JOIN unbounded_counts u ON u.cohort_week_start = g.cohort_week_start AND u.user_lifetime_week = g.user_lifetime_week+ORDER BY g.cohort_week_start, g.user_lifetime_weekdiff --git a/autogpt_platform/analytics/queries/unit_economics_monthly.sql b/autogpt_platform/analytics/queries/unit_economics_monthly.sqlnew file mode 100644index 0000000000..884c17cc6c--- /dev/null+++ b/autogpt_platform/analytics/queries/unit_economics_monthly.sql@@ -0,0 +1,196 @@+-- =============================================================+-- View: analytics.unit_economics_monthly+-- Looker source alias: (new) | Charts: 0+-- =============================================================+-- DESCRIPTION+-- One row per (user, calendar month): what the user did, what it+-- cost us in real provider spend, and what we charged them. Built+-- for pricing questions ("what does a free trial with a card on+-- file cost us per user per month?") and margin questions.+--+-- - tasks_human human-started agent runs + human chat turns (a run+-- the copilot started is counted once, via its turn)+-- - tasks_automated schedule / webhook runs + scheduled follow-up turns+-- - platform_cost_usd our provider cost (PlatformCostLog), split into+-- agent / copilot / background (dream passes)+-- - credits_spent_usd what we charged in credits (USAGE)+-- - gross_margin_usd credits_spent_usd - platform_cost_usd (approximate:+-- credits are prepaid, cost is incurred at use)+-- - cost_per_task_usd platform_cost_usd / (tasks_human + tasks_automated)+--+-- subscription_tier is the user's CURRENT tier, not a snapshot+-- (there is no tier history table). Sum across users for platform+-- totals; average for "per user".+--+-- SOURCE TABLES+-- platform.AgentGraphExecution, platform.ChatMessage/ChatSession,+-- platform.PlatformCostLog, platform.CreditTransaction, platform.User+--+-- OUTPUT COLUMNS+-- user_id, email, subscription_tier, month (DATE, first of month),+-- tasks_human, tasks_automated, agent_runs, autopilot_turns, expert_turns,+-- active_days, platform_cost_usd, agent_cost_usd, copilot_cost_usd,+-- background_cost_usd, credits_spent_usd, credits_purchased_usd,+-- gross_margin_usd, cost_per_task_usd, cost_per_active_day_usd+--+-- WINDOW+-- Rolling 12 months+--+-- EXAMPLE QUERIES+-- -- Average cost to us per active user per month, by tier+-- SELECT month, subscription_tier,+-- AVG(platform_cost_usd) AS avg_cost_usd,+-- PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY platform_cost_usd) AS p90_cost_usd,+-- COUNT(*) AS users+-- FROM analytics.unit_economics_monthly+-- WHERE tasks_human + tasks_automated > 0+-- GROUP BY 1, 2 ORDER BY 1, 2;+--+-- -- Platform-wide margin per month+-- SELECT month, SUM(credits_spent_usd) AS charged, SUM(platform_cost_usd) AS cost,+-- SUM(gross_margin_usd) AS margin+-- FROM analytics.unit_economics_monthly GROUP BY 1 ORDER BY 1;+-- =============================================================++WITH runs AS (+ SELECT+ ge."userId" AS user_id,+ DATE_TRUNC('month', ge."createdAt")::date AS month,+ COUNT(*) AS agent_runs,+ COUNT(*) FILTER (WHERE ge."triggerSource" IS NULL+ OR ge."triggerSource" IN ('manual', 'api', 'copilot'))+ AS agent_runs_human,+ COUNT(*) FILTER (WHERE ge."triggerSource" IN ('schedule', 'webhook'))+ AS agent_runs_automated,+ COUNT(*) FILTER (WHERE ge."triggerSource" = 'copilot') AS agent_runs_copilot+ FROM platform."AgentGraphExecution" ge+ WHERE ge."createdAt" > DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '11 months'+ AND ge."isDeleted" = FALSE+ AND ge."parentGraphExecutionId" IS NULL+ AND COALESCE(ge."stats"::jsonb->>'is_dry_run', 'false') <> 'true'+ GROUP BY 1, 2+),+turns AS (+ SELECT+ s."userId" AS user_id,+ DATE_TRUNC('month', m."createdAt")::date AS month,+ COUNT(*) FILTER (WHERE s."expertId" IS NULL+ AND COALESCE(s."metadata"::jsonb->>'origin', 'interactive') <> 'automation')+ AS autopilot_turns,+ COUNT(*) FILTER (WHERE s."expertId" IS NOT NULL+ AND COALESCE(s."metadata"::jsonb->>'origin', 'interactive') <> 'automation')+ AS expert_turns,+ COUNT(*) FILTER (WHERE COALESCE(s."metadata"::jsonb->>'origin', 'interactive') = 'automation')+ AS scheduled_turns+ FROM platform."ChatMessage" m+ JOIN platform."ChatSession" s ON s."id" = m."sessionId"+ WHERE m."role" = 'user'+ AND m."createdAt" > DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '11 months'+ AND COALESCE(s."metadata"::jsonb->>'kind', 'normal') <> 'dream'+ GROUP BY 1, 2+),+-- Distinct calendar days on which a person did something (a human-started+-- run or a human turn; same predicate as tasks_human), across both surfaces,+-- so a user who runs agents on Monday and chats on Tuesday has two active+-- days, while a schedule firing every night adds none.+activity_days AS (+ SELECT user_id, DATE_TRUNC('month', day)::date AS month, COUNT(DISTINCT day) AS active_days+ FROM (+ SELECT ge."userId" AS user_id, ge."createdAt"::date AS day+ FROM platform."AgentGraphExecution" ge+ WHERE ge."createdAt" > DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '11 months'+ AND ge."isDeleted" = FALSE+ AND ge."parentGraphExecutionId" IS NULL+ AND COALESCE(ge."stats"::jsonb->>'is_dry_run', 'false') <> 'true'+ AND (ge."triggerSource" IS NULL OR ge."triggerSource" IN ('manual', 'api'))+ UNION+ SELECT s."userId", m."createdAt"::date+ FROM platform."ChatMessage" m+ JOIN platform."ChatSession" s ON s."id" = m."sessionId"+ WHERE m."role" = 'user'+ AND m."createdAt" > DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '11 months'+ AND COALESCE(s."metadata"::jsonb->>'kind', 'normal') <> 'dream'+ AND COALESCE(s."metadata"::jsonb->>'origin', 'interactive') <> 'automation'+ ) d+ GROUP BY 1, 2+),+costs AS (+ SELECT+ "userId" AS user_id,+ DATE_TRUNC('month', "createdAt")::date AS month,+ SUM(COALESCE("costMicrodollars", 0)) / 1000000.0 AS platform_cost_usd,+ SUM(COALESCE("costMicrodollars", 0)) FILTER (+ WHERE COALESCE("blockName", '') NOT ILIKE 'copilot:%') / 1000000.0 AS agent_cost_usd,+ SUM(COALESCE("costMicrodollars", 0)) FILTER (+ WHERE "blockName" ILIKE 'copilot:%'+ AND COALESCE("metadata"::jsonb->>'source', 'copilot') <> 'dream_pass'+ AND "blockName" NOT ILIKE 'copilot:dream%') / 1000000.0 AS copilot_cost_usd,+ SUM(COALESCE("costMicrodollars", 0)) FILTER (+ WHERE "metadata"::jsonb->>'source' = 'dream_pass'+ OR "blockName" ILIKE 'copilot:dream%') / 1000000.0 AS background_cost_usd+ FROM platform."PlatformCostLog"+ WHERE "userId" IS NOT NULL+ AND "createdAt" > DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '11 months'+ GROUP BY 1, 2+),+credits AS (+ -- Personal ledger plus org-billed rows attributed to the user who+ -- initiated them, so an org member's spend sits next to their cost.+ SELECT+ user_id,+ DATE_TRUNC('month', created_at)::date AS month,+ -COALESCE(SUM(amount) FILTER (WHERE type = 'USAGE'), 0) / 100.0 AS credits_spent_usd,+ COALESCE(SUM(amount) FILTER (WHERE type IN ('TOP_UP', 'SUBSCRIPTION')), 0) / 100.0+ AS credits_purchased_usd+ FROM (+ SELECT "userId" AS user_id, "amount" AS amount, "type"::text AS type, "createdAt" AS created_at+ FROM platform."CreditTransaction"+ WHERE "isActive" = TRUE+ AND "createdAt" > DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '11 months'+ UNION ALL+ SELECT "initiatedByUserId", "amount", "type"::text, "createdAt"+ FROM platform."OrgCreditTransaction"+ WHERE "isActive" = TRUE AND "initiatedByUserId" IS NOT NULL+ AND "createdAt" > DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '11 months'+ ) c+ GROUP BY 1, 2+),+keys AS (+ SELECT user_id, month FROM runs+ UNION SELECT user_id, month FROM turns+ UNION SELECT user_id, month FROM costs+ UNION SELECT user_id, month FROM credits+),+assembled AS (+ SELECT+ k.user_id,+ u."email" AS email,+ u."subscriptionTier"::text AS subscription_tier,+ k.month,+ COALESCE(r.agent_runs_human, 0) - COALESCE(r.agent_runs_copilot, 0)+ + COALESCE(t.autopilot_turns, 0) + COALESCE(t.expert_turns, 0) AS tasks_human,+ COALESCE(r.agent_runs_automated, 0) + COALESCE(t.scheduled_turns, 0) AS tasks_automated,+ COALESCE(r.agent_runs, 0) AS agent_runs,+ COALESCE(t.autopilot_turns, 0) AS autopilot_turns,+ COALESCE(t.expert_turns, 0) AS expert_turns,+ COALESCE(ad.active_days, 0) AS active_days,+ COALESCE(c.platform_cost_usd, 0) AS platform_cost_usd,+ COALESCE(c.agent_cost_usd, 0) AS agent_cost_usd,+ COALESCE(c.copilot_cost_usd, 0) AS copilot_cost_usd,+ COALESCE(c.background_cost_usd, 0) AS background_cost_usd,+ COALESCE(cr.credits_spent_usd, 0) AS credits_spent_usd,+ COALESCE(cr.credits_purchased_usd, 0) AS credits_purchased_usd+ FROM keys k+ LEFT JOIN platform."User" u ON u."id" = k.user_id+ LEFT JOIN runs r ON r.user_id = k.user_id AND r.month = k.month+ LEFT JOIN activity_days ad ON ad.user_id = k.user_id AND ad.month = k.month+ LEFT JOIN turns t ON t.user_id = k.user_id AND t.month = k.month+ LEFT JOIN costs c ON c.user_id = k.user_id AND c.month = k.month+ LEFT JOIN credits cr ON cr.user_id = k.user_id AND cr.month = k.month+)+SELECT+ a.*,+ a.credits_spent_usd - a.platform_cost_usd AS gross_margin_usd,+ a.platform_cost_usd / NULLIF(a.tasks_human + a.tasks_automated, 0) AS cost_per_task_usd,+ a.platform_cost_usd / NULLIF(a.active_days, 0) AS cost_per_active_day_usd+FROM assembled adiff --git a/autogpt_platform/analytics/queries/user_attribution.sql b/autogpt_platform/analytics/queries/user_attribution.sqlnew file mode 100644index 0000000000..3a5b44dc4a--- /dev/null+++ b/autogpt_platform/analytics/queries/user_attribution.sql@@ -0,0 +1,56 @@+-- =============================================================+-- View: analytics.user_attribution+-- Looker source alias: (new) | Charts: 0+-- =============================================================+-- DESCRIPTION+-- One row per user: where they came from, captured once around+-- signup by the browser that created the account. Carries the ids+-- the analytics tools knew the user by before they had a user id,+-- so channel (DataFast), product analytics (PostHog) and flag+-- bucketing (LaunchDarkly, via the shared anonymous id) can all be+-- joined to activation, retention and revenue on user_id.+--+-- Join to analytics.user_lifecycle for "which channel brings users+-- who activate". Rows exist only for users who signed up (or first+-- logged in) after this table shipped.+--+-- SOURCE TABLES+-- platform.UserAttribution+--+-- OUTPUT COLUMNS+-- user_id TEXT User UUID+-- captured_at TIMESTAMPTZ When the row was first written+-- anonymous_id TEXT First-party anonymous id shared with PostHog + LaunchDarkly+-- posthog_distinct_id TEXT PostHog device id at signup, when different+-- datafast_visitor_id TEXT DataFast visitor (join key into DataFast exports)+-- datafast_session_id TEXT DataFast session that contained the signup+-- landing_path TEXT First page seen in that browser+-- referrer TEXT document.referrer on that first page+-- utm_source / utm_medium / utm_campaign TEXT From the first landing URL+-- signup_method TEXT 'email' | 'google' | NULL (not a fresh signup in that browser)+--+-- EXAMPLE QUERIES+-- -- Activation rate by UTM source+-- SELECT COALESCE(a.utm_source, '(none)') AS source,+-- COUNT(*) AS signups,+-- AVG(CASE WHEN l.activated THEN 1 ELSE 0 END) AS activation_rate+-- FROM analytics.user_attribution a+-- JOIN analytics.user_lifecycle l ON l.user_id = a.user_id+-- WHERE l.signup_at < NOW() - INTERVAL '14 days'+-- GROUP BY 1 ORDER BY signups DESC;+-- =============================================================++SELECT+ "userId" AS user_id,+ "createdAt" AS captured_at,+ "anonymousId" AS anonymous_id,+ "posthogDistinctId" AS posthog_distinct_id,+ "datafastVisitorId" AS datafast_visitor_id,+ "datafastSessionId" AS datafast_session_id,+ "landingPath" AS landing_path,+ "referrer" AS referrer,+ "utmSource" AS utm_source,+ "utmMedium" AS utm_medium,+ "utmCampaign" AS utm_campaign,+ "signupMethod" AS signup_method+FROM platform."UserAttribution"diff --git a/autogpt_platform/analytics/queries/user_lifecycle.sql b/autogpt_platform/analytics/queries/user_lifecycle.sqlnew file mode 100644index 0000000000..755547b83f--- /dev/null+++ b/autogpt_platform/analytics/queries/user_lifecycle.sql@@ -0,0 +1,343 @@+-- =============================================================+-- View: analytics.user_lifecycle+-- Looker source alias: (new) | Charts: 0+-- =============================================================+-- DESCRIPTION+-- One row per user with the facts that describe where they are in+-- their lifecycle: when they signed up, when they first and last+-- did anything, how much they did in their first two weeks, what+-- they cost us, what they paid, and the derived labels GTM asked+-- for (activated, stale, churned). It is deliberately wide so it+-- can be used as the feature table for a churn-signal analysis:+-- pick a label column, regress on the rest.+--+-- Definitions (the task predicate is repeated in user_task_daily,+-- retention_task_weekly and unit_economics_monthly; keep them in sync):+-- - task human-started agent run (manual / API; untagged+-- legacy rows count) or a human chat turn. A run the+-- copilot started is represented by the chat turn that+-- asked for it, so it is not counted twice.+-- - activated >= 3 tasks on >= 2 distinct days within 14 days of signup+-- - last_active_at latest of: last task, last visit, last scheduled run,+-- falling back to signup+-- - stale_14d/30d no activity in the last 14 / 30 days+-- - churned_30d had at least one task ever, signed up > 30 days ago,+-- and no activity in the last 30 days+-- - never_activated_30d signed up > 30 days ago and never did a task+--+-- SOURCE TABLES+-- platform.User, platform.UserOnboarding,+-- auth.sessions (Supabase, history to 2026-07-30) + platform.UserAuthSession (Better Auth, since),+-- platform.AgentGraphExecution, platform.ChatMessage/ChatSession,+-- platform.ActivityEvent (schedules), platform.Expert,+-- platform.PlatformCostLog, platform.CreditTransaction + platform.OrgCreditTransaction+-- (org-billed runs, attributed to the user who initiated them)+--+-- OUTPUT COLUMNS+-- Identity: user_id, email, signup_at, subscription_tier, timezone,+-- usage_reason, onboarding_completed, onboarding_integrations_selected,+-- integrations_connected_total, integration_providers_connected,+-- first_integration_connected_at+-- Timestamps: first_login_at, last_login_at, last_visit_at, first_agent_run_at,+-- last_agent_run_at, first_chat_turn_at, last_chat_turn_at,+-- first_task_at, last_task_at, last_scheduled_run_at, last_active_at,+-- first_schedule_created_at, first_expert_hired_at, first_purchase_at+-- Counts: login_count, agent_runs_total, agent_runs_human_total,+-- agent_runs_scheduled_total, agent_runs_failed_total,+-- agent_runs_no_credits_total, expert_workflow_runs_total,+-- distinct_agents_run, scheduled_runs_30d, autopilot_turns_total,+-- expert_turns_total, chat_sessions_total, active_days_total,+-- tasks_first_7d, tasks_first_14d, active_days_first_14d,+-- tasks_7d, tasks_28d, active_days_28d, tasks_week_4,+-- schedules_created_total,+-- experts_hired_total, experts_active, purchases_total+-- Money: platform_cost_usd_total, platform_cost_usd_30d,+-- credits_spent_usd_total, credits_purchased_usd_total+-- Derived: hours_to_first_task, days_since_last_active,+-- first_task_within_7d, activated, stale_14d, stale_30d,+-- churned_30d, never_activated_30d+--+-- WINDOW+-- All users, full history (per-user aggregates)+--+-- EXAMPLE QUERIES+-- -- Activation rate by signup week+-- SELECT DATE_TRUNC('week', signup_at)::date AS week,+-- AVG(CASE WHEN activated THEN 1 ELSE 0 END) AS activation_rate, COUNT(*) AS signups+-- FROM analytics.user_lifecycle+-- WHERE signup_at < NOW() - INTERVAL '14 days'+-- GROUP BY 1 ORDER BY 1;+--+-- -- Which early behaviours separate churned from retained users+-- SELECT churned_30d,+-- AVG(tasks_first_7d) AS avg_tasks_first_7d,+-- AVG(CASE WHEN schedules_created_total > 0 THEN 1 ELSE 0 END) AS pct_with_schedule,+-- AVG(CASE WHEN expert_turns_total > 0 THEN 1 ELSE 0 END) AS pct_used_expert,+-- AVG(agent_runs_failed_total::float / NULLIF(agent_runs_total, 0)) AS avg_fail_ratio+-- FROM analytics.user_lifecycle+-- WHERE first_task_at IS NOT NULL AND signup_at < NOW() - INTERVAL '60 days'+-- GROUP BY 1;+--+-- -- Cost to us per user per month of life (users active this month)+-- SELECT AVG(platform_cost_usd_30d) FROM analytics.user_lifecycle WHERE NOT stale_30d;+-- =============================================================++WITH users AS (+ SELECT+ u."id" AS user_id,+ u."email" AS email,+ u."createdAt" AS signup_at,+ u."subscriptionTier"::text AS subscription_tier,+ u."timezone" AS timezone+ FROM platform."User" u+),+logins AS (+ SELECT+ user_id,+ MIN(created_at) AS first_login_at,+ MAX(created_at) AS last_login_at,+ MAX(seen_at) AS last_visit_at,+ COUNT(*) AS login_count+ FROM (+ -- Supabase sessions: history up to the Better Auth cutover (2026-07-30)+ SELECT user_id::text AS user_id, created_at::timestamptz AS created_at,+ GREATEST(refreshed_at::timestamptz, created_at::timestamptz) AS seen_at+ FROM auth.sessions WHERE user_id IS NOT NULL+ UNION ALL+ -- Better Auth sessions: everything since; updatedAt moves on refresh+ SELECT "userId", "createdAt"::timestamptz,+ GREATEST("updatedAt"::timestamptz, "createdAt"::timestamptz)+ FROM platform."UserAuthSession"+ ) s+ GROUP BY 1+),+runs AS (+ SELECT+ ge."userId" AS user_id,+ MIN(ge."createdAt") AS first_agent_run_at,+ MAX(ge."createdAt") AS last_agent_run_at,+ COUNT(*) AS agent_runs_total,+ COUNT(*) FILTER (WHERE ge."triggerSource" IS NULL+ OR ge."triggerSource" IN ('manual', 'api', 'copilot'))+ AS agent_runs_human_total,+ COUNT(*) FILTER (WHERE ge."triggerSource" = 'schedule') AS agent_runs_scheduled_total,+ COUNT(*) FILTER (WHERE ge."executionStatus" = 'FAILED') AS agent_runs_failed_total,+ -- Keep the legacy message predicates synchronized with graph_execution.sql+ -- (and backend/util/exceptions.py): rows before failure_reason existed+ -- (2026-08-18) only carry the message.+ COUNT(*) FILTER (WHERE ge."executionStatus" = 'FAILED'+ AND (ge."stats"::jsonb->>'failure_reason' = 'insufficient_balance'+ OR (ge."stats"::jsonb->>'failure_reason' IS NULL+ AND (ge."stats"::jsonb->>'error' = 'You have no credits left to run an agent.'+ OR ge."stats"::jsonb->>'error' ~ '^Insufficient balance of \$-?[0-9]+(\.[0-9]+)?, where this will cost \$-?[0-9]+(\.[0-9]+)?$'+ OR ge."stats"::jsonb->>'error' ~ '^Insufficient balance to run [A-Za-z_][A-Za-z0-9_]*: dynamic-cost blocks require a positive balance\.$'+ OR ge."stats"::jsonb->>'error' ~ '^Organization has -?[0-9]+ credits but needs [0-9]+$'))))+ AS agent_runs_no_credits_total,+ COUNT(*) FILTER (WHERE ge."expertId" IS NOT NULL) AS expert_workflow_runs_total,+ COUNT(DISTINCT ge."agentGraphId") AS distinct_agents_run,+ MAX(ge."createdAt") FILTER (WHERE ge."triggerSource" = 'schedule') AS last_scheduled_run_at,+ COUNT(*) FILTER (WHERE ge."triggerSource" = 'schedule'+ AND ge."createdAt" > NOW() - INTERVAL '30 days') AS scheduled_runs_30d+ FROM platform."AgentGraphExecution" ge+ WHERE ge."isDeleted" = FALSE+ AND ge."parentGraphExecutionId" IS NULL+ AND COALESCE(ge."stats"::jsonb->>'is_dry_run', 'false') <> 'true'+ GROUP BY 1+),+turns AS (+ SELECT+ s."userId" AS user_id,+ MIN(m."createdAt") AS first_chat_turn_at,+ MAX(m."createdAt") AS last_chat_turn_at,+ COUNT(*) FILTER (WHERE s."expertId" IS NULL) AS autopilot_turns_total,+ COUNT(*) FILTER (WHERE s."expertId" IS NOT NULL) AS expert_turns_total,+ COUNT(DISTINCT m."sessionId") AS chat_sessions_total+ FROM platform."ChatMessage" m+ JOIN platform."ChatSession" s ON s."id" = m."sessionId"+ WHERE m."role" = 'user'+ AND COALESCE(s."metadata"::jsonb->>'kind', 'normal') <> 'dream'+ AND COALESCE(s."metadata"::jsonb->>'origin', 'interactive') <> 'automation'+ GROUP BY 1+),+task_events AS (+ -- Copilot-started runs are left out on purpose: the user turn that asked+ -- for them is the task, and it is already in the second half of the union.+ SELECT ge."userId" AS user_id, ge."createdAt" AS at+ FROM platform."AgentGraphExecution" ge+ WHERE ge."isDeleted" = FALSE+ AND ge."parentGraphExecutionId" IS NULL+ AND COALESCE(ge."stats"::jsonb->>'is_dry_run', 'false') <> 'true'+ AND (ge."triggerSource" IS NULL OR ge."triggerSource" IN ('manual', 'api'))+ UNION ALL+ SELECT s."userId", m."createdAt"+ FROM platform."ChatMessage" m+ JOIN platform."ChatSession" s ON s."id" = m."sessionId"+ WHERE m."role" = 'user'+ AND COALESCE(s."metadata"::jsonb->>'kind', 'normal') <> 'dream'+ AND COALESCE(s."metadata"::jsonb->>'origin', 'interactive') <> 'automation'+),+task_days AS (+ SELECT+ t.user_id,+ MIN(t.at) AS first_task_at,+ MAX(t.at) AS last_task_at,+ COUNT(DISTINCT t.at::date) AS active_days_total,+ COUNT(*) FILTER (WHERE t.at < u.signup_at + INTERVAL '7 days') AS tasks_first_7d,+ COUNT(*) FILTER (WHERE t.at < u.signup_at + INTERVAL '14 days') AS tasks_first_14d,+ COUNT(DISTINCT t.at::date) FILTER (WHERE t.at < u.signup_at + INTERVAL '14 days')+ AS active_days_first_14d,+ COUNT(*) FILTER (WHERE t.at > NOW() - INTERVAL '7 days') AS tasks_7d,+ COUNT(*) FILTER (WHERE t.at > NOW() - INTERVAL '28 days') AS tasks_28d,+ COUNT(DISTINCT t.at::date) FILTER (WHERE t.at > NOW() - INTERVAL '28 days')+ AS active_days_28d,+ COUNT(*) FILTER (WHERE t.at >= u.signup_at + INTERVAL '21 days'+ AND t.at < u.signup_at + INTERVAL '28 days') AS tasks_week_4+ FROM task_events t+ JOIN users u ON u.user_id = t.user_id+ GROUP BY 1+),+schedules AS (+ SELECT+ "userId" AS user_id,+ MIN("createdAt") FILTER (WHERE "eventType" = 'schedule.created') AS first_schedule_created_at,+ COUNT(*) FILTER (WHERE "eventType" = 'schedule.created') AS schedules_created_total+ FROM platform."ActivityEvent"+ WHERE "category" = 'SCHEDULE'+ GROUP BY 1+),+experts AS (+ SELECT+ "ownerUserId" AS user_id,+ COUNT(*) AS experts_hired_total,+ COUNT(*) FILTER (WHERE "isArchived" = FALSE) AS experts_active,+ MIN("createdAt") AS first_expert_hired_at+ FROM platform."Expert"+ WHERE "ownerUserId" IS NOT NULL AND "isTemplate" = FALSE+ GROUP BY 1+),+costs AS (+ SELECT+ "userId" AS user_id,+ SUM(COALESCE("costMicrodollars", 0)) / 1000000.0 AS platform_cost_usd_total,+ SUM(COALESCE("costMicrodollars", 0)) FILTER (WHERE "createdAt" > NOW() - INTERVAL '30 days') / 1000000.0+ AS platform_cost_usd_30d+ FROM platform."PlatformCostLog"+ WHERE "userId" IS NOT NULL+ GROUP BY 1+),+credits AS (+ -- Personal ledger plus org-billed rows attributed to the user who+ -- initiated them, so an org member's spend sits next to their cost.+ SELECT+ user_id,+ -COALESCE(SUM(amount) FILTER (WHERE type = 'USAGE'), 0) / 100.0 AS credits_spent_usd_total,+ COALESCE(SUM(amount) FILTER (WHERE type IN ('TOP_UP', 'SUBSCRIPTION')), 0) / 100.0+ AS credits_purchased_usd_total,+ COUNT(*) FILTER (WHERE type IN ('TOP_UP', 'SUBSCRIPTION')) AS purchases_total,+ MIN(created_at) FILTER (WHERE type IN ('TOP_UP', 'SUBSCRIPTION')) AS first_purchase_at+ FROM (+ SELECT "userId" AS user_id, "amount" AS amount, "type"::text AS type, "createdAt" AS created_at+ FROM platform."CreditTransaction"+ WHERE "isActive" = TRUE+ UNION ALL+ SELECT "initiatedByUserId", "amount", "type"::text, "createdAt"+ FROM platform."OrgCreditTransaction"+ WHERE "isActive" = TRUE AND "initiatedByUserId" IS NOT NULL+ ) c+ GROUP BY 1+),+onboarding AS (+ SELECT+ "userId" AS user_id,+ "usageReason" AS usage_reason,+ 'ONBOARDING_COMPLETE' = ANY("completedSteps") AS onboarding_completed,+ COALESCE(cardinality("integrations"), 0) AS onboarding_integrations_selected+ FROM platform."UserOnboarding"+),+integrations AS (+ -- Credentials the user owns personally; team- and org-owned ones are+ -- attributed to their owner, not to whoever created them.+ SELECT+ "ownerId" AS user_id,+ COUNT(*) AS integrations_connected_total,+ COUNT(DISTINCT "provider") AS integration_providers_connected,+ MIN("createdAt") AS first_integration_connected_at+ FROM platform."IntegrationCredential"+ WHERE "ownerType" = 'USER'+ GROUP BY 1+),+assembled AS (+ SELECT+ u.user_id, u.email, u.signup_at, u.subscription_tier, u.timezone,+ o.usage_reason,+ COALESCE(o.onboarding_completed, FALSE) AS onboarding_completed,+ COALESCE(o.onboarding_integrations_selected, 0) AS onboarding_integrations_selected,+ COALESCE(i.integrations_connected_total, 0) AS integrations_connected_total,+ COALESCE(i.integration_providers_connected, 0) AS integration_providers_connected,+ i.first_integration_connected_at,+ l.first_login_at, l.last_login_at, l.last_visit_at,+ COALESCE(l.login_count, 0) AS login_count,+ r.first_agent_run_at, r.last_agent_run_at,+ t.first_chat_turn_at, t.last_chat_turn_at,+ td.first_task_at, td.last_task_at,+ r.last_scheduled_run_at,+ COALESCE(+ GREATEST(td.last_task_at, t.last_chat_turn_at, l.last_visit_at, r.last_scheduled_run_at),+ u.signup_at+ ) AS last_active_at,+ s.first_schedule_created_at,+ e.first_expert_hired_at,+ cr.first_purchase_at,+ COALESCE(r.agent_runs_total, 0) AS agent_runs_total,+ COALESCE(r.agent_runs_human_total, 0) AS agent_runs_human_total,+ COALESCE(r.agent_runs_scheduled_total, 0) AS agent_runs_scheduled_total,+ COALESCE(r.agent_runs_failed_total, 0) AS agent_runs_failed_total,+ COALESCE(r.agent_runs_no_credits_total, 0) AS agent_runs_no_credits_total,+ COALESCE(r.expert_workflow_runs_total, 0) AS expert_workflow_runs_total,+ COALESCE(r.distinct_agents_run, 0) AS distinct_agents_run,+ COALESCE(r.scheduled_runs_30d, 0) AS scheduled_runs_30d,+ COALESCE(t.autopilot_turns_total, 0) AS autopilot_turns_total,+ COALESCE(t.expert_turns_total, 0) AS expert_turns_total,+ COALESCE(t.chat_sessions_total, 0) AS chat_sessions_total,+ COALESCE(td.active_days_total, 0) AS active_days_total,+ COALESCE(td.tasks_first_7d, 0) AS tasks_first_7d,+ COALESCE(td.tasks_first_14d, 0) AS tasks_first_14d,+ COALESCE(td.active_days_first_14d, 0) AS active_days_first_14d,+ COALESCE(td.tasks_7d, 0) AS tasks_7d,+ COALESCE(td.tasks_28d, 0) AS tasks_28d,+ COALESCE(td.active_days_28d, 0) AS active_days_28d,+ COALESCE(td.tasks_week_4, 0) AS tasks_week_4,+ COALESCE(s.schedules_created_total, 0) AS schedules_created_total,+ COALESCE(e.experts_hired_total, 0) AS experts_hired_total,+ COALESCE(e.experts_active, 0) AS experts_active,+ COALESCE(cr.purchases_total, 0) AS purchases_total,+ COALESCE(c.platform_cost_usd_total, 0) AS platform_cost_usd_total,+ COALESCE(c.platform_cost_usd_30d, 0) AS platform_cost_usd_30d,+ COALESCE(cr.credits_spent_usd_total, 0) AS credits_spent_usd_total,+ COALESCE(cr.credits_purchased_usd_total, 0) AS credits_purchased_usd_total+ FROM users u+ LEFT JOIN onboarding o ON o.user_id = u.user_id+ LEFT JOIN integrations i ON i.user_id = u.user_id+ LEFT JOIN logins l ON l.user_id = u.user_id+ LEFT JOIN runs r ON r.user_id = u.user_id+ LEFT JOIN turns t ON t.user_id = u.user_id+ LEFT JOIN task_days td ON td.user_id = u.user_id+ LEFT JOIN schedules s ON s.user_id = u.user_id+ LEFT JOIN experts e ON e.user_id = u.user_id+ LEFT JOIN costs c ON c.user_id = u.user_id+ LEFT JOIN credits cr ON cr.user_id = u.user_id+)+SELECT+ a.*,+ EXTRACT(EPOCH FROM (a.first_task_at - a.signup_at)) / 3600.0 AS hours_to_first_task,+ (CURRENT_DATE - a.last_active_at::date) AS days_since_last_active,+ a.tasks_first_7d >= 1 AS first_task_within_7d,+ a.tasks_first_14d >= 3 AND a.active_days_first_14d >= 2 AS activated,+ a.last_active_at < NOW() - INTERVAL '14 days' AS stale_14d,+ a.last_active_at < NOW() - INTERVAL '30 days' AS stale_30d,+ a.first_task_at IS NOT NULL+ AND a.signup_at < NOW() - INTERVAL '30 days'+ AND a.last_active_at < NOW() - INTERVAL '30 days' AS churned_30d,+ a.first_task_at IS NULL+ AND a.signup_at < NOW() - INTERVAL '30 days' AS never_activated_30d+FROM assembled adiff --git a/autogpt_platform/analytics/queries/user_lifecycle_funnel_weekly.sql b/autogpt_platform/analytics/queries/user_lifecycle_funnel_weekly.sqlnew file mode 100644index 0000000000..7aeb708429--- /dev/null+++ b/autogpt_platform/analytics/queries/user_lifecycle_funnel_weekly.sql@@ -0,0 +1,84 @@+-- =============================================================+-- View: analytics.user_lifecycle_funnel_weekly+-- Looker source alias: (new) | Charts: 0+-- =============================================================+-- DESCRIPTION+-- Activation funnel per signup-week cohort, built on+-- analytics.user_lifecycle (so the definitions live in one place).+-- Each stage is a count of users in the cohort who reached it, plus+-- the share of the cohort. Stages that need time to mature (e.g.+-- activated within 14 days) are NULL until the whole cohort week has+-- aged that long, counted from the week's last day (so week start plus+-- 7 days plus the window), so a half-baked recent week never reads as+-- a drop.+--+-- SOURCE VIEWS+-- analytics.user_lifecycle+--+-- OUTPUT COLUMNS+-- cohort_week_start DATE Monday of the signup week+-- cohort_label TEXT ISO week label+-- signups BIGINT Users who signed up that week+-- onboarded BIGINT Completed onboarding+-- first_task_7d BIGINT Did a task within 7 days of signup+-- activated_14d BIGINT Met the activation definition (see user_lifecycle)+-- connected_integration BIGINT Connected at least one credential (any time)+-- created_schedule BIGINT Created at least one schedule (any time)+-- used_expert BIGINT Talked to or ran an expert (any time)+-- purchased BIGINT Bought credits or a subscription (any time)+-- retained_w4 BIGINT Did a task in their fourth week after signup+-- (days 21-27; cohort >= 4 weeks old)+-- pct_onboarded, pct_first_task_7d, pct_activated_14d, pct_connected_integration,+-- pct_created_schedule, pct_used_expert, pct_purchased, pct_retained_w4+-- FLOAT share of signups+--+-- WINDOW+-- Signup cohorts from the last 180 days+--+-- EXAMPLE QUERIES+-- SELECT cohort_label, signups, pct_first_task_7d, pct_activated_14d, pct_retained_w4+-- FROM analytics.user_lifecycle_funnel_weekly ORDER BY cohort_week_start;+-- =============================================================++WITH cohorts AS (+ SELECT+ DATE_TRUNC('week', signup_at)::date AS cohort_week_start,+ COUNT(*) AS signups,+ COUNT(*) FILTER (WHERE onboarding_completed) AS onboarded,+ COUNT(*) FILTER (WHERE first_task_within_7d) AS first_task_7d,+ COUNT(*) FILTER (WHERE activated) AS activated_14d,+ COUNT(*) FILTER (WHERE integrations_connected_total > 0) AS connected_integration,+ COUNT(*) FILTER (WHERE schedules_created_total > 0) AS created_schedule,+ COUNT(*) FILTER (WHERE expert_turns_total > 0 OR expert_workflow_runs_total > 0)+ AS used_expert,+ COUNT(*) FILTER (WHERE purchases_total > 0) AS purchased,+ COUNT(*) FILTER (WHERE tasks_week_4 > 0) AS retained_w4+ FROM analytics.user_lifecycle+ WHERE signup_at >= CURRENT_DATE - INTERVAL '180 days'+ GROUP BY 1+)+SELECT+ cohort_week_start,+ TO_CHAR(cohort_week_start, 'IYYY-"W"IW') AS cohort_label,+ signups,+ onboarded,+ CASE WHEN cohort_week_start + 14 <= CURRENT_DATE THEN first_task_7d END AS first_task_7d,+ CASE WHEN cohort_week_start + 21 <= CURRENT_DATE THEN activated_14d END AS activated_14d,+ connected_integration,+ created_schedule,+ used_expert,+ purchased,+ CASE WHEN cohort_week_start + 35 <= CURRENT_DATE THEN retained_w4 END AS retained_w4,+ onboarded::float / NULLIF(signups, 0) AS pct_onboarded,+ CASE WHEN cohort_week_start + 14 <= CURRENT_DATE+ THEN first_task_7d::float / NULLIF(signups, 0) END AS pct_first_task_7d,+ CASE WHEN cohort_week_start + 21 <= CURRENT_DATE+ THEN activated_14d::float / NULLIF(signups, 0) END AS pct_activated_14d,+ connected_integration::float / NULLIF(signups, 0) AS pct_connected_integration,+ created_schedule::float / NULLIF(signups, 0) AS pct_created_schedule,+ used_expert::float / NULLIF(signups, 0) AS pct_used_expert,+ purchased::float / NULLIF(signups, 0) AS pct_purchased,+ CASE WHEN cohort_week_start + 35 <= CURRENT_DATE+ THEN retained_w4::float / NULLIF(signups, 0) END AS pct_retained_w4+FROM cohorts+ORDER BY cohort_week_startdiff --git a/autogpt_platform/analytics/queries/user_task_daily.sql b/autogpt_platform/analytics/queries/user_task_daily.sqlnew file mode 100644index 0000000000..4f976ac22b--- /dev/null+++ b/autogpt_platform/analytics/queries/user_task_daily.sql@@ -0,0 +1,270 @@+-- =============================================================+-- View: analytics.user_task_daily+-- Looker source alias: (new) | Charts: 0+-- =============================================================+-- DESCRIPTION+-- One row per (user, day) with everything the user did and cost+-- that day, across every surface: classic agent runs (split by how+-- they were started), Autopilot turns, expert turns and expert+-- workflow runs, schedules created, our real provider cost, and the+-- credits we charged. This is the workhorse table for "how many+-- tasks are people running" and for per-day cost per user.+--+-- A "task" is a unit of work a person asked for now: a human-started+-- agent run (manual UI or API key) or a human chat turn. A run the+-- copilot tool started is counted once, through the chat turn that+-- asked for it. Automated work (schedule fires, webhook triggers,+-- scheduled follow-ups) is counted separately so the two can be compared.+--+-- Sub-graph runs (parentGraphExecutionId set) and dry runs are+-- excluded from run counts: they are part of the parent task.+-- Runs created before triggerSource existed cannot be split by+-- start method; they are counted as human (same rule as+-- user_lifecycle and retention_task_weekly) and also reported+-- separately in agent_runs_untagged. Runs an admin started on the+-- user's behalf (triggerSource = admin) are in agent_runs and+-- agent_runs_admin but in neither task bucket, so tasks_human ++-- tasks_automated can be less than agent_runs.+--+-- SOURCE TABLES+-- platform.AgentGraphExecution — agent runs (triggerSource, expertId, stats)+-- platform.ChatMessage / ChatSession — copilot turns+-- platform.ActivityEvent — schedule.created / schedule.deleted+-- platform.PlatformCostLog — our provider cost (microdollars)+-- platform.CreditTransaction — credits charged to / bought by the user (cents)+-- platform.OrgCreditTransaction — org-billed rows, attributed to initiatedByUserId+-- auth.sessions — logins before the Better Auth cutover (2026-07-30)+-- platform.UserAuthSession — logins since (Better Auth)+--+-- OUTPUT COLUMNS+-- user_id TEXT User UUID+-- day DATE Calendar day (UTC)+-- tasks_human BIGINT agent_runs_human - agent_runs_copilot + autopilot_turns + expert_turns+-- tasks_automated BIGINT scheduled + webhook runs + scheduled follow-up turns+-- agent_runs BIGINT All root, non-dry agent runs created that day+-- agent_runs_human BIGINT triggerSource IN (manual, api, copilot) or NULL+-- agent_runs_copilot BIGINT triggerSource = copilot (started from a chat turn, which is the task)+-- agent_runs_scheduled BIGINT triggerSource = schedule+-- agent_runs_webhook BIGINT triggerSource = webhook+-- agent_runs_untagged BIGINT triggerSource IS NULL (pre-deploy rows; already inside agent_runs_human)+-- agent_runs_admin BIGINT triggerSource = admin (support/ops started it; in neither task bucket)+-- agent_runs_completed BIGINT Terminal status COMPLETED (as of query time)+-- agent_runs_failed BIGINT Terminal status FAILED+-- agent_runs_no_credits BIGINT FAILED with failure_reason insufficient_balance+-- expert_workflow_runs BIGINT Runs attributed to a hired expert+-- autopilot_turns BIGINT Human turns in Autopilot chats+-- expert_turns BIGINT Human turns in expert chats+-- scheduled_turns BIGINT Model-authored scheduled follow-up turns+-- chat_sessions_touched BIGINT Distinct chat sessions with a user turn+-- schedules_created BIGINT schedule.created activity events+-- schedules_deleted BIGINT schedule.deleted activity events+-- logins BIGINT Sessions created (Supabase history + Better Auth)+-- platform_cost_usd NUMERIC Our total provider cost for the user that day+-- agent_cost_usd NUMERIC ...of which block/agent runs+-- copilot_cost_usd NUMERIC ...of which copilot turns (excl. dream passes)+-- background_cost_usd NUMERIC ...of which dream/memory passes (no user action)+-- credits_spent_usd NUMERIC USAGE transactions (what we charged)+-- credits_purchased_usd NUMERIC TOP_UP + SUBSCRIPTION transactions+-- run_credits_usd NUMERIC Sum of stats.cost on runs created that day+--+-- WINDOW+-- Rolling 90 days+--+-- EXAMPLE QUERIES+-- -- Tasks per active user per day (platform-wide)+-- SELECT day, SUM(tasks_human)::float / NULLIF(COUNT(*) FILTER (WHERE tasks_human > 0), 0)+-- FROM analytics.user_task_daily GROUP BY 1 ORDER BY 1;+--+-- -- Cost to us per task (human tasks only), last 30 days+-- SELECT SUM(platform_cost_usd) / NULLIF(SUM(tasks_human), 0) AS cost_per_task_usd+-- FROM analytics.user_task_daily WHERE day >= CURRENT_DATE - 30;+--+-- -- Daily active users by surface+-- SELECT day,+-- COUNT(*) FILTER (WHERE agent_runs_human > 0) AS agent_dau,+-- COUNT(*) FILTER (WHERE autopilot_turns > 0) AS autopilot_dau,+-- COUNT(*) FILTER (WHERE expert_turns + expert_workflow_runs > 0) AS expert_dau+-- FROM analytics.user_task_daily GROUP BY 1 ORDER BY 1;+-- =============================================================++WITH runs AS (+ SELECT+ ge."userId" AS user_id,+ DATE_TRUNC('day', ge."createdAt")::date AS day,+ COUNT(*) AS agent_runs,+ COUNT(*) FILTER (WHERE ge."triggerSource" IS NULL+ OR ge."triggerSource" IN ('manual', 'api', 'copilot'))+ AS agent_runs_human,+ COUNT(*) FILTER (WHERE ge."triggerSource" = 'copilot')+ AS agent_runs_copilot,+ COUNT(*) FILTER (WHERE ge."triggerSource" = 'schedule')+ AS agent_runs_scheduled,+ COUNT(*) FILTER (WHERE ge."triggerSource" = 'webhook')+ AS agent_runs_webhook,+ COUNT(*) FILTER (WHERE ge."triggerSource" IS NULL) AS agent_runs_untagged,+ COUNT(*) FILTER (WHERE ge."triggerSource" = 'admin') AS agent_runs_admin,+ COUNT(*) FILTER (WHERE ge."executionStatus" = 'COMPLETED')+ AS agent_runs_completed,+ COUNT(*) FILTER (WHERE ge."executionStatus" = 'FAILED')+ AS agent_runs_failed,+ -- Keep the legacy message predicates synchronized with graph_execution.sql+ -- (and backend/util/exceptions.py): rows before failure_reason existed+ -- (2026-08-18) only carry the message.+ COUNT(*) FILTER (WHERE ge."executionStatus" = 'FAILED'+ AND (ge."stats"::jsonb->>'failure_reason' = 'insufficient_balance'+ OR (ge."stats"::jsonb->>'failure_reason' IS NULL+ AND (ge."stats"::jsonb->>'error' = 'You have no credits left to run an agent.'+ OR ge."stats"::jsonb->>'error' ~ '^Insufficient balance of \$-?[0-9]+(\.[0-9]+)?, where this will cost \$-?[0-9]+(\.[0-9]+)?$'+ OR ge."stats"::jsonb->>'error' ~ '^Insufficient balance to run [A-Za-z_][A-Za-z0-9_]*: dynamic-cost blocks require a positive balance\.$'+ OR ge."stats"::jsonb->>'error' ~ '^Organization has -?[0-9]+ credits but needs [0-9]+$'))))+ AS agent_runs_no_credits,+ COUNT(*) FILTER (WHERE ge."expertId" IS NOT NULL) AS expert_workflow_runs,+ COALESCE(SUM((ge."stats"::jsonb->>'cost')::numeric), 0) / 100.0+ AS run_credits_usd+ FROM platform."AgentGraphExecution" ge+ WHERE ge."createdAt" > CURRENT_DATE - INTERVAL '90 days'+ AND ge."isDeleted" = FALSE+ AND ge."parentGraphExecutionId" IS NULL+ AND COALESCE(ge."stats"::jsonb->>'is_dry_run', 'false') <> 'true'+ GROUP BY 1, 2+),+turns AS (+ SELECT+ s."userId" AS user_id,+ DATE_TRUNC('day', m."createdAt")::date AS day,+ COUNT(*) FILTER (WHERE s."expertId" IS NULL+ AND COALESCE(s."metadata"::jsonb->>'origin', 'interactive') <> 'automation')+ AS autopilot_turns,+ COUNT(*) FILTER (WHERE s."expertId" IS NOT NULL+ AND COALESCE(s."metadata"::jsonb->>'origin', 'interactive') <> 'automation')+ AS expert_turns,+ COUNT(*) FILTER (WHERE COALESCE(s."metadata"::jsonb->>'origin', 'interactive') = 'automation')+ AS scheduled_turns,+ COUNT(DISTINCT m."sessionId") AS chat_sessions_touched+ FROM platform."ChatMessage" m+ JOIN platform."ChatSession" s ON s."id" = m."sessionId"+ WHERE m."role" = 'user'+ AND m."createdAt" > CURRENT_DATE - INTERVAL '90 days'+ AND COALESCE(s."metadata"::jsonb->>'kind', 'normal') <> 'dream'+ GROUP BY 1, 2+),+schedules AS (+ SELECT+ "userId" AS user_id,+ DATE_TRUNC('day', "createdAt")::date AS day,+ COUNT(*) FILTER (WHERE "eventType" = 'schedule.created') AS schedules_created,+ COUNT(*) FILTER (WHERE "eventType" = 'schedule.deleted') AS schedules_deleted+ FROM platform."ActivityEvent"+ WHERE "category" = 'SCHEDULE'+ AND "createdAt" > CURRENT_DATE - INTERVAL '90 days'+ GROUP BY 1, 2+),+costs AS (+ SELECT+ "userId" AS user_id,+ DATE_TRUNC('day', "createdAt")::date AS day,+ SUM(COALESCE("costMicrodollars", 0)) AS platform_cost_md,+ SUM(COALESCE("costMicrodollars", 0)) FILTER (+ WHERE COALESCE("blockName", '') NOT ILIKE 'copilot:%')+ AS agent_cost_md,+ SUM(COALESCE("costMicrodollars", 0)) FILTER (+ WHERE "blockName" ILIKE 'copilot:%'+ AND COALESCE("metadata"::jsonb->>'source', 'copilot') <> 'dream_pass'+ AND "blockName" NOT ILIKE 'copilot:dream%')+ AS copilot_cost_md,+ SUM(COALESCE("costMicrodollars", 0)) FILTER (+ WHERE "metadata"::jsonb->>'source' = 'dream_pass'+ OR "blockName" ILIKE 'copilot:dream%')+ AS background_cost_md+ FROM platform."PlatformCostLog"+ WHERE "userId" IS NOT NULL+ AND "createdAt" > CURRENT_DATE - INTERVAL '90 days'+ GROUP BY 1, 2+),+credits AS (+ -- Personal ledger plus org-billed rows attributed to the user who+ -- initiated them, so an org member's spend sits next to their cost.+ SELECT+ user_id,+ DATE_TRUNC('day', created_at)::date AS day,+ -COALESCE(SUM(amount) FILTER (WHERE type = 'USAGE'), 0) / 100.0+ AS credits_spent_usd,+ COALESCE(SUM(amount) FILTER (WHERE type IN ('TOP_UP', 'SUBSCRIPTION')), 0) / 100.0+ AS credits_purchased_usd+ FROM (+ SELECT "userId" AS user_id, "amount" AS amount, "type"::text AS type, "createdAt" AS created_at+ FROM platform."CreditTransaction"+ WHERE "isActive" = TRUE AND "createdAt" > CURRENT_DATE - INTERVAL '90 days'+ UNION ALL+ SELECT "initiatedByUserId", "amount", "type"::text, "createdAt"+ FROM platform."OrgCreditTransaction"+ WHERE "isActive" = TRUE AND "initiatedByUserId" IS NOT NULL+ AND "createdAt" > CURRENT_DATE - INTERVAL '90 days'+ ) c+ GROUP BY 1, 2+),+logins AS (+ SELECT+ user_id,+ DATE_TRUNC('day', created_at)::date AS day,+ COUNT(*) AS logins+ FROM (+ -- Supabase sessions: history up to the Better Auth cutover (2026-07-30)+ SELECT user_id::text AS user_id, created_at::timestamptz AS created_at+ FROM auth.sessions WHERE user_id IS NOT NULL+ UNION ALL+ -- Better Auth sessions: everything since+ SELECT "userId", "createdAt"::timestamptz+ FROM platform."UserAuthSession"+ ) s+ WHERE created_at > CURRENT_DATE - INTERVAL '90 days'+ GROUP BY 1, 2+),+keys AS (+ SELECT user_id, day FROM runs+ UNION SELECT user_id, day FROM turns+ UNION SELECT user_id, day FROM schedules+ UNION SELECT user_id, day FROM costs+ UNION SELECT user_id, day FROM credits+ UNION SELECT user_id, day FROM logins+)+SELECT+ k.user_id,+ k.day,+ COALESCE(r.agent_runs_human, 0) - COALESCE(r.agent_runs_copilot, 0)+ + COALESCE(t.autopilot_turns, 0) + COALESCE(t.expert_turns, 0)+ AS tasks_human,+ COALESCE(r.agent_runs_scheduled, 0) + COALESCE(r.agent_runs_webhook, 0) + COALESCE(t.scheduled_turns, 0)+ AS tasks_automated,+ COALESCE(r.agent_runs, 0) AS agent_runs,+ COALESCE(r.agent_runs_human, 0) AS agent_runs_human,+ COALESCE(r.agent_runs_copilot, 0) AS agent_runs_copilot,+ COALESCE(r.agent_runs_scheduled, 0) AS agent_runs_scheduled,+ COALESCE(r.agent_runs_webhook, 0) AS agent_runs_webhook,+ COALESCE(r.agent_runs_untagged, 0) AS agent_runs_untagged,+ COALESCE(r.agent_runs_admin, 0) AS agent_runs_admin,+ COALESCE(r.agent_runs_completed, 0) AS agent_runs_completed,+ COALESCE(r.agent_runs_failed, 0) AS agent_runs_failed,+ COALESCE(r.agent_runs_no_credits, 0) AS agent_runs_no_credits,+ COALESCE(r.expert_workflow_runs, 0) AS expert_workflow_runs,+ COALESCE(t.autopilot_turns, 0) AS autopilot_turns,+ COALESCE(t.expert_turns, 0) AS expert_turns,+ COALESCE(t.scheduled_turns, 0) AS scheduled_turns,+ COALESCE(t.chat_sessions_touched, 0) AS chat_sessions_touched,+ COALESCE(s.schedules_created, 0) AS schedules_created,+ COALESCE(s.schedules_deleted, 0) AS schedules_deleted,+ COALESCE(l.logins, 0) AS logins,+ COALESCE(c.platform_cost_md, 0) / 1000000.0 AS platform_cost_usd,+ COALESCE(c.agent_cost_md, 0) / 1000000.0 AS agent_cost_usd,+ COALESCE(c.copilot_cost_md, 0) / 1000000.0 AS copilot_cost_usd,+ COALESCE(c.background_cost_md, 0) / 1000000.0 AS background_cost_usd,+ COALESCE(cr.credits_spent_usd, 0) AS credits_spent_usd,+ COALESCE(cr.credits_purchased_usd, 0) AS credits_purchased_usd,+ COALESCE(r.run_credits_usd, 0) AS run_credits_usd+FROM keys k+LEFT JOIN runs r ON r.user_id = k.user_id AND r.day = k.day+LEFT JOIN turns t ON t.user_id = k.user_id AND t.day = k.day+LEFT JOIN schedules s ON s.user_id = k.user_id AND s.day = k.day+LEFT JOIN costs c ON c.user_id = k.user_id AND c.day = k.day+LEFT JOIN credits cr ON cr.user_id = k.user_id AND cr.day = k.day+LEFT JOIN logins l ON l.user_id = k.user_id AND l.day = k.daydiff --git a/autogpt_platform/backend/scripts/generate_views.py b/autogpt_platform/backend/scripts/generate_views.pyindex 9439db26f4..5fa2b9aa8b 100755--- a/autogpt_platform/backend/scripts/generate_views.py+++ b/autogpt_platform/backend/scripts/generate_views.py@@ -11,7 +11,7 @@ Step 1 — one-time setup (creates schema, role, grants):poetry run analytics-setup-Step 2 — create / refresh all 14 analytics views:+Step 2 — create / refresh every analytics view (one per .sql file):poetry run analytics-viewsdiff --git a/docs/platform/SUMMARY.md b/docs/platform/SUMMARY.mdindex d6af31c552..e176f80903 100644--- a/docs/platform/SUMMARY.md+++ b/docs/platform/SUMMARY.md@@ -12,6 +12,7 @@* [Scheduling & Triggers](scheduling-and-triggers.md)* [Templates](templates.md)* [Credits & Billing](credits-and-billing.md)+* [Activation Metrics & Experiments](activation-metrics.md)* [Integrations & Credentials](integrations-and-credentials.md)* [Data Flow & Execution](data-flow-and-execution.md)* [Sharing & Exporting Agents](sharing-and-exporting.md)diff --git a/docs/platform/activation-metrics.md b/docs/platform/activation-metrics.mdnew file mode 100644index 0000000000..4053a5ac77--- /dev/null+++ b/docs/platform/activation-metrics.md@@ -0,0 +1,280 @@+# Activation Metrics & Experiments++How the platform measures activation, retention and unit economics across+every surface (classic agents, Autopilot, experts, schedules), and how to run+an A/B/C experiment whose results can be sliced in both PostHog and Looker.++This page is the contract between engineering and go-to-market: the event+names, the SQL views and the definitions below are the ones dashboards should+be built on. Change a definition here and in every view that repeats it: the+task predicate is spelled out in `user_task_daily`, `user_lifecycle`,+`retention_task_weekly` and `unit_economics_monthly` until a shared base view+exists, so a change to one must be made in all four.++## Where each tool fits++| Tool | Owns | Reads |+| --- | --- | --- |+| **Postgres `analytics.*` views** | The numbers. Every metric below is a view in `autogpt_platform/analytics/queries/`. | Primary tables (`platform.*`, `auth.*`). |+| **Looker Studio** | GTM dashboards. | Only `analytics.*` views, via the `analytics_readonly` role. |+| **PostHog** | Funnels, session replay, experiment bucketing and significance testing. | Frontend autocapture + the server-side activation events below. Can also read `analytics.*` as a data-warehouse source. |+| **LaunchDarkly** | Feature gating (on/off, rollouts, targeting). Not for A/B measurement. | — |+| **DataFast** | Marketing attribution: which channel brought a visitor who signed up, ran an agent, paid. | Browser goals only (`signup`, `paywall_view`, `run_agent`, `schedule_agent`, ...). |+| **Admin dashboard** | Ops views: platform costs, copilot usage export, execution accuracy. | Backend admin routes. |+| **Sentry** | Errors and stack traces behind `agent_run_failed`. | — |++## Definitions++- **Task**: a unit of work a person asked for *now*. Either a human-started+ agent run (`triggerSource` in `manual`, `api`) or a human chat turn (a+ `user` message in an Autopilot or expert session whose origin is not+ `automation`). A run the copilot tool started (`triggerSource` = `copilot`)+ is counted once, through the chat turn that asked for it. Dry runs, nested+ sub-graph runs and dream/memory sessions are never tasks.+- **Automated task**: a schedule fire, a webhook trigger, or a scheduled+ follow-up turn. Counted separately so "people doing things" and "agents+ running" can be compared.+- **Activated**: at least 3 tasks on at least 2 distinct days within 14 days+ of signup. (`analytics.user_lifecycle.activated`)+- **Last active**: latest of last task, last visit, last scheduled run,+ falling back to signup. Because a scheduled run counts, an account whose+ schedule fires daily is never `stale_*` or `churned_30d` while it keeps+ firing; `retention_task_weekly` deliberately ignores schedules and measures+ people coming back. Use the one that matches the question.+- **Login / visit**: a session row, from `auth.sessions` (Supabase, history+ up to the Better Auth cutover on 2026-07-30) or `platform.UserAuthSession`+ (Better Auth, everything since). Every login column reads both.+- **Stale**: no activity in 14 / 30 days (`stale_14d`, `stale_30d`).+- **Churned (30d)**: had at least one task ever, signed up more than 30 days+ ago, no activity in the last 30 days.+- **Never activated (30d)**: signed up more than 30 days ago and never did a task.+- **Cost to us**: `PlatformCostLog.costMicrodollars`, our real provider spend,+ split into agent (block runs), copilot (turns) and background (dream passes).+- **Credits charged**: `CreditTransaction` `USAGE` rows, in cents. A run+ billed to an organisation writes `OrgCreditTransaction` instead; the credit+ columns in `user_task_daily`, `user_lifecycle` and `unit_economics_monthly`+ include those rows under the user who initiated them (`initiatedByUserId`),+ while cost stays with the acting user, so per-user margin stays comparable+ for org members.++## Activation events (PostHog)++Emitted server-side by `backend/util/product_analytics.py` with+`distinct_id = user id`, so they join the frontend's `posthog.identify`.+Every event carries `environment` and `source: "platform"`.++| GTM asked for | Event | Fires when | Key properties |+| --- | --- | --- | --- |+| run_agent | `run_agent` | A person starts an agent run (UI, API key, copilot tool). Not schedules, webhooks, sub-graphs or dry runs. | `trigger`, `trigger_ref`, `graph_id`, `graph_exec_id` |+| run_autopilot | `run_autopilot` | A person sends a message in an Autopilot chat. | `session_id`, `surface` (web chat, slack, telegram, discord) |+| run_expert | `run_expert` | A person sends a message in an expert chat (`kind: chat_turn`) or starts an expert workflow run (`kind: workflow_run`). | `expert_id`, `kind` |+| schedule_agent_run / schedule_autopilot_run / schedule_expert_run | `schedule_created` | Any schedule is registered, from any surface. | `target`: `agent` / `autopilot` / `expert`, `cron`, `is_recurring`, `schedule_id` |+| schedule_agent_ran / schedule_autopilot_ran / schedule_expert_ran | `schedule_fired` | A schedule produced work. | `target`, `schedule_id`, `graph_exec_id` or `session_id` |+| (trigger) | `trigger_fired` | A webhook produced a run. | `webhook_id`, `graph_exec_id`, `target` |+| agent_fail | `agent_run_failed` | A run reaches FAILED. | `trigger`, `failure_reason`, `expert_id` |+| — | `agent_run_completed` | A run reaches COMPLETED. | `trigger`, `cost_cents`, `duration_seconds` |+| — | `expert_hired` | A user hires an expert from a template. | `expert_id`, `template_id` |+| — | `integration_connected` | A user connects a credential, by OAuth or by pasting a key. | `provider`, `credential_type`, `method` |+| agent_idle, stale account | *(not events)* | Computed states, see `agent_health` and `user_lifecycle`. | — |++The pre-existing copilot events (`copilot_message_sent`, `copilot_tool_called`,+...) and billing events (`credit_topup_success`, `subscription_*`) are unchanged.++## SQL views (Looker)++New or extended in this change. Existing views (`retention_login_*`,+`retention_execution_*`, `users_activities`, `platform_cost_log`, ...) are+untouched.++| View | Grain | Answers |+| --- | --- | --- |+| `graph_execution` (extended) | one row per agent run | Now also `triggerSource`, `triggerRef`, `expertId`, `failureReason`, `isDryRun`, `isSubgraphRun`. |+| `chat_turn` | one row per human chat message | Autopilot vs expert usage; the SQL twin of `run_autopilot` / `run_expert`. |+| `user_task_daily` | user × day | How many tasks people run, by surface and by how they started; failures; our cost; credits charged; logins. |+| `user_lifecycle` | one row per user | First/last activity per surface, first-two-weeks behaviour, cost and revenue, and the labels `activated`, `stale_*`, `churned_30d`, `never_activated_30d`. The feature table for churn analysis. |+| `user_lifecycle_funnel_weekly` | signup-week cohort | Signup → onboarded → first task in 7d → activated in 14d → schedule / expert / purchase → retained at week 4. |+| `retention_task_weekly` | cohort × lifetime week | Retention where "active" means any human task on any surface. |+| `unit_economics_monthly` | user × month | Cost to us per user per month, per task and per active day; credits charged; gross margin; by tier. |+| `agent_health` | user × library agent | Idle, never-run and failing agents. |+| `experiment_assignment` | user × experiment | The arm each user saw; join on `user_id` to split anything else by variant. |+| `user_attribution` | one row per user | Where the user came from: shared anonymous id, PostHog device id, DataFast visitor id, first landing page, UTM tags, signup method. Join to `user_lifecycle` for activation by channel. |++Apply them to a database with:++```bash+cd autogpt_platform/backend+poetry run analytics-views+```++Then add each new view as a data source in Looker Studio (the header of every+`.sql` file documents its columns and example queries). `triggerSource` is+NULL on runs created before September 2026; the views expose those as+`agent_runs_untagged` and count them as human tasks.++## One identity across LaunchDarkly, PostHog and the database++The two tools used to disagree about who a visitor is. LaunchDarkly only+knew a user after login (every logged-out visitor shared the literal key+`anonymous`, so it could not bucket pre-signup at all), while PostHog minted+its own anonymous id and merged it into the user id at identify. Nothing+tied a pre-signup arm to a post-signup person, and nothing tied either to+the DataFast visitor that marketing sees.++Now the frontend owns one first-party anonymous id+(`src/services/analytics/anonymous-id.ts`, stored in localStorage, adopting+an existing PostHog device id when there is one) and hands it to everything:++- **PostHog** is bootstrapped with it as the anonymous distinct id, so+ `identify(user.id)` merges it into the person as usual.+- **LaunchDarkly** gets it as the anonymous `user` key before login, and as+ a `device` context alongside the `user` context after login. Rules on the+ `user` kind are unchanged; a rule that buckets by `device` keeps the same+ arm across signup.+- **The database** stores it on the user at first login (`UserAttribution`,+ reported by the browser to `POST /api/analytics/attribution`) together+ with the PostHog device id, the DataFast visitor and session ids (from the+ request headers the frontend already sends), the first landing page, UTM+ tags and the signup method. `analytics.user_attribution` exposes it.++Rule of thumb: **LaunchDarkly gates, PostHog measures, the database joins.**+Server-side flag evaluation stays keyed by user id (the backend has no+device context yet), so bucket pre-login experiments on the client.++## Running an experiment++1. Create a **multivariate feature flag** in PostHog with the arm keys you want+ (`control`, `variant-a`, `variant-b`, ...) and make it an experiment there.+2. In the frontend, read it through `useExperiment(key)` from+ `src/services/experiments/useExperiment.ts`. It returns `{ variant, isResolved }`:+ render the control experience until `isResolved`, and do not fire one-shot+ side effects before then, or a late variant is mis-recorded as control.+ The hook reports the arm to `POST /api/experiments/assignments` once per+ user and experiment; the backend keeps the **first** arm it sees.+3. Prefer a LaunchDarkly flag for the arms? Use `useLaunchDarklyExperiment(flagKey)`+ with a string-valued flag. It reports the assignment the same way+ (`source = launchdarkly`) and sends PostHog an `experiment_exposed` event+ carrying `$feature/<flag>`, so a PostHog experiment can use it as its+ exposure and measure the activation events against it.+4. Backend-decided experiments can call+ `backend.data.experiments.record_assignment(user_id, key, variant, source="backend")`.+5. Read results in PostHog (exposure + the activation events above as goals),+ and in Looker by joining `analytics.experiment_assignment` to+ `user_lifecycle`, `user_task_daily` or `unit_economics_monthly`.++The onboarding paywall experiment (`subscription-pricing-page-initial-state`)+is already wired this way.++## Deploying this change++1. Run the migrations `20260902120000_add_execution_trigger_source` (adds+ `AgentGraphExecution.triggerSource` / `triggerRef`),+ `20260902120100_add_experiment_assignment` (the `ExperimentAssignment`+ table) and `20260902120200_add_user_attribution` (the `UserAttribution`+ table). Existing `analytics.*` views are unaffected: no column they select+ from changes type.+2. Deploy the backend. From that moment every run row carries its trigger and+ every activation event flows to PostHog.+3. Nick Tindle runs `poetry run analytics-views` against production right+ after the deploy that carries this change, before any Looker source is+ added. Before that, on staging: `EXPLAIN (ANALYZE, BUFFERS)` on+ `user_lifecycle`, `user_lifecycle_funnel_weekly`, `experiment_assignment`+ and `user_attribution`. `user_lifecycle` has no date bound (it is a+ per-user lifetime table), so every refresh aggregates the full history of+ `AgentGraphExecution`, `ChatMessage`, `PlatformCostLog` and+ `CreditTransaction`; if staging shows that hurting, the follow-up is a+ nightly-refreshed materialized view (a `generate_views.py` change), not a+ Looker source on the live view. Then wire the new views into Looker Studio.+4. Confirm in PostHog that `run_agent`, `run_autopilot`, `schedule_created`+ and `agent_run_failed` are arriving with `source = platform`.++## The questions GTM will ask next++The list GTM gave us is the first layer. These are the questions that follow+once they see the data, with where the answer already lives or what is still+missing.++**Activation**++- *Which first action predicts retention: an agent run, an Autopilot chat, an+ expert, or a schedule?* — `user_lifecycle` has the first-time timestamps+ and early counts per surface; group by the earliest one and compare+ `churned_30d`.+- *How long from signup to first task, and where do people stall?* —+ `hours_to_first_task` plus the onboarding funnel view; combine with PostHog+ session replay on users with `never_activated_30d`.+- *Does connecting an integration change activation?* —+ `integrations_connected_total` and `first_integration_connected_at` in+ `user_lifecycle` (from `IntegrationCredential`), `connected_integration` in+ the funnel, and the `integration_connected` event in PostHog.+- *Which marketing channel brings activated users?* — join+ `user_attribution` (UTM tags, referrer, landing page, DataFast visitor id)+ to `user_lifecycle`. For channels only DataFast knows, export DataFast by+ visitor id and join on `datafast_visitor_id`.++**Retention and churn**++- *Are we retaining people, or just their schedules?* — `retention_task_weekly`+ (people) next to `retention_execution_weekly` (all runs incl. automated).+- *Which early behaviours separate churned from retained users?* — the+ example query on `user_lifecycle`; the next step is a proper model, which+ needs the same table exported to a notebook.+- *Who is about to churn?* — `stale_14d` users with `tasks_28d` dropping vs+ their prior 28 days; `agent_health.idle_7d` and `failing` are the earliest+ per-agent signals. **Gap**: a weekly "at-risk" list is a view away, but+ acting on it (email, in-product nudge) needs the alerts pipeline.+- *Do failures cause churn?* — `agent_runs_failed_total` and+ `agent_runs_no_credits_total` in `user_lifecycle`; `failure_reason` in+ `graph_execution`. Insufficient balance is already separated because it is+ a pricing problem, not a product problem.++**Cost and pricing**++- *What will a free trial with card on file cost us?* —+ `unit_economics_monthly` by tier and by month-of-life; the+ `PERCENTILE_CONT` example shows the tail, which is what a trial cap must+ cover.+- *Cost per task by surface and model?* — split exists by surface in+ `user_task_daily`; per-model cost is in `platform_cost_log`. **Gap**: chat+ turns are not linked to the exact cost rows (copilot cost rows carry the+ session id in `graphExecId`), so cost per *turn* is an average, not exact.+- *Gross margin per user and per tier?* — `gross_margin_usd` in+ `unit_economics_monthly`, with the caveat that credits are prepaid.+- *How much do we spend on users who never come back?* — join+ `unit_economics_monthly` to `user_lifecycle.churned_30d`.+- *Background cost (dream/memory passes) as a share of total?* —+ `background_cost_usd` in `user_task_daily` and `unit_economics_monthly`.++**Experiments**++- *Did the pricing-page arm change activation or retention, not just+ conversion?* — `experiment_assignment` joined to `user_lifecycle`.+- *Can we A/B backend behaviour (model routing, default expert, prompt)?* —+ yes via `record_assignment(source="backend")`; LaunchDarkly stays the+ gate, PostHog and the assignment table hold the arm.+- *Can the team keep using LaunchDarkly for arms?* — yes, through+ `useLaunchDarklyExperiment`; the arm still lands in PostHog and the+ assignment table. **Gap**: the backend's LaunchDarkly context has no+ `device` kind yet, so server-evaluated flags cannot bucket by the+ anonymous id.++**Experts and schedules**++- *Do hired experts get used after day one?* — `expert_turns_total`,+ `expert_workflow_runs_total`, `experts_active` in `user_lifecycle`;+ `expert_hired` event in PostHog for the funnel.+- *How many schedules are created and then silently stop?* — creations are in+ `ActivityEvent`, fires are in `graph_execution.triggerSource = 'schedule'`.+ **Gap**: schedules live only in the APScheduler job store, so there is no+ durable schedule row to join pauses and deletions to; a `Schedule` table+ would make schedule health first-class.++**Operational gaps to close next**++- No `lastActiveAt` on `User`; `user_lifecycle` derives it, which is fine for+ dashboards but not for real-time gating.+- Frontend PostHog is not bootstrapped with flags, so experiment variants+ resolve after first paint. `useExperiment.isResolved` papers over this;+ server-side bootstrap would remove the delay.+- DataFast goals fire only after analytics consent, so they undercount+ relative to the server-side events. Use the server-side numbers as truth+ and DataFast for channel attribution only.