Overview
Datasources are the foundation of your Tinybird data platform. This starter kit uses 6 datasources:- 1 landing datasource for raw event ingestion
- 5 materialized view target datasources for pre-aggregated metrics
Landing Datasource
analytics_events
The primary ingestion point for all web analytics events.Schema Fields
Event timestamp - when the analytics event occurred
Unique session identifier for grouping user activity
Event action type (e.g., “page_hit”, “click”, “custom_event”)
Tracker version for schema evolution support
JSON-encoded event data containing:
domain: Website domainlocale: User localelocation: Geographic locationreferrer: Traffic source URLpathname: Page pathhref: Full URLuser-agent: Browser user agent string
Multi-tenancy identifier for data isolation
Website domain (can also be extracted from payload as fallback)
Engine Configuration
toYYYYMM(timestamp) - Monthly partitioning for efficient time-based queries["tenant_id", "domain", "timestamp"] - Optimizes filtering and range scansPerformance: The sorting key is ordered by query filter frequency - most queries filter by tenant_id and domain first.
Materialized View Datasources
analytics_pages_mv
Aggregates page-level metrics for fast page analytics queries.Aggregate Functions
Unique session count using ClickHouse’s
uniqState functionMerging: Use uniqMerge(visits) to combine aggregatesTotal page view count using ClickHouse’s
countState functionMerging: Use countMerge(hits) to combine aggregatesAggregatingMergeTree automatically merges aggregate function states during background merges, enabling efficient incremental aggregation.
analytics_sessions_mv
Tracks session-level metrics including duration and hit counts.Simple vs Regular Aggregates
simpleAggregateFunction vs aggregateFunction
simpleAggregateFunction vs aggregateFunction
Simple Aggregate Functions (
any, min, max):- Lower overhead
- Don’t require
-Mergesuffix for querying - Use
anySimpleState,minSimpleState,maxSimpleState
uniq, count, sum):- More complex state
- Require
-Mergesuffix:uniqMerge(),countMerge() - More flexible for complex aggregations
Timestamp of first page view in session
Timestamp of most recent page view in session
analytics_sources_mv
Aggregates traffic source and referrer metrics.Traffic source URL - external sites that referred visitors
The materialization filters out same-domain referrers to track only external traffic sources
tenant_actions_mv
Tracks all distinct action types per tenant and domain.Use Case: This datasource powers action discovery and custom event tracking across your analytics platform.
Sample payload from most recent occurrence of this action
Total number of times this action has occurred
tenant_domains_mv
Tracks active domains for each tenant with activity timestamps.Use Case: Enables tenant management features like domain listing, activity tracking, and multi-domain analytics.
Type Inference
The SDK provides TypeScript type inference for all datasources:Type Exports
Using Types
Best Practices
Partition Pruning
Always include date filters in queries to leverage partitioning:
Sorting Key Order
Filter by sorting key columns in order for best performance:
Low Cardinality
Use
lowCardinality() for fields with less than 10,000 distinct values to reduce storageJSON Payload
Keep the payload field flexible for schema evolution without datasource migrations
Next Steps
Pipes
Learn how pipes transform raw data
Materialized Views
Understand how MVs populate these datasources
Architecture
See how datasources fit in the overall architecture