Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion content/300-accelerate/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pagination_next: 'accelerate/getting-started'

import {
Bolt,
BorderBox,
BoxTitle,
Database,
Grid,
Expand Down
1 change: 0 additions & 1 deletion content/700-optimize/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pagination_next: 'optimize/getting-started'

import {
Bolt,
BorderBox,
BoxTitle,
Database,
Grid,
Expand Down
15 changes: 15 additions & 0 deletions src/hooks/useUTMParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useState } from 'react';

export const useUTMParams = (): string => {
const [utmParams, setUTMParams] = useState('');

useEffect(() => {
// Check if we're on the client side
if (typeof window !== 'undefined') {
const storedParams = sessionStorage.getItem('utm_params');
setUTMParams(storedParams || '');
}
}, []);

return utmParams;
};
44 changes: 42 additions & 2 deletions src/theme/NavbarItem/NavbarNavLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {isRegexpStringMatch} from '@docusaurus/theme-common';
import IconExternalLink from '@theme/Icon/ExternalLink';
import type {Props} from '@theme/NavbarItem/NavbarNavLink';
import { Icon } from '@site/src/components/Icon';
import { useUTMParams } from '@site/src/hooks/useUTMParams';


type CustomProps = Props & {
Expand All @@ -31,6 +32,28 @@ export default function NavbarNavLink({
const normalizedHref = useBaseUrl(href, {forcePrependBaseUrl: true});
const isExternalLink = label && href && !isInternalUrl(href);

// Get UTM parameters from sessionStorage using custom hook
const utmParams = useUTMParams();

// Helper function to append UTM params to URL
const appendUtmParams = (url: string): string => {
if (!utmParams) {
console.log('NavbarNavLink: No UTM params for URL:', url);
return url;
}

const [baseUrl, existingQuery] = url.split('?');
if (existingQuery) {
const result = `${baseUrl}?${existingQuery}&${utmParams}`;
console.log('NavbarNavLink: Adding UTM params to existing query:', url, '->', result);
return result;
} else {
const result = `${baseUrl}?${utmParams}`;
console.log('NavbarNavLink: Adding UTM params to URL:', url, '->', result);
return result;
}
};

// Link content is set through html XOR label
const linkContentProps = html
? {dangerouslySetInnerHTML: {__html: html}}
Expand All @@ -54,18 +77,35 @@ export default function NavbarNavLink({
};

if (href) {
// For external links, return as-is
if (isExternalLink) {
return (
<Link
href={prependBaseUrlToHref ? normalizedHref : href}
{...props}
{...linkContentProps}
/>
);
}

// For internal links, append UTM parameters if available
const finalHref = prependBaseUrlToHref ? normalizedHref : href;
const urlWithUtms = appendUtmParams(finalHref);

return (
<Link
href={prependBaseUrlToHref ? normalizedHref : href}
href={urlWithUtms}
{...props}
{...linkContentProps}
/>
);
}

const urlWithUtms = appendUtmParams(toUrl);

return (
<Link
to={toUrl}
to={urlWithUtms}
isNavLink
{...((activeBasePath || activeBaseRegex) && {
isActive: (_match, location) =>
Expand Down
Loading