Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor analytics queries to utilize events table for session data

Bill Yang5525e99ebba1
diff --git a/server/src/api/analytics/getSession.ts b/server/src/api/analytics/getSession.ts
index 7e053e0a7..37f136e8e 100644
--- a/server/src/api/analytics/getSession.ts
+++ b/server/src/api/analytics/getSession.ts
@@ -74,31 +74,34 @@ export async function getSession(
}
try {
- // 1. First query: Get session data
+ // 1. First query: Get session data derived from events
const sessionQuery = `
SELECT
session_id,
- user_id,
- country,
- region,
- language,
- device_type,
- browser,
- browser_version,
- operating_system,
- operating_system_version,
- screen_width,
- screen_height,
- referrer,
- session_end,
- session_start,
- pageviews,
- entry_page,
- exit_page
-FROM sessions
+ any(user_id) as user_id,
+ any(country) as country,
+ any(region) as region,
+ any(language) as language,
+ any(device_type) as device_type,
+ any(browser) as browser,
+ any(browser_version) as browser_version,
+ any(operating_system) as operating_system,
+ any(operating_system_version) as operating_system_version,
+ any(screen_width) as screen_width,
+ any(screen_height) as screen_height,
+ any(referrer) as referrer,
+ min(timestamp) as session_start,
+ max(timestamp) as session_end,
+ dateDiff('second', min(timestamp), max(timestamp)) as session_duration,
+ countIf(type = 'pageview') as pageviews,
+ count() as events,
+ argMinIf(pathname, timestamp, type = 'pageview') as entry_page,
+ argMaxIf(pathname, timestamp, type = 'pageview') as exit_page
+FROM events
WHERE
site_id = {siteId:Int32}
AND session_id = {sessionId:String}
+GROUP BY session_id
LIMIT 1
`;
diff --git a/server/src/api/analytics/getUserSessionCount.ts b/server/src/api/analytics/getUserSessionCount.ts
index fe930c17e..7bd4a78a7 100644
--- a/server/src/api/analytics/getUserSessionCount.ts
+++ b/server/src/api/analytics/getUserSessionCount.ts
@@ -37,9 +37,9 @@ export async function getUserSessionCount(
const query = `
SELECT
- toDate(session_start, ${SqlString.escape(timezone)}) as date,
- count() as sessions
- FROM sessions
+ toDate(timestamp, ${SqlString.escape(timezone)}) as date,
+ count(DISTINCT session_id) as sessions
+ FROM events
WHERE
site_id = {siteId:Int32}
AND user_id = {userId:String}
diff --git a/server/src/db/clickhouse/clickhouse.ts b/server/src/db/clickhouse/clickhouse.ts
index 5d327a289..99b27e9c4 100644
--- a/server/src/db/clickhouse/clickhouse.ts
+++ b/server/src/db/clickhouse/clickhouse.ts
@@ -48,76 +48,6 @@ export const initializeClickhouse = async () => {
ORDER BY (site_id, timestamp)
`,
});
-
- await clickhouse.exec({
- query: `
- CREATE TABLE IF NOT EXISTS sessions
- (
- site_id UInt16,
- session_id String,
- session_start DateTime,
- session_end DateTime,
- user_id String,
- pageviews UInt32,
- entry_page String,
- exit_page String,
-
- hostname String,
- referrer String,
- browser LowCardinality(String),
- browser_version LowCardinality(String),
- operating_system LowCardinality(String),
- operating_system_version LowCardinality(String),
- language LowCardinality(String),
- country LowCardinality(FixedString(2)),
- region LowCardinality(String),
- screen_width UInt16,
- screen_height UInt16,
- device_type LowCardinality(String),
- -- Version column for ReplacingMergeTree
- version UInt64
- )
- ENGINE = ReplacingMergeTree(version)
- PARTITION BY toYYYYMM(session_start)
- ORDER BY (site_id, session_id);
- `,
- });
-
- await clickhouse.exec({
- query: `
- CREATE MATERIALIZED VIEW IF NOT EXISTS sessions_mv
- TO sessions
- AS
- SELECT
- site_id,
- session_id,
- min(timestamp) AS session_start,
- max(timestamp) AS session_end,
- any(user_id) AS user_id,
- countIf(type = 'pageview') AS pageviews,
- argMinIf(pathname, timestamp, type = 'pageview') AS entry_page,
- argMaxIf(pathname, timestamp, type = 'pageview') AS exit_page,
-
- any(hostname) AS hostname,
- any(referrer) AS referrer,
- any(browser) AS browser,
- any(browser_version) AS browser_version,
- any(operating_system) AS operating_system,
- any(operating_system_version) AS operating_system_version,
- any(language) AS language,
- any(country) AS country,
- any(region) AS region,
- any(screen_width) AS screen_width,
- any(screen_height) AS screen_height,
- any(device_type) AS device_type,
- -- Use the largest timestamp as the 'version'
- max(toUInt64(timestamp)) AS version
- FROM events
- GROUP BY
- site_id,
- session_id;
- `,
- });
};
export default clickhouse;