22
33import { WebNavigation } from "@prisma-docs/ui/components/web-navigation" ;
44import { Footer } from "@prisma-docs/ui/components/footer" ;
5- import { usePathname } from "next/navigation" ;
5+ import { useEffect , useState } from "react" ;
6+ import { usePathname , useSearchParams } from "next/navigation" ;
7+ import {
8+ getUtmParams ,
9+ readStoredUtmParams ,
10+ type UtmParams ,
11+ writeStoredUtmParams ,
12+ } from "@/lib/utm" ;
613
714interface Link {
815 text : string ;
@@ -23,7 +30,7 @@ interface Link {
2330interface NavigationWrapperProps {
2431 links : Link [ ] ;
2532 utm : {
26- source : "website" ;
33+ source : string ;
2734 } ;
2835}
2936
@@ -49,6 +56,27 @@ function getUtmMedium(pathname: string) {
4956
5057export function NavigationWrapper ( { links, utm } : NavigationWrapperProps ) {
5158 const pathname = usePathname ( ) ;
59+ const searchParams = useSearchParams ( ) ;
60+ const [ storedUtmParams , setStoredUtmParams ] = useState < UtmParams > ( {
61+ utm_source : utm . source ,
62+ } ) ;
63+
64+ useEffect ( ( ) => {
65+ const currentUtmParams = getUtmParams ( new URLSearchParams ( searchParams . toString ( ) ) ) ;
66+
67+ if ( currentUtmParams . utm_source ) {
68+ setStoredUtmParams ( currentUtmParams ) ;
69+ writeStoredUtmParams ( currentUtmParams ) ;
70+ return ;
71+ }
72+
73+ const persistedUtmParams = readStoredUtmParams ( ) ;
74+ setStoredUtmParams (
75+ persistedUtmParams . utm_source
76+ ? persistedUtmParams
77+ : { utm_source : utm . source } ,
78+ ) ;
79+ } , [ searchParams , utm . source ] ) ;
5280
5381 // Determine button variant based on pathname
5482 const getButtonVariant = ( ) : ColorType => {
@@ -62,7 +90,13 @@ export function NavigationWrapper({ links, utm }: NavigationWrapperProps) {
6290 return (
6391 < WebNavigation
6492 links = { links }
65- utm = { { source : utm . source , medium : getUtmMedium ( pathname ) } }
93+ utm = { {
94+ source : storedUtmParams . utm_source || utm . source ,
95+ medium : storedUtmParams . utm_medium || getUtmMedium ( pathname ) ,
96+ campaign : storedUtmParams . utm_campaign ,
97+ content : storedUtmParams . utm_content ,
98+ term : storedUtmParams . utm_term ,
99+ } }
66100 buttonVariant = { getButtonVariant ( ) }
67101 />
68102 ) ;
0 commit comments