Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

📈 Update Posthog bootstrapping code (#1208)

Luke Vella7c1bc2290958
diff --git a/apps/web/package.json b/apps/web/package.json
index 6e97f9596..ecd5aee1e 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -64,7 +64,7 @@
"next-i18next": "^13.0.3",
"php-serialize": "^4.1.1",
"postcss": "^8.4.31",
- "posthog-js": "^1.147.0",
+ "posthog-js": "^1.148.0",
"posthog-node": "^3.6.0",
"react-big-calendar": "^1.8.1",
"react-hook-form": "^7.42.1",
diff --git a/apps/web/src/contexts/posthog.tsx b/apps/web/src/contexts/posthog.tsx
index 951c41bdc..0a6d7bfa5 100644
--- a/apps/web/src/contexts/posthog.tsx
+++ b/apps/web/src/contexts/posthog.tsx
@@ -1,32 +1,61 @@
"use client";
+import { usePathname, useSearchParams } from "next/navigation";
import posthog from "posthog-js";
-import { PostHogProvider as Provider } from "posthog-js/react";
+import { PostHogProvider as Provider, usePostHog } from "posthog-js/react";
+import React from "react";
import { useMount } from "react-use";
import { useUser } from "@/components/user-provider";
+import { env } from "@/env";
type PostHogProviderProps = React.PropsWithChildren;
+if (typeof window !== "undefined" && env.NEXT_PUBLIC_POSTHOG_API_KEY) {
+ posthog.init(env.NEXT_PUBLIC_POSTHOG_API_KEY, {
+ debug: true,
+ api_host: env.NEXT_PUBLIC_POSTHOG_API_HOST,
+ capture_pageview: false,
+ capture_pageleave: true,
+ disable_session_recording: true,
+ enable_heatmaps: false,
+ persistence: "memory",
+ autocapture: false,
+ opt_out_capturing_by_default: false,
+ });
+}
+
+function usePostHogPageView() {
+ const pathname = usePathname();
+ const searchParams = useSearchParams();
+ const posthog = usePostHog();
+ const previousUrl = React.useRef<string | null>(null);
+ React.useEffect(() => {
+ // Track pageviews
+ if (pathname && posthog) {
+ let url = window.origin + pathname;
+ if (searchParams?.toString()) {
+ url = url + `?${searchParams.toString()}`;
+ }
+ if (previousUrl.current !== url) {
+ posthog.capture("$pageview", {
+ $current_url: url,
+ });
+ previousUrl.current = url;
+ }
+ }
+ }, [pathname, searchParams, posthog]);
+}
+
export function PostHogProvider(props: PostHogProviderProps) {
const { user } = useUser();
+ usePostHogPageView();
useMount(() => {
- // initalize posthog with our user id
- if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {
- posthog.init(process.env.NEXT_PUBLIC_POSTHOG_API_KEY, {
- api_host: process.env.NEXT_PUBLIC_POSTHOG_API_HOST,
- opt_out_capturing_by_default: false,
- capture_pageview: true,
- capture_pageleave: true,
- // we use session recordings to offer support and
- // improve the product for our pro users
- disable_session_recording: true,
- persistence: "memory",
- autocapture: false,
- opt_in_site_apps: true,
- bootstrap: {
- distinctID: user.id,
- },
+ if (user.email) {
+ posthog.identify(user.id);
+ posthog.people.set({
+ $email: user.email,
+ $name: user.name,
});
}
});
diff --git a/yarn.lock b/yarn.lock
index 36e8c18dd..d59a729f5 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -11182,10 +11182,10 @@ postgres-range@^1.1.1:
resolved "https://registry.yarnpkg.com/postgres-range/-/postgres-range-1.1.4.tgz#a59c5f9520909bcec5e63e8cf913a92e4c952863"
integrity sha512-i/hbxIE9803Alj/6ytL7UHQxRvZkI9O4Sy+J3HGc4F4oo/2eQAjTSNJ0bfxyse3bH0nuVesCk+3IRLaMtG3H6w==
-posthog-js@^1.147.0:
- version "1.147.0"
- resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.147.0.tgz#012b13a37044623d0d9bf1843ab3d37f916c8647"
- integrity sha512-cALKIcix1W9xMsfUUI/QUFhtBeabDGsQCb3lpZEFd7WCPNLOcGmpeomm4xMX7MO2OnfK6Ov91/HNyfK1wncjSA==
+posthog-js@^1.148.0:
+ version "1.148.0"
+ resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.148.0.tgz#c9a70dfc679d5fcb512c902b06044af72337f8eb"
+ integrity sha512-nydf+aret26YViDBwH7DdN/c8dmCzQGNQ1yredeE47UlbpJ3J/iTgskNdmGJIofFcdSagcwAE/FF7s++YfTtvg==
dependencies:
fflate "^0.4.8"
preact "^10.19.3"