Skip to content

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

Refactor getSingleCol API to unify entry and exit page queries

Bill Yang79d5a6f58bdc
diff --git a/server/src/api/getSingleCol.ts b/server/src/api/getSingleCol.ts
index f981d37b2..6c8933a95 100644
--- a/server/src/api/getSingleCol.ts
+++ b/server/src/api/getSingleCol.ts
@@ -42,46 +42,24 @@ const getQuery = (request: GenericRequest["Querystring"]) => {
const filterStatement = getFilterStatement(filters);
- if (parameter === "exit_page") {
+ if (parameter === "exit_page" || parameter === "entry_page") {
return `
- SELECT
- pathname as value,
- COUNT(*) as count,
- ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
- FROM (
- SELECT
- session_id,
- argMax(hostname, timestamp) AS hostname,
- argMax(pathname, timestamp) AS pathname
- FROM pageviews
- WHERE
- site_id = ${site}
- ${getTimeStatement(startDate, endDate, timezone)}
- GROUP BY session_id
- ) AS exit_pages
- GROUP BY value ORDER BY count desc
- ${limit ? `LIMIT ${limit}` : ""};`;
- }
-
- if (parameter === "entry_page") {
- return `
- SELECT
- pathname as value,
- COUNT(*) as count,
+ SELECT
+ ${parameter} as value,
+ count() as count,
ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
- FROM (
- SELECT
- session_id,
- argMin(hostname, timestamp) AS hostname,
- argMin(pathname, timestamp) AS pathname
- FROM pageviews
- WHERE
- site_id = ${site}
- ${getTimeStatement(startDate, endDate, timezone)}
- GROUP BY session_id
- ) AS entry_pages
- GROUP BY value ORDER BY count desc
- ${limit ? `LIMIT ${limit}` : ""};`;
+ FROM
+ sessions_mv
+ WHERE
+ site_id = ${site}
+ AND notEmpty(entry_page)
+ ${getTimeStatement(startDate, endDate, timezone, "sessions")}
+ GROUP BY
+ ${parameter}
+ ORDER BY
+ COUNT() DESC
+ ${limit ? `LIMIT ${limit}` : ""};
+ `;
}
if (parameter === "dimensions") {
diff --git a/server/src/api/utils.ts b/server/src/api/utils.ts
index e331cb488..33b6168e0 100644
--- a/server/src/api/utils.ts
+++ b/server/src/api/utils.ts
@@ -4,16 +4,20 @@ import { Filter, FilterParameter, FilterType } from "./types.js";
export function getTimeStatement(
startDate: string,
endDate: string,
- timezone: string
+ timezone: string,
+ table: "events" | "sessions" = "events"
) {
if (!startDate && !endDate) {
return "";
}
- return `AND timestamp >= toTimeZone(
+
+ const col = table === "events" ? "timestamp" : "session_end";
+
+ return `AND ${col} >= toTimeZone(
toStartOfDay(toDateTime('${startDate}', '${timezone}')),
'UTC'
)
- AND timestamp < if(
+ AND ${col} < if(
toDate('${endDate}') = toDate(now(), '${timezone}'),
now(),
toTimeZone(
diff --git a/server/src/db/clickhouse/clickhouse.ts b/server/src/db/clickhouse/clickhouse.ts
index cb6388fe9..5ef2d01ef 100644
--- a/server/src/db/clickhouse/clickhouse.ts
+++ b/server/src/db/clickhouse/clickhouse.ts
@@ -116,7 +116,7 @@ export const initializeClickhouse = async () => {
});
await clickhouse.exec({
query: `
- CREATE MATERIALIZED VIEW sessions_mv
+ CREATE MATERIALIZED VIEW IF NOT EXISTS sessions_mv
TO sessions
AS
SELECT