#7091 EventTrackers send information of events to the TinyBird instance: In order to test: 1. Set ANALYTICS_ENABLED= true and TELEMETRY_ENABLED=true in evironment-variables.ts 2. Set the TINYBIRD_TOKEN in environment variables (go to TiniyBird Tokens) 3. Log in to twenty's TinyBird and go to datasources/analytics_events in twenty_analytics workspace 4. Run twenty and navigate it 5. New events will be logged in the datasources, containing their timestamp, sessionId and payload. <img width="1189" alt="Screenshot 2024-09-24 at 17 23 01" src="https://github.com/user-attachments/assets/85375897-504d-4e75-98e4-98e6a9671f98"> Example of payload when user is not logged in ``` {"hostName":"localhost", "pathname":"/welcome", "locale":"en-US", "userAgent":"Mozilla/5.0", "href":"http://localhost:3001/welcome", "referrer":"", "timeZone":"Europe/Barcelona"} ``` Example of payload when user is logged in ``` {"userId":"2020202", "workspaceId":"202", "workspaceDisplayName":"Apple", "workspaceDomainName":"apple.dev", "hostName":"localhost", "pathname":"/objects/companies", "locale":"en-US", "userAgent":"Mozilla/5.0Chrome/128.0.0.0Safari/537.36", "href":"http://localhost:3001/objects/companies", "referrer":"", "timeZone":"Europe/Paris"} ``` --------- Co-authored-by: Félix Malfait <felix@twenty.com>
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { useTrackMutation } from '~/generated/graphql';
|
|
export interface EventData {
|
|
pathname: string;
|
|
userAgent: string;
|
|
timeZone: string;
|
|
locale: string;
|
|
href: string;
|
|
referrer: string;
|
|
}
|
|
export const ANALYTICS_COOKIE_NAME = 'analyticsCookie';
|
|
export const getSessionId = (): string => {
|
|
const cookie: { [key: string]: string } = {};
|
|
document.cookie.split(';').forEach((el) => {
|
|
const [key, value] = el.split('=');
|
|
cookie[key.trim()] = value;
|
|
});
|
|
return cookie[ANALYTICS_COOKIE_NAME];
|
|
};
|
|
|
|
export const setSessionId = (domain?: string): void => {
|
|
const sessionId = getSessionId() || crypto.randomUUID();
|
|
const baseCookie = `${ANALYTICS_COOKIE_NAME}=${sessionId}; Max-Age=1800; path=/; secure`;
|
|
const cookie = domain ? baseCookie + `; domain=${domain}` : baseCookie;
|
|
|
|
document.cookie = cookie;
|
|
};
|
|
|
|
export const useEventTracker = () => {
|
|
const [createEventMutation] = useTrackMutation();
|
|
|
|
return useCallback(
|
|
(eventAction: string, eventPayload: EventData) => {
|
|
createEventMutation({
|
|
variables: {
|
|
action: eventAction,
|
|
payload: {
|
|
sessionId: getSessionId(),
|
|
...eventPayload,
|
|
},
|
|
},
|
|
});
|
|
},
|
|
[createEventMutation],
|
|
);
|
|
};
|