diff --git a/src/App.tsx b/src/App.tsx index 90358e0..3eae1bd 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -5,6 +5,8 @@ import { FilesProvider } from './contexts/Files'; import DownloadView from './views/DownloadView'; import HomeView from './views/HomeView'; import NotFoundView from './views/NotFoundView'; +import { useEffect } from 'react'; +import { cleanUrlParam } from './utils/cleanUrl'; function DownloadRedirectWrapper() { const { sendId } = useParams(); @@ -15,6 +17,10 @@ function DownloadRedirectWrapper() { } function App() { + useEffect(() => { + cleanUrlParam(); + }, []); + return ( <> diff --git a/src/utils/cleanUrl.ts b/src/utils/cleanUrl.ts new file mode 100644 index 0000000..a547a9d --- /dev/null +++ b/src/utils/cleanUrl.ts @@ -0,0 +1,26 @@ +/** + * Remove Google Analytics/Tag Manager tracking query parameters + * to keep the URL looking clean for the users after page has loaded + */ + +export function cleanUrlParam() { + const url = new URL(window.location.href); + let hadChanges = false; + + const currentKeys = Array.from(url.searchParams.keys()); + + currentKeys.forEach((param) => { + if (param === '_gl' || param === '_gcl_au' || param === '_fplc' || param.startsWith('_ga')) { + url.searchParams.delete(param); + hadChanges = true; + } + }); + + if (hadChanges) { + const newUrl = url.searchParams.toString() + ? `${url.pathname}?${url.searchParams.toString()}${url.hash}` + : `${url.pathname}${url.hash}`; + + window.history.replaceState({}, document.title, newUrl); + } +}