Feature: Email Analytics
Relevant source files
The following files were used as context for generating this wiki page:The Email Analytics feature provides users with the ability to track their email activity and trends over time. It is designed to offer insights into email volume, actions taken, and overall productivity within the inbox. The system is built upon Tinybird for real-time data ingestion and querying, which allows for efficient collection and display of analytics data.
This feature consists of three main parts: data ingestion to track user events, data processing within Tinybird using pipes, and data retrieval via an API to be displayed on the user's statistics page.
Sources: README.md, ARCHITECTURE.md
Architecture Overview
The analytics architecture is a loop that starts with a user action in the web application. This action triggers an event that is sent to Tinybird for ingestion. Tinybird processes this raw event data through a series of data pipes to create aggregated, queryable datasets. The frontend then calls a dedicated API endpoint in the Next.js application, which in turn queries the processed data from Tinybird and returns it to the client for rendering in charts and summaries.
Sources: ARCHITECTURE.md:100-102
The following diagram illustrates the high-level data flow for the Email Analytics feature.
Data Ingestion
Event data is captured in the frontend and sent to Tinybird using a helper utility.
Sources: apps/web/utils/tinybird.ts
Event Tracking
The primary function for data ingestion is trackEvent, located in apps/web/utils/tinybird.ts. This function is responsible for sending event data to Tinybird's ingestion API. It takes an event object, which must conform to the TinybirdEvent type.
The trackEvent function constructs a request to the Tinybird events API (https://api.tinybird.co/v0/events) and sends the event payload. It is designed to not block the user's interaction by running in the background and not throwing errors that would interrupt the UI.
Sources: apps/web/utils/tinybird.ts:8-18
Tracked Events
The system tracks a variety of events related to email and application interactions. The name of the event is a key property that determines the type of action being recorded.
| Event Name | Description |
|---|---|
email_received | An email was received in the user's inbox. |
email_sent | An email was sent by the user. |
email_archived | An email was archived. |
email_trashed | An email was moved to trash. |
email_replied | The user replied to an email. |
email_action_rule_executed | An AI rule was executed on an email. |
user_subscribed | A user subscribed to a premium plan. |
user_unsubscribed | A user unsubscribed from a premium plan. |
user_login | A user logged in. |
user_signup | A new user signed up. |
ai_tokens_used | Records the number of tokens used in an AI call. |
Sources: packages/tinybird/src/index.ts:13-33
The following sequence diagram shows how a user action leads to data ingestion in Tinybird.
Sources: apps/web/utils/tinybird.ts:8-18
Data Processing in Tinybird
Once events are ingested, they are stored and processed within Tinybird using Data Sources and Pipes.
Sources: packages/tinybird/src/index.ts
Data Sources
Data Sources are the underlying tables in Tinybird where raw data is stored. The primary data source for analytics is events_v2.
| Data Source | Description |
|---|---|
events_v2 | Stores all raw event data sent from the application. |
Sources: packages/tinybird/src/index.ts:41
Pipes
Pipes are SQL queries that transform data from data sources into a more useful, aggregated format. The email_stats_by_period pipe is used to generate the statistics for the analytics page.
| Pipe Name | Description |
|---|---|
email_stats_by_period | Aggregates event counts (received, sent, archived, trashed) by a specified time period (day, week, month) and timezone. |
ai_usage_by_user_v2 | Tracks AI token usage per user. |
active_users | Calculates the number of active users. |
daily_subscribers | Tracks daily subscription counts. |
Sources: packages/tinybird/src/index.ts:46-50
Data Retrieval and Display
The processed analytics data is retrieved via a backend API and displayed on the stats page in the frontend.
API Endpoint: GET /api/user/stats/by-period
This API route is responsible for fetching the aggregated email statistics from Tinybird. It acts as a proxy between the frontend and the Tinybird Pipe API.
The endpoint uses the tinybird.getStatsByPeriod method from the Tinybird client package, passing along the period and tz (timezone) parameters from the request.
Sources: apps/web/app/api/user/stats/by-period/route.ts:10-18
Query Parameters:
| Parameter | Type | Description |
|---|---|---|
period | string | The time period for aggregation. Can be 7d, 30d, or 90d. Defaults to 30d. |
tz | string | The timezone to use for date calculations, e.g., America/New_York. Defaults to UTC. |
Sources: apps/web/app/api/user/stats/by-period/route.ts:11-12
Frontend Display
The analytics are displayed on the stats page, located at /[emailAccountId]/stats. This page is a client component that fetches data from the /api/user/stats/by-period endpoint.
The component uses a hook (likely useSWR, a common pattern in the project) to fetch the data and manage loading/error states. It then renders several StatCard components to display key metrics like "Emails Received," "Emails Sent," and "Emails Archived." A StatsChart component is used to visualize the trends over the selected period.
Sources: apps/web/app/(app)/[emailAccountId]/stats/page.tsx:21-57
The following sequence diagram shows the data retrieval flow.