Skip to content

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

Enhance analytics API and UI with session/event type support

Bill Yangde5611c7172d
diff --git a/client/src/api/analytics/useSingleCol.ts b/client/src/api/analytics/useSingleCol.ts
index 895ed1030..84cee0174 100644
--- a/client/src/api/analytics/useSingleCol.ts
+++ b/client/src/api/analytics/useSingleCol.ts
@@ -17,18 +17,20 @@ export function useSingleCol({
limit = 10000,
periodTime,
useFilters = true,
+ type = "events",
}: {
parameter: FilterParameter;
limit?: number;
periodTime?: PeriodTime;
useFilters?: boolean;
+ type?: "events" | "sessions";
}): UseQueryResult<APIResponse<SingleColResponse[]>> {
const { time, previousTime, site, filters } = useStore();
const timeToUse = periodTime === "previous" ? previousTime : time;
const { startDate, endDate } = getStartAndEndDate(timeToUse);
return useQuery({
- queryKey: [parameter, timeToUse, site, filters, limit],
+ queryKey: [parameter, timeToUse, site, filters, limit, type],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return authedFetch(
@@ -38,7 +40,9 @@ export function useSingleCol({
endDate ? `endDate=${endDate}&` : ""
}timezone=${timezone}&site=${site}&parameter=${parameter}${
limit ? `&limit=${limit}` : ""
- }${useFilters ? `&filters=${JSON.stringify(filters)}` : ""}`
+ }${
+ useFilters ? `&filters=${JSON.stringify(filters)}` : ""
+ }&type=${type}`
).then((res) => res.json());
},
staleTime: Infinity,
diff --git a/client/src/app/[site]/components/shared/StandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection.tsx
index 59fd1e388..8700cf0f5 100644
--- a/client/src/app/[site]/components/shared/StandardSection.tsx
+++ b/client/src/app/[site]/components/shared/StandardSection.tsx
@@ -76,6 +76,7 @@ export function StandardSection({
getValue,
getLink,
filterParameter,
+ type,
}: {
title: string;
isFetching?: boolean;
@@ -84,9 +85,11 @@ export function StandardSection({
getValue: (item: SingleColResponse) => string;
getLink?: (item: SingleColResponse) => string;
filterParameter: FilterParameter;
+ type: "events" | "sessions";
}) {
const { data, isFetching } = useSingleCol({
parameter: filterParameter,
+ type,
});
const { data: previousData, isFetching: previousIsFetching } = useSingleCol({
diff --git a/client/src/app/[site]/main/components/sections/Chloropleth.tsx b/client/src/app/[site]/main/components/sections/Chloropleth.tsx
deleted file mode 100644
index 611a36a65..000000000
--- a/client/src/app/[site]/main/components/sections/Chloropleth.tsx
+++ /dev/null
@@ -1,104 +0,0 @@
-"use client";
-
-import { scaleLinear } from "d3-scale";
-import { Feature, FeatureCollection } from "geojson";
-import { LatLngExpression } from "leaflet";
-import "leaflet/dist/leaflet.css";
-import dynamic from "next/dynamic";
-import { useEffect, useMemo, useState } from "react";
-import { useSingleCol } from "../../../../../api/analytics/useSingleCol";
-
-const MapContainer = dynamic(
- () => import("react-leaflet").then((mod) => mod.MapContainer),
- {
- ssr: false,
- }
-);
-
-const TileLayer = dynamic(
- () => import("react-leaflet").then((mod) => mod.TileLayer),
- {
- ssr: false,
- }
-);
-
-const GeoJSON = dynamic(
- () => import("react-leaflet").then((mod) => mod.GeoJSON),
- {
- ssr: false,
- }
-);
-
-export function Chloropleth() {
- const { data, isLoading } = useSingleCol({ parameter: "country" });
- const [geoJson, setGeoJson] = useState<FeatureCollection | null>(null);
-
- useEffect(() => {
- // Load GeoJSON data
- fetch("/countries.geojson")
- .then((res) => res.json())
- .then((data) => setGeoJson(data));
- }, []);
-
- const countryData = useMemo(() => {
- if (!data?.data) return new Map();
- return new Map(data.data.map((item) => [item.value, item]));
- }, [data?.data]);
-
- const colorScale = useMemo(() => {
- if (!data?.data) return () => "#eee";
- const maxValue = Math.max(...data.data.map((d) => d.count));
- return scaleLinear<string>()
- .domain([0, maxValue])
- .range(["hsl(var(--accent-100))", "hsl(var(--accent-600))"]);
- }, [data?.data]);
-
- const style = (feature: Feature<any, any> | undefined) => {
- const countryName = feature?.properties?.name;
- const countryStats = countryData.get(countryName);
- return {
- fillColor: countryStats ? colorScale(countryStats.count) : "#eee",
- weight: 1,
- opacity: 1,
- color: "hsl(var(--neutral-800))",
- fillOpacity: 0.7,
- };
- };
-
- const onEachFeature = (feature: Feature, layer: any) => {
- const countryName = feature.properties?.name;
- const countryStats = countryData.get(countryName);
-
- layer.bindTooltip(
- `<div class="p-2">
- <div class="font-bold">${countryName}</div>
- ${
- countryStats
- ? `<div>Pageviews: ${countryStats.count.toLocaleString()}</div>
- <div>Percentage: ${countryStats.percentage.toFixed(1)}%</div>`
- : "No data"
- }
- </div>`,
- { sticky: true }
- );
- };
-
- if (isLoading || !geoJson) return <div>Loading...</div>;
-
- return (
- <div className="h-[400px] w-full rounded-lg border overflow-hidden">
- <MapContainer
- center={[20, 0] as LatLngExpression}
- zoom={2}
- className="h-full w-full"
- zoomControl={false}
- >
- <TileLayer
- attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
- url="https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png"
- />
- <GeoJSON data={geoJson} style={style} onEachFeature={onEachFeature} />
- </MapContainer>
- </div>
- );
-}
diff --git a/client/src/app/[site]/main/components/sections/Countries.tsx b/client/src/app/[site]/main/components/sections/Countries.tsx
index db6da3823..db0a0785c 100644
--- a/client/src/app/[site]/main/components/sections/Countries.tsx
+++ b/client/src/app/[site]/main/components/sections/Countries.tsx
@@ -37,6 +37,7 @@ export function Countries() {
</TabsList>
<TabsContent value="countries">
<StandardSection
+ type="sessions"
filterParameter="country"
title="Countries"
getValue={(e) => e.value}
diff --git a/client/src/app/[site]/main/components/sections/Devices.tsx b/client/src/app/[site]/main/components/sections/Devices.tsx
index 3acfd399a..678b34432 100644
--- a/client/src/app/[site]/main/components/sections/Devices.tsx
+++ b/client/src/app/[site]/main/components/sections/Devices.tsx
@@ -39,6 +39,7 @@ export function Devices() {
</TabsList>
<TabsContent value="devices">
<StandardSection
+ type="sessions"
filterParameter="device_type"
title="Devices"
getValue={(e) => e.value}
@@ -48,6 +49,7 @@ export function Devices() {
</TabsContent>
<TabsContent value="browsers">
<StandardSection
+ type="sessions"
filterParameter="browser"
title="Browsers"
getValue={(e) => e.value}
@@ -62,6 +64,7 @@ export function Devices() {
</TabsContent>
<TabsContent value="os">
<StandardSection
+ type="sessions"
title="Operating Systems"
getValue={(e) => e.value}
getKey={(e) => e.value}
diff --git a/client/src/app/[site]/main/components/sections/Pages.tsx b/client/src/app/[site]/main/components/sections/Pages.tsx
index 51c9ae2de..82fea3235 100644
--- a/client/src/app/[site]/main/components/sections/Pages.tsx
+++ b/client/src/app/[site]/main/components/sections/Pages.tsx
@@ -42,6 +42,7 @@ export function Pages({
</TabsList>
<TabsContent value="pages">
<StandardSection
+ type="events"
filterParameter="pathname"
title="Pages"
getValue={(e) => e.value}
@@ -52,6 +53,7 @@ export function Pages({
</TabsContent>
<TabsContent value="entry_pages">
<StandardSection
+ type="events"
filterParameter="entry_page"
title="Entry Pages"
getValue={(e) => e.value}
@@ -62,6 +64,7 @@ export function Pages({
</TabsContent>
<TabsContent value="exit_pages">
<StandardSection
+ type="events"
filterParameter="exit_page"
title="Exit Pages"
getValue={(e) => e.value}
diff --git a/client/src/app/[site]/main/components/sections/Referrers.tsx b/client/src/app/[site]/main/components/sections/Referrers.tsx
index 9bdcc4683..e668e0533 100644
--- a/client/src/app/[site]/main/components/sections/Referrers.tsx
+++ b/client/src/app/[site]/main/components/sections/Referrers.tsx
@@ -34,6 +34,7 @@ export function Referrers() {
</TabsList>
<TabsContent value="referrers">
<StandardSection
+ type="sessions"
filterParameter="referrer"
title="Referrers"
getValue={(e) => e.value}
diff --git a/server/src/api/getOverview.ts b/server/src/api/getOverview.ts
index f51c1059e..009c522d3 100644
--- a/server/src/api/getOverview.ts
+++ b/server/src/api/getOverview.ts
@@ -1,12 +1,12 @@
import { FastifyReply, FastifyRequest } from "fastify";
import clickhouse from "../db/clickhouse/clickhouse.js";
-import { GenericRequest } from "./types.js";
import {
getFilterStatement,
getTimeStatement,
processResults,
} from "./utils.js";
import { getUserHasAccessToSite } from "../lib/auth-utils.js";
+import { FilterParameter } from "./types.js";
type GetOverviewResponse = {
sessions: number;
@@ -129,10 +129,21 @@ const getQuery = ({
) AS page_stats`;
};
+export interface GenericRequest {
+ Querystring: {
+ startDate: string;
+ endDate: string;
+ timezone: string;
+ site: string;
+ filters: string;
+ parameter: FilterParameter;
+ past24Hours: boolean;
+ limit?: number;
+ };
+}
+
export async function getOverview(
- req: FastifyRequest<
- GenericRequest & { Querystring: { past24Hours: boolean } }
- >,
+ req: FastifyRequest<GenericRequest>,
res: FastifyReply
) {
const { startDate, endDate, timezone, site, filters, past24Hours } =
diff --git a/server/src/api/getSingleCol.ts b/server/src/api/getSingleCol.ts
index 714174ee4..52418754a 100644
--- a/server/src/api/getSingleCol.ts
+++ b/server/src/api/getSingleCol.ts
@@ -1,6 +1,5 @@
import { FastifyReply, FastifyRequest } from "fastify";
import clickhouse from "../db/clickhouse/clickhouse.js";
-import { GenericRequest } from "./types.js";
import {
geSqlParam,
getFilterStatement,
@@ -8,6 +7,20 @@ import {
processResults,
} from "./utils.js";
import { getUserHasAccessToSite } from "../lib/auth-utils.js";
+import { FilterParameter } from "./types.js";
+
+interface GenericRequest {
+ Querystring: {
+ startDate: string;
+ endDate: string;
+ timezone: string;
+ site: string;
+ filters: string;
+ parameter: FilterParameter;
+ type: "events" | "sessions";
+ limit?: number;
+ };
+}
type GetSingleColResponse = {
value: string;
@@ -16,8 +29,16 @@ type GetSingleColResponse = {
}[];
const getQuery = (request: GenericRequest["Querystring"]) => {
- const { startDate, endDate, timezone, site, filters, parameter, limit } =
- request;
+ const {
+ startDate,
+ endDate,
+ timezone,
+ site,
+ filters,
+ parameter,
+ limit,
+ type,
+ } = request;
const filterStatement = getFilterStatement(filters);
@@ -66,8 +87,19 @@ const getQuery = (request: GenericRequest["Querystring"]) => {
return `
SELECT
${geSqlParam(parameter)} as value,
- COUNT(*) as count,
- ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
+ ${
+ type === "sessions"
+ ? "COUNT(distinct(session_id)) as count"
+ : "COUNT(*) as count"
+ },
+ ${
+ type === "sessions"
+ ? `ROUND(
+ COUNT(distinct(session_id)) * 100.0 / SUM(COUNT(distinct(session_id))) OVER (),
+ 2
+ ) as percentage`
+ : "ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage"
+ }
FROM pageviews
WHERE
site_id = ${site}
diff --git a/server/src/api/types.ts b/server/src/api/types.ts
index 493f38b68..1ea8cbb80 100644
--- a/server/src/api/types.ts
+++ b/server/src/api/types.ts
@@ -21,15 +21,3 @@ export type Filter = {
value: string[];
type: FilterType;
};
-
-export interface GenericRequest {
- Querystring: {
- startDate: string;
- endDate: string;
- timezone: string;
- site: string;
- filters: string;
- parameter: FilterParameter;
- limit?: number;
- };
-}
diff --git a/server/src/db/clickhouse/clickhouse.ts b/server/src/db/clickhouse/clickhouse.ts
index 3051387d7..14b09c0cb 100644
--- a/server/src/db/clickhouse/clickhouse.ts
+++ b/server/src/db/clickhouse/clickhouse.ts
@@ -46,20 +46,34 @@ export const initializeClickhouse = async () => {
query: `
CREATE TABLE IF NOT EXISTS sessions (
site_id UInt16,
- hostname String,
- session_id String,
start_time DateTime,
end_time DateTime,
+ session_id String,
+ user_id String,
+ hostname String,
+ pathname String,
+ querystring String,
+ page_title 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)),
+ iso_3166_2 LowCardinality(String),
+ screen_width UInt16,
+ screen_height UInt16,
+ device_type LowCardinality(String),
entry_page String,
exit_page String,
pageviews UInt32,
events UInt32,
- duration UInt32,
- referrer String,
- ip_address String,
- browser String,
- language String,
- country LowCardinality(String)
+ utm_source String,
+ utm_medium String,
+ utm_campaign String,
+ utm_term String,
+ utm_content String
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(start_time)
diff --git a/server/src/db/postgres/session-cleanup.ts b/server/src/db/postgres/session-cleanup.ts
index 550b35e6c..d27906216 100644
--- a/server/src/db/postgres/session-cleanup.ts
+++ b/server/src/db/postgres/session-cleanup.ts
@@ -49,7 +49,7 @@ export async function cleanupOldSessions() {
// deletedSessions.map((e) => ({
// ...e,
// start_time: convertPostgresToClickhouse(e.start_time),
- // last_activity: convertPostgresToClickhouse(e.last_activity),
+ // end_time: convertPostgresToClickhouse(e.end_time),
// }))
// );
// }
diff --git a/server/src/db/postgres/types.ts b/server/src/db/postgres/types.ts
index ad2d33f2b..0d6dcaffb 100644
--- a/server/src/db/postgres/types.ts
+++ b/server/src/db/postgres/types.ts
@@ -3,7 +3,7 @@ export type Session = {
user_id: string;
hostname: string;
start_time: string;
- last_activity: string;
+ end_time: string;
pageviews: number;
entry_page: string;
exit_page: string;