Querying Analytics

Analytics

Query your analytics data via the API to build custom dashboards and reports.

Flexible Queries

Filter, group, aggregate

Time Series

Hourly, daily, weekly trends

Funnels

Conversion analysis

Data Export

CSV for external tools

Query Events

Query events with filters and aggregations:

import { platform } from '@/lib/platform'

// Get event count
const result = await platform.analytics.query({
  event: 'signup_completed',
  dateRange: {
    from: '2024-01-01',
    to: '2024-01-31',
  },
  aggregation: 'count',
})
// { count: 1523 }

// Group by property
const byMethod = await platform.analytics.query({
  event: 'signup_completed',
  dateRange: { from: '2024-01-01', to: '2024-01-31' },
  aggregation: 'count',
  groupBy: 'properties.method',
})
// [
//   { method: 'email', count: 892 },
//   { method: 'google', count: 521 },
//   { method: 'github', count: 110 },
// ]

Time Series

Get event counts over time:

// Daily signups for the last 30 days
const timeSeries = await platform.analytics.query({
  event: 'signup_completed',
  dateRange: { from: '-30d', to: 'now' },
  aggregation: 'count',
  groupBy: 'day',
})
// [
//   { date: '2024-01-01', count: 45 },
//   { date: '2024-01-02', count: 52 },
//   { date: '2024-01-03', count: 38 },
//   ...
// ]

// Hourly breakdown
const hourly = await platform.analytics.query({
  event: 'page_viewed',
  dateRange: { from: '-24h', to: 'now' },
  aggregation: 'count',
  groupBy: 'hour',
})

Filters

Filter events by properties:

// Filter by property value
const proSignups = await platform.analytics.query({
  event: 'subscription_started',
  dateRange: { from: '-30d', to: 'now' },
  filters: [
    { property: 'properties.plan', operator: 'eq', value: 'pro' },
  ],
  aggregation: 'count',
})

// Multiple filters (AND)
const highValueOrders = await platform.analytics.query({
  event: 'order_completed',
  dateRange: { from: '-30d', to: 'now' },
  filters: [
    { property: 'properties.total', operator: 'gte', value: 100 },
    { property: 'properties.status', operator: 'eq', value: 'completed' },
  ],
  aggregation: 'sum',
  aggregationProperty: 'properties.total',
})

// Filter operators: eq, neq, gt, gte, lt, lte, contains, not_contains

Aggregations

Supported aggregation types:

PropertyTypeDescription
countnumberCount of events
countUniquenumberUnique values of a property
sumnumberSum of a numeric property
avgnumberAverage of a numeric property
minnumberMinimum value
maxnumberMaximum value
// Total revenue
const revenue = await platform.analytics.query({
  event: 'order_completed',
  dateRange: { from: '-30d', to: 'now' },
  aggregation: 'sum',
  aggregationProperty: 'properties.total',
})

// Average order value
const aov = await platform.analytics.query({
  event: 'order_completed',
  dateRange: { from: '-30d', to: 'now' },
  aggregation: 'avg',
  aggregationProperty: 'properties.total',
})

// Unique users who purchased
const buyers = await platform.analytics.query({
  event: 'order_completed',
  dateRange: { from: '-30d', to: 'now' },
  aggregation: 'countUnique',
  aggregationProperty: 'userId',
})

Funnels

Analyze conversion funnels:

const funnel = await platform.analytics.funnel({
  steps: [
    { event: 'signup_started' },
    { event: 'signup_completed' },
    { event: 'onboarding_started' },
    { event: 'onboarding_completed' },
    { event: 'subscription_started' },
  ],
  dateRange: { from: '-30d', to: 'now' },
  conversionWindow: '7d', // User must complete within 7 days
})

// Result:
// {
//   steps: [
//     { event: 'signup_started', count: 1000, conversionRate: 100 },
//     { event: 'signup_completed', count: 750, conversionRate: 75 },
//     { event: 'onboarding_started', count: 600, conversionRate: 80 },
//     { event: 'onboarding_completed', count: 450, conversionRate: 75 },
//     { event: 'subscription_started', count: 180, conversionRate: 40 },
//   ],
//   overallConversionRate: 18,
// }

User Sessions

Query user session data:

// Get user's recent events
const userEvents = await platform.analytics.getUserEvents({
  userId: user.id,
  limit: 100,
  dateRange: { from: '-7d', to: 'now' },
})

// Get user's sessions
const sessions = await platform.analytics.getUserSessions({
  userId: user.id,
  limit: 10,
})

Export Data

Export raw event data for analysis in external tools:

// Export to CSV
const exportJob = await platform.analytics.export({
  events: ['signup_completed', 'subscription_started'],
  dateRange: { from: '2024-01-01', to: '2024-01-31' },
  format: 'csv',
})

// Check export status
const status = await platform.analytics.getExportStatus(exportJob.id)
if (status.ready) {
  // Download from status.downloadUrl
}

Large Exports

For large date ranges, exports are processed asynchronously. You'll receive a download URL when ready.