Skip to content

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

Traits (#733)

Bill Yangca8400770c4b
diff --git a/client/src/app/components/AddSite.tsx b/client/src/app/components/AddSite.tsx
index 37c897499..f6b7031b8 100644
--- a/client/src/app/components/AddSite.tsx
+++ b/client/src/app/components/AddSite.tsx
@@ -23,9 +23,17 @@ import { resetStore, useStore } from "../../lib/store";
import { SubscriptionData, useStripeSubscription } from "../../lib/subscription/useStripeSubscription";
import { isValidDomain, normalizeDomain } from "../../lib/utils";
import { FREE_SITE_LIMIT, IS_CLOUD, PRO_SITE_LIMIT, STANDARD_SITE_LIMIT } from "../../lib/const";
+import { DateTime } from "luxon";
const getSiteLimit = (subscription: SubscriptionData | undefined) => {
if (subscription?.planName.includes("standard")) {
+ // grant unlimited sites to organizations created before June 27, 2025
+ if (
+ subscription?.createdAt &&
+ DateTime.fromISO(subscription.createdAt) < DateTime.fromFormat("2025-06-27", "yyyy-MM-dd")
+ ) {
+ return PRO_SITE_LIMIT;
+ }
return STANDARD_SITE_LIMIT;
}
if (subscription?.planName.includes("pro")) {
@@ -51,6 +59,8 @@ export function AddSite({ trigger, disabled }: { trigger?: React.ReactNode; disa
const { data: sites, refetch } = useGetSitesFromOrg(activeOrganization?.id);
const { data: subscription } = useStripeSubscription();
+ console.info(subscription);
+
const isOverSiteLimit = getSiteLimit(subscription) <= (sites?.sites?.length || 0) && IS_CLOUD;
const finalDisabled = disabled || isOverSiteLimit;
diff --git a/client/src/app/layout.tsx b/client/src/app/layout.tsx
index 8c12a4f24..f51226dbe 100644
--- a/client/src/app/layout.tsx
+++ b/client/src/app/layout.tsx
@@ -1,19 +1,19 @@
"use client";
+import { useAppEnv } from "@/hooks/useIsProduction";
+import { useStopImpersonation } from "@/hooks/useStopImpersonation";
import QueryProvider from "@/providers/QueryProvider";
+import { ThemeProvider } from "next-themes";
import { Inter } from "next/font/google";
+import Script from "next/script";
+import { NuqsAdapter } from "nuqs/adapters/next/app";
+import { AuthenticationGuard } from "../components/AuthenticationGuard";
+import { OrganizationInitializer } from "../components/OrganizationInitializer";
import { Toaster } from "../components/ui/sonner";
import { TooltipProvider } from "../components/ui/tooltip";
import { cn } from "../lib/utils";
import "./globals.css";
-import Script from "next/script";
-import { useStopImpersonation } from "@/hooks/useStopImpersonation";
-import { useAppEnv } from "@/hooks/useIsProduction";
import { ReactScan } from "./ReactScan";
-import { OrganizationInitializer } from "../components/OrganizationInitializer";
-import { AuthenticationGuard } from "../components/AuthenticationGuard";
-import { ThemeProvider } from "next-themes";
-import { NuqsAdapter } from "nuqs/adapters/next/app";
const inter = Inter({ subsets: ["latin"] });
diff --git a/client/src/components/OrganizationInitializer.tsx b/client/src/components/OrganizationInitializer.tsx
index 1e74256b2..8f3cd7955 100644
--- a/client/src/components/OrganizationInitializer.tsx
+++ b/client/src/components/OrganizationInitializer.tsx
@@ -3,6 +3,7 @@
import { useEffect } from "react";
import { authClient } from "../lib/auth";
import { useUserOrganizations } from "../api/admin/organizations";
+import { useTrack } from "../hooks/useTrack";
function OrganizationInitializerInner() {
const { data: organizations } = useUserOrganizations();
@@ -21,6 +22,7 @@ function OrganizationInitializerInner() {
export function OrganizationInitializer() {
const session = authClient.useSession();
+ useTrack();
if (session.data?.user) {
return <OrganizationInitializerInner />;
}
diff --git a/client/src/hooks/useTrack.ts b/client/src/hooks/useTrack.ts
new file mode 100644
index 000000000..166ad7ba6
--- /dev/null
+++ b/client/src/hooks/useTrack.ts
@@ -0,0 +1,34 @@
+import { useEffect } from "react";
+import { authClient } from "@/lib/auth";
+import { useStripeSubscription } from "../lib/subscription/useStripeSubscription";
+import { useGetSitesFromOrg } from "../api/admin/sites";
+
+export function useTrack() {
+ const { data: subscription, isLoading } = useStripeSubscription();
+ const { data: activeOrganization, isPending: isPendingActiveOrganization } = authClient.useActiveOrganization();
+ const {
+ data: sites,
+ isPending: isPendingSites,
+ isLoading: isLoadingSites,
+ } = useGetSitesFromOrg(activeOrganization?.id);
+
+ const user = authClient.useSession();
+ useEffect(() => {
+ if (typeof window !== "undefined" && user.data?.user?.id && window.rybbit && !isLoading && !isLoadingSites) {
+ window.rybbit?.identify(user.data?.user?.id, {
+ email: user.data?.user?.email,
+ name: user.data?.user?.name,
+ plan: subscription?.planName,
+ organization: activeOrganization?.name,
+ sites: sites?.sites.map(site => site.domain),
+ });
+ }
+ }, [
+ user.data?.user?.id,
+ window.rybbit,
+ subscription?.planName,
+ isLoading,
+ isPendingActiveOrganization,
+ isPendingSites,
+ ]);
+}
diff --git a/client/src/lib/rybbit.d.ts b/client/src/lib/rybbit.d.ts
new file mode 100644
index 000000000..0c8e87552
--- /dev/null
+++ b/client/src/lib/rybbit.d.ts
@@ -0,0 +1,53 @@
+interface Rybbit {
+ /**
+ * Tracks a page view
+ */
+ pageview: () => void;
+
+ /**
+ * Tracks a custom event
+ * @param name Name of the event
+ * @param properties Optional properties for the event
+ */
+ event: (name: string, properties?: Record<string, any>) => void;
+
+ /**
+ * Sets a custom user ID for tracking logged-in users
+ * @param userId The user ID to set (will be stored in localStorage)
+ * @param traits Optional user metadata (email, name, custom fields)
+ */
+ identify: (userId: string, traits?: Record<string, unknown>) => void;
+
+ /**
+ * Updates traits for the currently identified user
+ * @param traits User metadata to merge with existing traits
+ */
+ setTraits: (traits: Record<string, unknown>) => void;
+
+ /**
+ * Clears the stored user ID
+ */
+ clearUserId: () => void;
+
+ /**
+ * Gets the currently set user ID
+ * @returns The current user ID or null if not set
+ */
+ getUserId: () => string | null;
+
+ /**
+ * Manually tracks outbound link clicks
+ * @param url The URL of the outbound link
+ * @param text Optional text content of the link
+ * @param target Optional target attribute of the link
+ */
+ trackOutbound: (url: string, text?: string, target?: string) => void;
+}
+
+declare global {
+ interface Window {
+ rybbit: Rybbit;
+ }
+}
+
+export {};
diff --git a/docs/src/components/rybbit.d.ts b/docs/src/components/rybbit.d.ts
index a434c3039..0c8e87552 100644
--- a/docs/src/components/rybbit.d.ts
+++ b/docs/src/components/rybbit.d.ts
@@ -3,26 +3,38 @@ interface Rybbit {
* Tracks a page view
*/
pageview: () => void;
+
/**
* Tracks a custom event
* @param name Name of the event
* @param properties Optional properties for the event
*/
event: (name: string, properties?: Record<string, any>) => void;
+
/**
* Sets a custom user ID for tracking logged-in users
* @param userId The user ID to set (will be stored in localStorage)
+ * @param traits Optional user metadata (email, name, custom fields)
*/
- identify: (userId: string) => void;
+ identify: (userId: string, traits?: Record<string, unknown>) => void;
+
+ /**
+ * Updates traits for the currently identified user
+ * @param traits User metadata to merge with existing traits
+ */
+ setTraits: (traits: Record<string, unknown>) => void;
+
/**
* Clears the stored user ID
*/
clearUserId: () => void;
+
/**
* Gets the currently set user ID
* @returns The current user ID or null if not set
*/
getUserId: () => string | null;
+
/**
* Manually tracks outbound link clicks
* @param url The URL of the outbound link
@@ -31,9 +43,11 @@ interface Rybbit {
*/
trackOutbound: (url: string, text?: string, target?: string) => void;
}
+
declare global {
interface Window {
rybbit: Rybbit;
}
}
+
export {};