Defining Metrics

Learn how to create and manage business metrics

Last updated: 1/17/2025

Metrics help you track key performance indicators (KPIs) and business health.

What are Metrics?

Metrics in ContextFlo are:

  • Computed values based on your data
  • Tracked over time for trend analysis
  • Reusable across dashboards and reports
  • Shareable with your team

Common Metrics

Examples of business metrics:

  • Revenue Metrics: MRR, ARR, ARPU
  • Growth Metrics: User growth rate, churn rate
  • Engagement Metrics: DAU, MAU, retention
  • Sales Metrics: Conversion rate, deal velocity
  • Product Metrics: Feature adoption, usage frequency

Creating a Metric

Step 1: Navigate to Metrics

Click Metrics in the sidebar, then Create New Metric.

Step 2: Define Basic Information

Provide:

  • Name: Descriptive metric name
  • Slug: URL-friendly identifier
  • Description: What this metric measures
  • Category: Group similar metrics

Step 3: Write the Formula

Metrics use SQL to compute values:

SELECT COUNT(DISTINCT user_id) as value
FROM events
WHERE event_type = 'purchase'
  AND created_at >= NOW() - INTERVAL '30 days'

Step 4: Configure Computation

Set how often to compute:

  • Daily: For high-frequency metrics
  • Weekly: For trend analysis
  • Monthly: For business reporting

Step 5: Test and Save

  1. Click Evaluate to test
  2. Verify the result makes sense
  3. Click Save

Metric Formulas

Simple Count

SELECT COUNT(*) as value
FROM users
WHERE created_at >= NOW() - INTERVAL '1 day'

Sum of Values

SELECT SUM(amount) as value
FROM orders
WHERE status = 'completed'
  AND created_at >= NOW() - INTERVAL '1 day'

Average

SELECT AVG(response_time_ms) as value
FROM api_requests
WHERE created_at >= NOW() - INTERVAL '1 hour'

Percentage

SELECT
  (COUNT(*) FILTER (WHERE status = 'success') * 100.0 /
   NULLIF(COUNT(*), 0)) as value
FROM api_requests
WHERE created_at >= NOW() - INTERVAL '1 day'

Growth Rate

WITH current_period AS (
  SELECT COUNT(*) as count
  FROM users
  WHERE created_at >= NOW() - INTERVAL '30 days'
),
previous_period AS (
  SELECT COUNT(*) as count
  FROM users
  WHERE created_at >= NOW() - INTERVAL '60 days'
    AND created_at < NOW() - INTERVAL '30 days'
)
SELECT
  ((c.count - p.count) * 100.0 / NULLIF(p.count, 0)) as value
FROM current_period c, previous_period p

Metric Dependencies

Metrics can reference other metrics:

-- Metric: revenue_per_user
SELECT
  @total_revenue / NULLIF(@active_users, 0) as value

The @metric_slug syntax references other metrics by their slug.

Best Practices

Use Descriptive Names

Good:

  • Monthly Recurring Revenue (MRR)
  • Daily Active Users (DAU)
  • Customer Acquisition Cost (CAC)

Bad:

  • Metric 1
  • Data
  • Count

Include Time Windows

Always specify time ranges:

-- Good
WHERE created_at >= NOW() - INTERVAL '30 days'

-- Bad
WHERE created_at IS NOT NULL

Handle NULL Values

Use NULLIF to avoid division by zero:

SELECT
  total / NULLIF(count, 0) as value

Document Your Logic

Add comments to complex formulas:

-- Calculate churn rate:
-- (Users who left this month / Total users at start) * 100
SELECT
  (COUNT(*) FILTER (WHERE status = 'churned') * 100.0 /
   NULLIF(COUNT(*), 0)) as value
FROM users
WHERE ...

Metric Computation

Automatic Computation

Metrics are computed:

  • On schedule (daily, weekly, monthly)
  • When manually triggered
  • When referenced by dashboards

Computation History

View historical values:

  1. Click on a metric
  2. Go to History tab
  3. See trends over time

Manual Computation

Force recomputation:

  1. Open the metric
  2. Click Compute Now
  3. Wait for completion

Metric Validation

ContextFlo validates metrics before saving:

  • ✅ SQL syntax check
  • ✅ Table existence verification
  • ✅ Column validation
  • ✅ Permission check

Performance Tips

Optimize Queries

Good:

SELECT COUNT(*) as value
FROM users
WHERE created_at >= NOW() - INTERVAL '7 days'
  AND status = 'active'

Bad:

SELECT COUNT(*) as value
FROM (
  SELECT * FROM users  -- Fetches all columns
  WHERE user_id IN (SELECT user_id FROM huge_table)  -- Slow subquery
) subquery

Use Indexes

Ensure indexes on:

  • Date/timestamp columns
  • Columns in WHERE clauses
  • Foreign keys

Aggregate in Database

Let the database do the work:

-- Good: Database aggregation
SELECT COUNT(*) as value FROM users

-- Bad: Fetch all, count in code
-- (This would happen if you tried to count in app code)

Metric Categories

Organize metrics into categories:

  • Growth: User growth, revenue growth
  • Engagement: DAU, MAU, session length
  • Revenue: MRR, ARPU, LTV
  • Sales: Conversion rate, pipeline value
  • Operations: System uptime, error rate

Next Steps