From b074a55ecd04fc1bd8db14c69f9f0466c536b20e Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Sun, 3 Mar 2024 12:31:57 -0500 Subject: [PATCH] change user modify auth table to more understandable event names (#12) --- supabase/functions/on_user_modify/index.ts | 23 +++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/supabase/functions/on_user_modify/index.ts b/supabase/functions/on_user_modify/index.ts index f65de5d..983e5ad 100644 --- a/supabase/functions/on_user_modify/index.ts +++ b/supabase/functions/on_user_modify/index.ts @@ -4,6 +4,7 @@ Deno.serve(async (req) => { let { record, old_record, type } = await req.json(); record = record || old_record; + let event: string | undefined; // deno-lint-ignore no-explicit-any const properties: Record = { type }; properties["$set"] = properties["$set"] || {}; @@ -11,11 +12,23 @@ Deno.serve(async (req) => { properties["$set"].created_at = record.created_at; properties["$set"].last_sign_in_at = record.last_sign_in_at; - posthog.capture({ - distinctId: record.id, - event: "user modify auth table", - properties: properties, - }); + if (type === "INSERT") { + event = "user signs up"; + } else if ( + type === "UPDATE" && record.last_sign_in_at !== old_record.last_sign_in_at + ) { + event = "user signs in"; + } else if (type === "DELETE") { + event = "user deletes account"; + } + + if (event) { + posthog.capture({ + distinctId: record.id, + event, + properties: properties, + }); + } return new Response(null); });