Skip to content

Commit

Permalink
feat: ✨ create posthog provider
Browse files Browse the repository at this point in the history
  • Loading branch information
neopromic committed Nov 22, 2024
1 parent 1d0f93e commit a4c8c5f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/_lib/providers/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use client";

import posthog from "posthog-js";
import { PostHogProvider as Provider } from "posthog-js/react";
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import { env } from "../env";
if (typeof window !== "undefined") {
posthog.init(env.NEXT_PUBLIC_POSTHOG_KEY, {
api_host: env.NEXT_PUBLIC_POSTHOG_HOST,
capture_pageview: true,
person_profiles: "always",
autocapture: true,
capture_heatmaps: true,
});
}

export function PostHogProvider({ children }: { children: React.ReactNode }) {
const pathname = usePathname();
const searchParams = useSearchParams();

useEffect(() => {
if (pathname) {
let url = window.origin + pathname;
if (searchParams?.toString()) {
url = url + `?${searchParams.toString()}`;
}
posthog.capture("$pageview", {
$current_url: url,
});
}
}, [pathname, searchParams]);

return <Provider client={posthog}>{children}</Provider>;
}

0 comments on commit a4c8c5f

Please sign in to comment.