Custom Tables and Queries
Learn how to create custom tables and write SQL queries for advanced analytics
Analytics Database Documentation
Database Overview
The analytics data is stored in a database called default
which contains multiple tables. For this documentation, we’ll focus on the analytics
table, which can be accessed using the format default.analytics
.
Table Structure
The analytics
table contains the following columns:
Column Name | Type | Description |
---|---|---|
id | UUID | Unique identifier for each record |
event_name | LowCardinality(String) | Name of the tracked event |
distinct_id | LowCardinality(String) | Unique identifier for tracking |
properties | String | JSON object containing detailed event data |
created_at | DateTime | Timestamp of event creation |
Properties Structure
The properties
column contains a JSON object with rich event data. Here’s the standard structure:
Top-Level Properties
-
distinct
: Contains user identification information -
page
: Page-related information -
screen
: Device screen specifications -
browser
: Browser details -
device_type
: Type of device (e.g., desktop, mobile) -
timezone
: User’s timezone -
timestamp
: Event timestamp -
query_params
: URL query parameters -
events
: Array of detailed event information -
batch_size
: Number of events in batch -
batch_timestamp
: Timestamp of the batch
Properties Detail
Page Information
Screen Information
Browser Information
Querying Data
Basic Query Structure
To query the analytics table:
Extracting JSON Properties
The properties
column contains nested JSON data that can be accessed using specialized functions:
String Extraction
Numeric Extraction
Working with Events Array
The events
array contains detailed event information. To access specific events:
Example Queries
Here’s a complete example that extracts multiple properties:
Best Practices
-
Always use appropriate JSON extraction functions based on the expected data type:
-
JSONExtractString
for text -
JSONExtractFloat
for decimal numbers -
JSONExtractInt
for whole numbers -
JSONExtractBool
for boolean values
-
-
When working with arrays, remember that indexing starts at 1, not 0
-
For nested properties, chain the extraction functions from outer to inner elements
-
Use meaningful aliases in your SELECT statements to make the output more readable
-
Consider adding LIMIT clauses during development to avoid processing large amounts of data
Was this page helpful?