-
Notifications
You must be signed in to change notification settings - Fork 865
fix: DR-6518 UTM persistence #7384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
carlagn
wants to merge
4
commits into
main
Choose a base branch
from
fix/DR-6518-docs-utm-persistence
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−8
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ pagination_next: 'accelerate/getting-started' | |
|
|
||
| import { | ||
| Bolt, | ||
| BorderBox, | ||
| BoxTitle, | ||
| Database, | ||
| Grid, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ pagination_next: 'optimize/getting-started' | |
|
|
||
| import { | ||
| Bolt, | ||
| BorderBox, | ||
| BoxTitle, | ||
| Database, | ||
| Grid, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { useState, useEffect } from 'react'; | ||
|
|
||
| export const useUTMParams = (): string => { | ||
| const [utmParams, setUTMParams] = useState(''); | ||
|
|
||
| useEffect(() => { | ||
| // Check if we're on the client side | ||
| if (typeof window !== 'undefined') { | ||
| const updateUTMParams = () => { | ||
| const storedParams = sessionStorage.getItem('utm_params'); | ||
| setUTMParams(storedParams || ''); | ||
| }; | ||
|
|
||
| // Initial load | ||
| updateUTMParams(); | ||
|
|
||
| // Listen for storage changes (in case UTM params are set after initial load) | ||
| const handleStorageChange = (e: StorageEvent) => { | ||
| if (e.key === 'utm_params') { | ||
| updateUTMParams(); | ||
| } | ||
| }; | ||
|
|
||
| window.addEventListener('storage', handleStorageChange); | ||
|
|
||
| // Also check periodically in case the storage was updated in the same tab | ||
| const interval = setInterval(updateUTMParams, 100); | ||
|
|
||
| // Clean up after a short time to avoid infinite checking | ||
| setTimeout(() => { | ||
| clearInterval(interval); | ||
| }, 2000); | ||
|
|
||
| return () => { | ||
| window.removeEventListener('storage', handleStorageChange); | ||
| clearInterval(interval); | ||
| }; | ||
| } | ||
| }, []); | ||
|
|
||
| return utmParams; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import React, {type ReactNode} from 'react'; | ||
| import Link from '@docusaurus/Link'; | ||
| import useBaseUrl from '@docusaurus/useBaseUrl'; | ||
| import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
| import {useThemeConfig, type NavbarLogo} from '@docusaurus/theme-common'; | ||
| import ThemedImage from '@theme/ThemedImage'; | ||
| import type {Props} from '@theme/Logo'; | ||
| import { useUTMParams } from '@site/src/hooks/useUTMParams'; | ||
|
|
||
| function LogoThemedImage({ | ||
| logo, | ||
| alt, | ||
| imageClassName, | ||
| }: { | ||
| logo: NavbarLogo; | ||
| alt: string; | ||
| imageClassName?: string; | ||
| }) { | ||
| const sources = { | ||
| light: useBaseUrl(logo.src), | ||
| dark: useBaseUrl(logo.srcDark || logo.src), | ||
| }; | ||
| const themedImage = ( | ||
| <ThemedImage | ||
| className={logo.className} | ||
| sources={sources} | ||
| height={logo.height} | ||
| width={logo.width} | ||
| alt={alt} | ||
| style={logo.style} | ||
| /> | ||
| ); | ||
|
|
||
| // Is this extra div really necessary? | ||
| // introduced in https://github.com/facebook/docusaurus/pull/5666 | ||
| return imageClassName ? ( | ||
| <div className={imageClassName}>{themedImage}</div> | ||
| ) : ( | ||
| themedImage | ||
| ); | ||
| } | ||
|
|
||
| export default function Logo(props: Props): ReactNode { | ||
| const { | ||
| siteConfig: {title}, | ||
| } = useDocusaurusContext(); | ||
| const { | ||
| navbar: {title: navbarTitle, logo}, | ||
| } = useThemeConfig(); | ||
|
|
||
| const {imageClassName, titleClassName, ...propsRest} = props; | ||
| const logoLink = useBaseUrl(logo?.href || '/'); | ||
| const utmParams = useUTMParams(); | ||
|
|
||
| // Helper function to append UTM params to URL | ||
| const appendUtmParams = (url: string): string => { | ||
| if (!utmParams) { | ||
| return url; | ||
| } | ||
| const separator = url.includes('?') ? '&' : '?'; | ||
| const result = `${url}${separator}${utmParams}`; | ||
| return result; | ||
| }; | ||
|
|
||
| // If visible title is shown, fallback alt text should be | ||
| // an empty string to mark the logo as decorative. | ||
| const fallbackAlt = navbarTitle ? '' : title; | ||
|
|
||
| // Use logo alt text if provided (including empty string), | ||
| // and provide a sensible fallback otherwise. | ||
| const alt = logo?.alt ?? fallbackAlt; | ||
|
|
||
| return ( | ||
| <Link | ||
| to={appendUtmParams(logoLink)} | ||
| {...propsRest} | ||
| {...(logo?.target && {target: logo.target})}> | ||
| {logo && ( | ||
| <LogoThemedImage | ||
| logo={logo} | ||
| alt={alt} | ||
| imageClassName={imageClassName} | ||
| /> | ||
| )} | ||
| {navbarTitle != null && <b className={titleClassName}>{navbarTitle}</b>} | ||
| </Link> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import React, { type ReactNode } from 'react'; | |
| import styles from './styles.module.css'; | ||
| import Link from '@docusaurus/Link'; | ||
| import useBaseUrl from '@docusaurus/useBaseUrl'; | ||
| import { useUTMParams } from '@site/src/hooks/useUTMParams'; | ||
|
|
||
| function useNavbarItems() { | ||
| // TODO temporary casting until ThemeConfig type is improved | ||
|
|
@@ -58,12 +59,20 @@ function NavbarContentLayout({ | |
|
|
||
| export default function NavbarContent(): ReactNode { | ||
| const mobileSidebar = useNavbarMobileSidebar(); | ||
| const utmParams = useUTMParams(); | ||
|
|
||
| const items = useNavbarItems(); | ||
| const [leftItems, rightItems] = splitNavbarItems(items); | ||
|
|
||
| const searchBarItem = items.find((item) => item.type === 'search'); | ||
| const baseUrl = useBaseUrl("/"); | ||
|
|
||
| // Helper function to append UTM params to URL | ||
| const appendUtmParams = (url: string): string => { | ||
| if (!utmParams) return url; | ||
| const separator = url.includes('?') ? '&' : '?'; | ||
| return `${url}${separator}${utmParams}`; | ||
| }; | ||
|
Comment on lines
+69
to
+75
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment as above for comments |
||
|
|
||
| return ( | ||
| <NavbarContentLayout | ||
|
|
@@ -73,7 +82,7 @@ export default function NavbarContent(): ReactNode { | |
| {!mobileSidebar.disabled && <NavbarMobileSidebarToggle />} | ||
| <NavbarLogo /> | ||
| <span className={styles.separator}>/</span> | ||
| <Link to={baseUrl} className="logo-link">docs</Link> | ||
| <Link to={appendUtmParams(baseUrl)} className="logo-link">docs</Link> | ||
| </> | ||
| } | ||
| middle={ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 & { | ||
|
|
@@ -31,6 +32,25 @@ 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 => { | ||
|
Comment on lines
+35
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment as above |
||
| if (!utmParams) { | ||
| return url; | ||
| } | ||
|
|
||
| const [baseUrl, existingQuery] = url.split('?'); | ||
| if (existingQuery) { | ||
| const result = `${baseUrl}?${existingQuery}&${utmParams}`; | ||
| return result; | ||
| } else { | ||
| const result = `${baseUrl}?${utmParams}`; | ||
| return result; | ||
| } | ||
| }; | ||
|
|
||
| // Link content is set through html XOR label | ||
| const linkContentProps = html | ||
| ? {dangerouslySetInnerHTML: {__html: html}} | ||
|
|
@@ -54,18 +74,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) => | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO
appendUtmParamsis enough to understand, I think you can drop the "Helper function to append UTM params to URL" comment.More generally, do we tend to keep comments in the codebase ? (still fresh in the team so just asking for context)