🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
export const PRIVATE_KEY_PATTERN = /^[a-f0-9]{12}$/i;
export const SYNCED_ANALYTICS_ROUTES = new Set([
"main",
"sessions",
"users",
"performance",
"globe",
"goals",
"events",
"funnels",
"journeys",
"retention",
"errors",
"pages",
"replay",
"feature-flags",
"experiments",
"bots",
"user",
"dashboards",
]);
/**
* Synced routes usually opened from a link without a query string: a profile
* from a list of users, a dashboard from the dashboards list, the next user in
* the stepper. An address with no analytics state keeps the period and filters
* the page was opened with instead of resetting them; a refreshed or shared
* address restores its own like every other synced route.
*/
const CARRY_OVER_ANALYTICS_ROUTES = new Set(["user", "dashboards"]);
export type SiteRouteContext = {
siteId: string | null;
privateKey: string | null;
route: string | null;
};
export function getSiteRouteContext(pathname: string | null | undefined): SiteRouteContext {
const segments = pathname?.split("/").filter(Boolean) ?? [];
const siteId = segments[0] ?? null;
const hasPrivateKey = !!segments[1] && PRIVATE_KEY_PATTERN.test(segments[1]);
return {
siteId,
privateKey: hasPrivateKey ? segments[1] : null,
route: hasPrivateKey ? (segments[2] ?? null) : (segments[1] ?? null),
};
}
export function isSyncedAnalyticsRoute(route: string | null | undefined) {
return !!route && SYNCED_ANALYTICS_ROUTES.has(route);
}
export function isCarryOverAnalyticsRoute(route: string | null | undefined) {
return !!route && CARRY_OVER_ANALYTICS_ROUTES.has(route);
}
export function getMainDashboardPath(pathname: string | null | undefined) {
const { siteId, privateKey } = getSiteRouteContext(pathname);
if (!siteId || isNaN(Number(siteId))) return null;
return privateKey ? `/${siteId}/${privateKey}/main` : `/${siteId}/main`;
}