Hanzo
CommerceCommerce Modules

Analytics Module

Commerce events, dashboards, and ClickHouse integration

The Analytics module captures commerce events and stores them in ClickHouse for real-time dashboards, reporting, and business intelligence.

Key Concepts

  • Event -- a timestamped record of a commerce action (page view, add to cart, purchase)
  • Dashboard -- pre-built and custom views of aggregated metrics
  • Funnel -- a conversion pipeline from visitor to purchaser
  • Cohort -- a group of customers segmented by behavior or time period

Event Schema

{
  "id": "evt_abc123",
  "type": "order.completed",
  "timestamp": "2025-01-15T10:35:00Z",
  "sessionId": "sess_xyz",
  "customerId": "cust_abc123",
  "properties": {
    "orderId": "order_xyz789",
    "total": 6791,
    "currency": "USD",
    "itemCount": 2,
    "paymentMethod": "stripe"
  },
  "context": {
    "ip": "203.0.113.42",
    "userAgent": "Mozilla/5.0...",
    "referrer": "https://google.com",
    "country": "US"
  }
}

Tracked Events

EventTrigger
page.viewedStorefront page load
product.viewedProduct detail page
cart.item_addedItem added to cart
cart.item_removedItem removed from cart
checkout.startedCart completion initiated
order.completedOrder successfully placed
payment.capturedPayment collected
order.refundedRefund processed

API Examples

Track a Custom Event

curl -X POST https://api.hanzo.ai/analytics/event \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "product.viewed",
    "sessionId": "sess_xyz",
    "properties": {
      "productId": "prod_abc123",
      "productName": "Premium T-Shirt",
      "price": 2999,
      "currency": "USD"
    }
  }'

Query Metrics

curl "https://api.hanzo.ai/analytics/metrics?metric=revenue&period=30d&granularity=day" \
  -H "Authorization: Bearer $TOKEN"

Response:

{
  "metric": "revenue",
  "period": "30d",
  "total": 1284500,
  "currency": "USD",
  "dataPoints": [
    {"date": "2025-01-01", "value": 42500},
    {"date": "2025-01-02", "value": 38900}
  ]
}

Conversion Funnel

curl "https://api.hanzo.ai/analytics/funnel?steps=page.viewed,product.viewed,cart.item_added,order.completed&period=7d" \
  -H "Authorization: Bearer $TOKEN"

Built-in Dashboards

DashboardMetrics
OverviewRevenue, orders, AOV, conversion rate
ProductsBest sellers, views, add-to-cart rate
CustomersNew vs returning, LTV, retention
MarketingTraffic sources, campaign performance
InventoryStock levels, sell-through rate

ClickHouse Integration

Events are written to ClickHouse for fast analytical queries:

SELECT
    toDate(timestamp) AS day,
    count() AS orders,
    sum(JSONExtractInt(properties, 'total')) AS revenue
FROM commerce_events
WHERE type = 'order.completed'
    AND timestamp >= now() - INTERVAL 30 DAY
GROUP BY day
ORDER BY day

Configuration

analytics:
  enabled: true
  provider: clickhouse         # clickhouse | bigquery
  clickhouse_url: ${CLICKHOUSE_URL}
  batch_size: 1000             # events per batch write
  flush_interval_seconds: 5    # max time between flushes
  retention_days: 365          # event retention period

Commerce events are automatically tracked for all API operations. Use the analytics/event endpoint to add custom storefront events like page views and product impressions.

How is this guide?

Last updated on

On this page