add tracking code workflow for Add website. Add linked ID to session modal.

This commit is contained in:
Francis Cao committed 2026-07-16 11:32:32 -07:00
1 parent 7afc4ccf6b
commit f7b94c259b
58 files changed
+163 -19

No files matched your search

+60 -6
View File
@@ -1,23 +1,77 @@
import { useToast } from '@umami/react-zen';
import { useMessages, useModified } from '@/components/hooks';
import { Button, Column, Row, useToast } from '@umami/react-zen';
import { useState } from 'react';
import { IconLabel } from '@/components/common/IconLabel';
import { useConfig, useMessages, useModified } from '@/components/hooks';
import { Plus } from '@/components/icons';
import { DialogButton } from '@/components/input/DialogButton';
import { WebsiteTrackingCode } from './[websiteId]/settings/WebsiteTrackingCode';
import { WebsiteAddForm } from './WebsiteAddForm';
export function WebsiteAddButton({ teamId, onSave }: { teamId: string; onSave?: () => void }) {
const { t, labels, messages } = useMessages();
const { toast } = useToast();
const { touch } = useModified();
const { cloudMode } = useConfig();
const [isOpen, setIsOpen] = useState(false);
const [createdWebsite, setCreatedWebsite] = useState<{ id: string } | null>(null);
const handleSave = async () => {
const handleSave = async (website: { id: string }) => {
toast(t(messages.saved));
touch('websites');
onSave?.();
if (cloudMode) {
setCreatedWebsite(website);
} else {
setIsOpen(false);
}
};
const handleOpenChange = (open: boolean) => {
setIsOpen(open);
if (!open) {
setCreatedWebsite(null);
}
};
return (
<DialogButton icon={<Plus />} label={t(labels.addWebsite)} variant="primary" width="400px">
{({ close }) => <WebsiteAddForm teamId={teamId} onSave={handleSave} onClose={close} />}
</DialogButton>
<>
<Button onPress={() => handleOpenChange(true)} variant="primary">
<IconLabel icon={<Plus />} label={t(labels.addWebsite)} />
</Button>
<DialogButton
title={createdWebsite ? t(labels.trackingCode) : t(labels.addWebsite)}
width={createdWebsite ? '720px' : '400px'}
isOpen={isOpen}
onOpenChange={handleOpenChange}
>
{({ close }) =>
createdWebsite ? (
<TrackingCodeStep websiteId={createdWebsite.id} onClose={() => handleOpenChange(false)} />
) : (
<WebsiteAddForm
teamId={teamId}
onSave={handleSave}
onClose={close}
closeOnSave={!cloudMode}
/>
)
}
</DialogButton>
</>
);
}
function TrackingCodeStep({ websiteId, onClose }: { websiteId: string; onClose: () => void }) {
const { t, labels } = useMessages();
return (
<Column gap="4">
<WebsiteTrackingCode websiteId={websiteId} showHeader={false} />
<Row justifyContent="flex-end" paddingTop="2">
<Button onPress={onClose}>{t(labels.close)}</Button>
</Row>
</Column>
);
}
+10 -7
View File
@@ -6,21 +6,24 @@ export function WebsiteAddForm({
teamId,
onSave,
onClose,
closeOnSave = true,
}: {
teamId?: string;
onSave?: () => void;
onSave?: (website: any) => void | Promise<void>;
onClose?: () => void;
closeOnSave?: boolean;
}) {
const { t, labels, messages } = useMessages();
const { mutateAsync, error, isPending } = useUpdateQuery('/websites', { teamId });
const handleSubmit = async (data: any) => {
await mutateAsync(data, {
onSuccess: async () => {
onSave?.();
onClose?.();
},
});
const website = await mutateAsync(data);
await onSave?.(website);
if (closeOnSave) {
onClose?.();
}
};
return (
@@ -4,19 +4,20 @@ import { DateDistance } from '@/components/common/DateDistance';
import { TypeIcon } from '@/components/common/TypeIcon';
import { useFormat, useLocale, useMessages, useRegionNames } from '@/components/hooks';
import { Calendar, KeyRound, Landmark, MapPin } from '@/components/icons';
import { Network } from '@/components/svg';
export function SessionInfo({ data }) {
const { locale } = useLocale();
const { t, labels } = useMessages();
const { formatValue } = useFormat();
const { getRegionName } = useRegionNames(locale);
const distinctId = data?.distinctId?.trim();
const stitchedSessionCount = data?.stitchedSessionCount;
return (
<Grid columns="repeat(auto-fit, minmax(200px, 1fr)" gap>
<Info label={t(labels.distinctId)} icon={<KeyRound />}>
<span style={{ overflowWrap: 'anywhere' }}>
{data?.distinctId}
</span>
{distinctId ? <span style={{ overflowWrap: 'anywhere' }}>{distinctId}</span> : '—'}
</Info>
<Info label={t(labels.lastSeen)} icon={<Calendar />}>
@@ -53,6 +54,12 @@ export function SessionInfo({ data }) {
<Info label={t(labels.device)} icon={<TypeIcon type="device" value={data?.device} />}>
{formatValue(data?.device, 'device')}
</Info>
{distinctId && stitchedSessionCount > 1 && (
<Info label="Linked IDs" icon={<Network />}>
{stitchedSessionCount}
</Info>
)}
</Grid>
);
}
@@ -6,9 +6,11 @@ const SCRIPT_NAME = 'script.js';
export function WebsiteTrackingCode({
websiteId,
hostUrl,
showHeader = true,
}: {
websiteId: string;
hostUrl?: string;
showHeader?: boolean;
}) {
const { t, messages, labels } = useMessages();
const config = useConfig();
@@ -32,7 +34,7 @@ export function WebsiteTrackingCode({
return (
<Column gap>
<Label>{t(labels.trackingCode)}</Label>
{showHeader && <Label>{t(labels.trackingCode)}</Label>}
<Text color="muted">{t(messages.trackingCode)}</Text>
<TextField
value={code}
@@ -1,7 +1,8 @@
import { uuid } from '@/lib/crypto';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { canViewWebsiteSection } from '@/permissions';
import { getWebsiteSession } from '@/queries/sql';
import { getLinkedDistinctIds, getLinkedSessionIds, getWebsiteSession } from '@/queries/sql';
export async function GET(
request: Request,
@@ -28,5 +29,29 @@ export async function GET(
const data = await getWebsiteSession(websiteId, sessionId);
return json(data);
let stitchedSessionCount = 0;
if (data) {
let sessionIds = [sessionId];
const distinctIds = data.distinctId
? [data.distinctId]
: await getLinkedDistinctIds(websiteId, sessionId);
if (distinctIds.length) {
const links = await Promise.all(
distinctIds.map(distinctId => getLinkedSessionIds(websiteId, distinctId)),
);
const linkedIds = links.flatMap(group => group.map(link => link.sessionId));
const identifiedIds = distinctIds.map(distinctId => uuid(websiteId, distinctId));
sessionIds = Array.from(new Set([sessionId, ...identifiedIds, ...linkedIds]));
}
stitchedSessionCount = sessionIds.length;
}
return json({
...data,
stitchedSessionCount,
});
}
+1
View File
@@ -4,6 +4,7 @@ export const labels: Record<string, string> = {
required: 'label.required',
save: 'label.save',
cancel: 'label.cancel',
close: 'label.close',
continue: 'label.continue',
delete: 'label.delete',
leave: 'label.leave',