@@ -5,6 +5,7 @@ import { PostHogProvider as PHProvider, usePostHog } from 'posthog-js/react';
55import { useSession } from 'next-auth/react' ;
66import { Suspense , useEffect , useRef } from 'react' ;
77import { usePathname , useSearchParams } from 'next/navigation' ;
8+ import { sanitizeAnalyticsUrl , sanitizeAnalyticsUrlValue } from '@/lib/sanitize-analytics-url' ;
89
910export function PostHogProvider ( { children } : { children : React . ReactNode } ) {
1011 useEffect ( ( ) => {
@@ -25,6 +26,18 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
2526 disable_web_experiments : false ,
2627 capture_pageview : false , // We capture pageviews manually
2728 capture_pageleave : true , // Enable pageleave capture
29+ before_send : event => {
30+ if ( ! event ?. properties ) return event ;
31+ return {
32+ ...event ,
33+ properties : {
34+ ...event . properties ,
35+ $current_url : sanitizeAnalyticsUrlValue ( event . properties . $current_url ) ,
36+ $referrer : sanitizeAnalyticsUrlValue ( event . properties . $referrer ) ,
37+ $referring_domain : sanitizeAnalyticsUrlValue ( event . properties . $referring_domain ) ,
38+ } ,
39+ } ;
40+ } ,
2841 loaded : function ( ph ) {
2942 if ( ! isProduction ) {
3043 // Opt out of capturing in non-production environments
@@ -69,11 +82,7 @@ function PostHogPageView() {
6982
7083 useEffect ( ( ) => {
7184 if ( pathname && posthog ) {
72- let url = window . origin + pathname ;
73- const search = searchParams . toString ( ) ;
74- if ( search ) {
75- url += '?' + search ;
76- }
85+ const url = sanitizeAnalyticsUrl ( window . origin , pathname , searchParams . toString ( ) ) ;
7786 posthog . capture ( '$pageview' , { $current_url : url } ) ;
7887 }
7988 } , [ pathname , searchParams , posthog ] ) ;
0 commit comments