Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions src/common/Track.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Helmet } from 'react-helmet';
import { useLocation } from 'react-router-dom';
import { useGetUsersMeQuery } from 'src/features/api';
import { useActiveWorkspace } from 'src/hooks/useActiveWorkspace';
import { useUtmParams } from 'src/hooks/useUtmParams';
import { useAnalytics } from 'use-analytics';

export const Track = ({
Expand All @@ -19,7 +20,7 @@ export const Track = ({
const { data: userData, isLoading, isSuccess } = useGetUsersMeQuery();
const { activeWorkspace } = useActiveWorkspace();
const { track, identify, page } = useAnalytics();
const utmSource = sessionStorage.getItem('utmSource');
const utmParams = useUtmParams();
const location = useLocation();

const defaultMeta = [
Expand All @@ -39,7 +40,7 @@ export const Track = ({
];

useEffect(() => {
page();
page(utmParams);
}, [location]);

const helmet = () => (
Expand All @@ -64,7 +65,7 @@ export const Track = ({
email: userData.email,
company: activeWorkspace.company,
workspace: activeWorkspace,
...((utmSource && { utm_source: utmSource }) || {}),
...utmParams,
});

track(
Expand Down
42 changes: 42 additions & 0 deletions src/hooks/useUtmParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useSearchParams } from 'react-router-dom';

const UTM_KEYS = [
'utm_source',
'utm_medium',
'utm_campaign',
'utm_content',
'utm_term',
'platform',
] as const;

const STORAGE_KEY = 'utmParams';

type UtmParams = Partial<Record<(typeof UTM_KEYS)[number], string>>;

function getStoredUtmParams(): UtmParams {
try {
const stored = sessionStorage.getItem(STORAGE_KEY);
return stored ? JSON.parse(stored) : {};
} catch {
return {};
}
}

export function useUtmParams(): UtmParams {
const [searchParams] = useSearchParams();

const urlParams: UtmParams = {};
UTM_KEYS.forEach((key) => {
const value = searchParams.get(key);
if (value) {
urlParams[key] = value;

Check warning on line 32 in src/hooks/useUtmParams.ts

View workflow job for this annotation

GitHub Actions / validate

Generic Object Injection Sink
}
});

if (Object.keys(urlParams).length > 0) {
sessionStorage.setItem(STORAGE_KEY, JSON.stringify(urlParams));
return urlParams;
}

return getStoredUtmParams();
}
5 changes: 0 additions & 5 deletions src/pages/JoinPage/useJoinSubmit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function useJoinSubmit(isInvited: boolean) {
const sendGTMevent = useSendGTMevent({ loggedUser: false });

const templateParam = searchParams.get('template');
const utmSource = searchParams.get('utm_source');
let templateId: number | undefined;

if (templateParam !== null) {
Expand All @@ -30,10 +29,6 @@ export function useJoinSubmit(isInvited: boolean) {
templateId = parsed;
}

if (utmSource !== null) {
sessionStorage.setItem('utmSource', utmSource);
}

const onSubmit = useCallback(
async (
values: JoinFormValues,
Expand Down
Loading