@@ -6,6 +6,7 @@ import {isRegexpStringMatch} from '@docusaurus/theme-common';
66import IconExternalLink from '@theme/Icon/ExternalLink' ;
77import type { Props } from '@theme/NavbarItem/NavbarNavLink' ;
88import { Icon } from '@site/src/components/Icon' ;
9+ import { useUTMParams } from '@site/src/hooks/useUTMParams' ;
910
1011
1112type CustomProps = Props & {
@@ -31,6 +32,28 @@ export default function NavbarNavLink({
3132 const normalizedHref = useBaseUrl ( href , { forcePrependBaseUrl : true } ) ;
3233 const isExternalLink = label && href && ! isInternalUrl ( href ) ;
3334
35+ // Get UTM parameters from sessionStorage using custom hook
36+ const utmParams = useUTMParams ( ) ;
37+
38+ // Helper function to append UTM params to URL
39+ const appendUtmParams = ( url : string ) : string => {
40+ if ( ! utmParams ) {
41+ console . log ( 'NavbarNavLink: No UTM params for URL:' , url ) ;
42+ return url ;
43+ }
44+
45+ const [ baseUrl , existingQuery ] = url . split ( '?' ) ;
46+ if ( existingQuery ) {
47+ const result = `${ baseUrl } ?${ existingQuery } &${ utmParams } ` ;
48+ console . log ( 'NavbarNavLink: Adding UTM params to existing query:' , url , '->' , result ) ;
49+ return result ;
50+ } else {
51+ const result = `${ baseUrl } ?${ utmParams } ` ;
52+ console . log ( 'NavbarNavLink: Adding UTM params to URL:' , url , '->' , result ) ;
53+ return result ;
54+ }
55+ } ;
56+
3457 // Link content is set through html XOR label
3558 const linkContentProps = html
3659 ? { dangerouslySetInnerHTML : { __html : html } }
@@ -54,18 +77,35 @@ export default function NavbarNavLink({
5477 } ;
5578
5679 if ( href ) {
80+ // For external links, return as-is
81+ if ( isExternalLink ) {
82+ return (
83+ < Link
84+ href = { prependBaseUrlToHref ? normalizedHref : href }
85+ { ...props }
86+ { ...linkContentProps }
87+ />
88+ ) ;
89+ }
90+
91+ // For internal links, append UTM parameters if available
92+ const finalHref = prependBaseUrlToHref ? normalizedHref : href ;
93+ const urlWithUtms = appendUtmParams ( finalHref ) ;
94+
5795 return (
5896 < Link
59- href = { prependBaseUrlToHref ? normalizedHref : href }
97+ href = { urlWithUtms }
6098 { ...props }
6199 { ...linkContentProps }
62100 />
63101 ) ;
64102 }
65103
104+ const urlWithUtms = appendUtmParams ( toUrl ) ;
105+
66106 return (
67107 < Link
68- to = { toUrl }
108+ to = { urlWithUtms }
69109 isNavLink
70110 { ...( ( activeBasePath || activeBaseRegex ) && {
71111 isActive : ( _match , location ) =>
0 commit comments