Skip to content

Commit

Permalink
feat: add cookies to the log (#3528)
Browse files Browse the repository at this point in the history
Add whitelisted cookies to log events
  • Loading branch information
idoshamun authored Sep 11, 2024
1 parent 0e33a00 commit 0f2c91e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/shared/src/hooks/log/useLogQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface LogEvent extends Record<string, unknown> {
event_name: string;
extra?: string;
device_id?: string;
cookies?: string;
}

export type PushToQueueFunc = (events: LogEvent[]) => void;
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/hooks/log/useLogSharedProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { useRouter } from 'next/router';
import { LogEvent } from './useLogQueue';
import SettingsContext from '../../contexts/SettingsContext';
import AuthContext from '../../contexts/AuthContext';
import { getCookies } from '../../lib/cookie';

const COOKIES = ['_ga', '_fbp', '_fbc'];

export default function useLogSharedProps(
app: string,
Expand Down Expand Up @@ -51,6 +54,7 @@ export default function useLogSharedProps(
});

const queryStr = JSON.stringify(queryObject);
const cookies = JSON.stringify(getCookies(COOKIES));

(sharedPropsRef.current?.device_id
? Promise.resolve(sharedPropsRef.current.device_id)
Expand All @@ -76,6 +80,7 @@ export default function useLogSharedProps(
utm_term: query?.utm_term,
visit_id: visitId,
device_id: _deviceId,
cookies: cookies === '{}' ? undefined : cookies,
};
setSharedPropsSet(true);
});
Expand Down
21 changes: 21 additions & 0 deletions packages/shared/src/lib/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ export type CookieOptions = {
secure: boolean;
};

export const getCookies = (
names: string[],
): Record<string, string> | undefined => {
const cookies =
document?.cookie?.split(';')?.map((cookie) => cookie.trim()) || [];
if (!cookies.length) {
return undefined;
}

return names.reduce((acc, name) => {
const foundCookie = cookies.find((cookie) => cookie.startsWith(`${name}=`));
if (!foundCookie) {
return acc;
}
return {
...acc,
[name]: decodeURIComponent(foundCookie.split('=')[1]),
};
}, {});
};

export const setCookie = (
name: string,
value: string | number | boolean,
Expand Down

0 comments on commit 0f2c91e

Please sign in to comment.