sdk improvement and get profile with events
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { parseIp } from '@/utils/parseIp';
|
||||
import { parseReferrer } from '@/utils/parseReferrer';
|
||||
import { getReferrerWithQuery, parseReferrer } from '@/utils/parseReferrer';
|
||||
import { parseUserAgent } from '@/utils/parseUserAgent';
|
||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { omit } from 'ramda';
|
||||
@@ -15,16 +15,18 @@ import type { PostEventPayload } from '@mixan/types';
|
||||
const SESSION_TIMEOUT = 1000 * 60 * 30;
|
||||
const SESSION_END_TIMEOUT = SESSION_TIMEOUT + 1000;
|
||||
|
||||
function parseSearchParams(params: URLSearchParams): Record<string, string> {
|
||||
function parseSearchParams(
|
||||
params: URLSearchParams
|
||||
): Record<string, string> | undefined {
|
||||
const result: Record<string, string> = {};
|
||||
for (const [key, value] of params.entries()) {
|
||||
result[key] = value;
|
||||
}
|
||||
return result;
|
||||
return Object.keys(result).length ? result : undefined;
|
||||
}
|
||||
|
||||
function parsePath(path?: string): {
|
||||
query?: Record<string, unknown>;
|
||||
query?: Record<string, string>;
|
||||
path: string;
|
||||
hash?: string;
|
||||
} {
|
||||
@@ -39,7 +41,7 @@ function parsePath(path?: string): {
|
||||
return {
|
||||
query: parseSearchParams(url.searchParams),
|
||||
path: url.pathname,
|
||||
hash: url.hash,
|
||||
hash: url.hash ?? undefined,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
@@ -57,8 +59,10 @@ export async function postEvent(
|
||||
let profileId: string | null = null;
|
||||
const projectId = request.projectId;
|
||||
const body = request.body;
|
||||
const createdAt = new Date(body.timestamp);
|
||||
const { path, hash, query } = parsePath(body.properties?.path);
|
||||
const referrer = parseReferrer(body.properties?.referrer);
|
||||
const utmReferrer = getReferrerWithQuery(query);
|
||||
const ip = getClientIp(request)!;
|
||||
const origin = request.headers.origin!;
|
||||
const ua = request.headers['user-agent']!;
|
||||
@@ -132,7 +136,7 @@ export async function postEvent(
|
||||
hash,
|
||||
query,
|
||||
}),
|
||||
createdAt: body.timestamp,
|
||||
createdAt,
|
||||
country: geo.country,
|
||||
city: geo.city,
|
||||
region: geo.region,
|
||||
@@ -147,8 +151,8 @@ export async function postEvent(
|
||||
duration: 0,
|
||||
path: path,
|
||||
referrer: referrer.url,
|
||||
referrerName: referrer.name,
|
||||
referrerType: referrer.type,
|
||||
referrerName: referrer.name ?? utmReferrer?.name ?? '',
|
||||
referrerType: referrer.type ?? utmReferrer?.type ?? '',
|
||||
};
|
||||
|
||||
const job = findJobByPrefix(eventsJobs, `event:${projectId}:${profileId}:`);
|
||||
@@ -180,6 +184,7 @@ export async function postEvent(
|
||||
payload: {
|
||||
...payload,
|
||||
name: 'session_start',
|
||||
// @ts-expect-error
|
||||
createdAt: toISOString(getTime(payload.createdAt) - 10),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -24,3 +24,27 @@ export function parseReferrer(url: string | undefined) {
|
||||
url: url ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
export function getReferrerWithQuery(
|
||||
query: Record<string, string> | undefined
|
||||
) {
|
||||
if (!query) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const source = query.utm_source ?? query.ref ?? query.utm_referrer ?? '';
|
||||
|
||||
const match = Object.values(referrers).find(
|
||||
(referrer) => referrer.name.toLowerCase() === source?.toLowerCase()
|
||||
);
|
||||
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
name: match.name,
|
||||
type: match.type,
|
||||
url: '',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ export function EventListItem({
|
||||
createdAt,
|
||||
name,
|
||||
properties,
|
||||
path,
|
||||
}: EventListItemProps) {
|
||||
const params = useAppParams();
|
||||
|
||||
@@ -46,16 +47,15 @@ export function EventListItem({
|
||||
|
||||
switch (name) {
|
||||
case 'screen_view': {
|
||||
const route = (properties?.route || properties?.path)!;
|
||||
if (route) {
|
||||
bullets.push(route);
|
||||
if (path) {
|
||||
bullets.push(path);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return bullets;
|
||||
}, [name, createdAt, profile, properties, params]);
|
||||
}, [name, createdAt, profile, properties, params, path]);
|
||||
|
||||
return (
|
||||
<ExpandableListItem
|
||||
|
||||
@@ -12,6 +12,12 @@ const handler = (req: Request) =>
|
||||
session: auth(),
|
||||
};
|
||||
},
|
||||
onError(opts) {
|
||||
const { error, type, path, input, ctx, req } = opts;
|
||||
console.error('---- TRPC ERROR');
|
||||
console.error('Error:', error);
|
||||
console.error();
|
||||
},
|
||||
});
|
||||
|
||||
export { handler as GET, handler as POST };
|
||||
|
||||
@@ -27,8 +27,8 @@ export function ExpandableListItem({
|
||||
<div className="flex flex-col flex-1 gap-1 min-w-0">
|
||||
<span className="text-lg font-medium leading-none mb-1">{title}</span>
|
||||
<div className="flex flex-col sm:flex-row sm:items-center gap-1 sm:gap-4 text-sm text-muted-foreground">
|
||||
{bullets.map((bullet) => (
|
||||
<span key={bullet}>{bullet}</span>
|
||||
{bullets.map((bullet, index) => (
|
||||
<span key={index}>{bullet}</span>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -3,7 +3,7 @@ import { transformEvent } from '@/server/services/event.service';
|
||||
import { z } from 'zod';
|
||||
|
||||
import type { IDBEvent } from '@mixan/db';
|
||||
import { chQuery, createSqlBuilder } from '@mixan/db';
|
||||
import { chQuery, createSqlBuilder, getEvents } from '@mixan/db';
|
||||
|
||||
export const eventRouter = createTRPCRouter({
|
||||
list: protectedProcedure
|
||||
@@ -31,6 +31,8 @@ export const eventRouter = createTRPCRouter({
|
||||
|
||||
sb.orderBy.created_at = 'created_at DESC';
|
||||
|
||||
return (await chQuery<IDBEvent>(getSql())).map(transformEvent);
|
||||
const res = await getEvents(getSql(), { profile: true });
|
||||
|
||||
return res;
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user