Skip to content

Commit

Permalink
chore: op improvements (#1137)
Browse files Browse the repository at this point in the history
* fix: add user agent and location

* fix: api middleware event track

* chore: trpc metadata
  • Loading branch information
mxkaske authored Dec 27, 2024
1 parent fa07287 commit 21049b2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 101 deletions.
4 changes: 3 additions & 1 deletion apps/server/src/v1/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export function trackMiddleware(event: EventProps, eventProps?: string[]) {
await next();

// REMINDER: only track the event if the request was successful
if (!c.error) {
const isValid = c.res.status.toString().startsWith("2") && !c.error;

if (isValid) {
// We have checked the request to be valid already
let json: unknown;
if (c.req.raw.bodyUsed) {
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/components/forms/monitor/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export function MonitorForm({

const handleDataUpdateOrInsertion = async (props: InsertMonitor) => {
if (defaultValues) {
console.log(props);
await api.monitor.update.mutate(props);
} else {
await api.monitor.create.mutate(props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ import type { User } from "@openstatus/auth";
export function trackMiddleware(event: EventProps) {
return async (c: Context<{ Variables: { user?: User } }, "/*">, next: Next) => {
await next();

const isValid = c.res.status.toString().startsWith("2") && !c.error;

if (!c.error) {
if (isValid) {
setTimeout(async () => {
const analytics = await setupAnalytics({
profileId: c.get("user")?.id,
Expand Down
3 changes: 1 addition & 2 deletions packages/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"description": "",
"main": "src/index.ts",
"dependencies": {
"@jitsu/js": "1.9.2",
"@openpanel/sdk": "^1.0.0",
"@openpanel/sdk": "1.0.0",
"@t3-oss/env-core": "0.7.0",
"zod": "3.23.8"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/analytics/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ export type IdentifyProps = {
email?: string;
workspaceId?: string;
plan?: string;
// headers from the request
location?: string;
userAgent?: string;
};

export async function setupAnalytics(props: IdentifyProps) {
if (process.env.NODE_ENV !== "production") {
return noop();
}

if (props.location) {
op.api.addHeader("x-client-ip", props.location);
}

if (props.userAgent) {
op.api.addHeader("user-agent", props.userAgent);
}

if (props.userId) {
const [firstName, lastName] = props.fullName?.split(" ") || [];
await op.identify({
Expand Down
19 changes: 17 additions & 2 deletions packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type CreateContextOptions = {
workspace?: Workspace | null;
user?: User | null;
req?: NextRequest;
metadata?: {
userAgent?: string;
location?: string;
};
};

type Meta = {
Expand Down Expand Up @@ -73,6 +77,13 @@ export const createTRPCContext = async (opts: {
workspace,
user,
req: opts.req,
metadata: {
userAgent: opts.req.headers.get("user-agent") ?? undefined,
location:
opts.req.headers.get("x-forwarded-for") ??
process.env.VERCEL_REGION ??
undefined,
},
});
};

Expand Down Expand Up @@ -202,9 +213,13 @@ const enforceUserIsAuthed = t.middleware(async (opts) => {
// REMINDER: We only track the event if the request was successful
// REMINDER: We are not blocking the request
after(async () => {
const { meta, getRawInput } = opts;
const { ctx, meta, getRawInput } = opts;

if (meta?.track) {
let identify: IdentifyProps = {};
let identify: IdentifyProps = {
userAgent: ctx.metadata?.userAgent,
location: ctx.metadata?.location,
};

if (user && workspace) {
identify = {
Expand Down
95 changes: 1 addition & 94 deletions pnpm-lock.yaml

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

0 comments on commit 21049b2

Please sign in to comment.