-
Notifications
You must be signed in to change notification settings - Fork 365
Add new migration for signup tracking #3569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
25caf90
Add new migration for signup tracking
colegottdank 214f9c9
Log when onboarded
colegottdank 0aee96d
Add posthog client that checks if enabled
colegottdank 5bb1785
has onboarded event
colegottdank 0177b0c
Fix migration
colegottdank a192fe3
fix build
colegottdank 836728c
revoke access
colegottdank 5aae5ad
Merge remote-tracking branch 'origin/main' into fix-posthog-signup
colegottdank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
supabase/migrations/20250408183827_user_signup_tracking.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,73 @@ | ||||||||||
CREATE EXTENSION IF NOT EXISTS http; | ||||||||||
CREATE TABLE IF NOT EXISTS public.system_config ( | ||||||||||
key TEXT PRIMARY KEY, | ||||||||||
value TEXT NOT NULL, | ||||||||||
description TEXT, | ||||||||||
created_at TIMESTAMPTZ DEFAULT NOW() | ||||||||||
); | ||||||||||
INSERT INTO public.system_config (key, value, description) | ||||||||||
VALUES ( | ||||||||||
'enable_tracking', | ||||||||||
'false', | ||||||||||
'Enable tracking of user signups' | ||||||||||
) ON CONFLICT (key) DO NOTHING; | ||||||||||
ALTER TABLE public.system_config ENABLE ROW LEVEL SECURITY; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
REVOKE ALL PRIVILEGES ON TABLE public.system_config | ||||||||||
FROM anon; | ||||||||||
REVOKE ALL PRIVILEGES ON TABLE public.system_config | ||||||||||
FROM authenticated; | ||||||||||
CREATE POLICY "Allow postgres access to system_config" ON public.system_config FOR ALL TO postgres USING (true); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fuck RLS
Suggested change
|
||||||||||
CREATE OR REPLACE FUNCTION public.track_user_signup_to_posthog() RETURNS TRIGGER LANGUAGE plpgsql SECURITY DEFINER | ||||||||||
SET search_path = public AS $$ | ||||||||||
DECLARE posthog_key TEXT; | ||||||||||
tracking_enabled TEXT; | ||||||||||
response http_response; | ||||||||||
BEGIN BEGIN | ||||||||||
SELECT value INTO tracking_enabled | ||||||||||
FROM public.system_config | ||||||||||
WHERE key = 'enable_tracking'; | ||||||||||
IF tracking_enabled = 'true' THEN BEGIN | ||||||||||
SELECT value INTO posthog_key | ||||||||||
FROM public.system_config | ||||||||||
WHERE key = 'posthog_public_key'; | ||||||||||
IF posthog_key IS NOT NULL | ||||||||||
AND posthog_key != '' THEN | ||||||||||
SELECT * INTO response | ||||||||||
FROM http_post( | ||||||||||
'https://app.posthog.com/capture/', | ||||||||||
jsonb_build_object( | ||||||||||
'api_key', | ||||||||||
posthog_key, | ||||||||||
'event', | ||||||||||
'user_signed_up', | ||||||||||
'properties', | ||||||||||
jsonb_build_object( | ||||||||||
'distinct_id', | ||||||||||
NEW.id, | ||||||||||
'email', | ||||||||||
NEW.email, | ||||||||||
'source', | ||||||||||
'database_trigger', | ||||||||||
'timestamp', | ||||||||||
extract( | ||||||||||
epoch | ||||||||||
from now() | ||||||||||
) * 1000 | ||||||||||
) | ||||||||||
)::text, | ||||||||||
'application/json' | ||||||||||
); | ||||||||||
END IF; | ||||||||||
EXCEPTION | ||||||||||
WHEN OTHERS THEN NULL; | ||||||||||
END; | ||||||||||
END IF; | ||||||||||
EXCEPTION | ||||||||||
WHEN OTHERS THEN NULL; | ||||||||||
END; | ||||||||||
RETURN NEW; | ||||||||||
END; | ||||||||||
$$; | ||||||||||
CREATE TRIGGER track_user_signup_after_creation | ||||||||||
AFTER | ||||||||||
INSERT ON auth.users FOR EACH ROW EXECUTE FUNCTION public.track_user_signup_to_posthog(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
CREATE OR REPLACE FUNCTION public.track_organization_onboarding() RETURNS TRIGGER LANGUAGE plpgsql SECURITY DEFINER AS $$ | ||
DECLARE posthog_key TEXT; | ||
tracking_enabled TEXT; | ||
org_owner_email TEXT; | ||
org_owner_name TEXT; | ||
response http_response; | ||
BEGIN IF ( | ||
OLD.has_onboarded = false | ||
AND NEW.has_onboarded = true | ||
AND NEW.tier != 'demo' | ||
) THEN | ||
SELECT value INTO tracking_enabled | ||
FROM public.system_config | ||
WHERE key = 'enable_tracking'; | ||
IF tracking_enabled = 'true' THEN BEGIN | ||
SELECT email, | ||
COALESCE( | ||
raw_user_meta_data->>'full_name', | ||
raw_user_meta_data->>'name', | ||
'' | ||
) INTO org_owner_email, | ||
org_owner_name | ||
FROM auth.users | ||
WHERE id = NEW.owner; | ||
SELECT value INTO posthog_key | ||
FROM public.system_config | ||
WHERE key = 'posthog_public_key'; | ||
IF posthog_key IS NOT NULL | ||
AND posthog_key != '' THEN | ||
SELECT * INTO response | ||
FROM http_post( | ||
'https://app.posthog.com/capture/', | ||
jsonb_build_object( | ||
'api_key', | ||
posthog_key, | ||
'event', | ||
'organization_onboarded', | ||
'distinct_id', | ||
NEW.owner, | ||
'properties', | ||
jsonb_build_object( | ||
'email', | ||
org_owner_email, | ||
'name', | ||
org_owner_name, | ||
'timestamp', | ||
extract( | ||
epoch | ||
from now() | ||
) * 1000, | ||
'$groups', | ||
jsonb_build_object( | ||
'organization', | ||
NEW.id | ||
) | ||
) | ||
)::text, | ||
'application/json' | ||
); | ||
END IF; | ||
EXCEPTION | ||
WHEN OTHERS THEN NULL; | ||
END; | ||
END IF; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$; | ||
CREATE TRIGGER track_organization_onboarding_trigger | ||
AFTER | ||
UPDATE ON public.organization FOR EACH ROW EXECUTE FUNCTION public.track_organization_onboarding(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
export class PosthogClient { | ||
private static instance: PosthogClient; | ||
private isEnabled: boolean; | ||
private apiKey: string | null; | ||
|
||
private constructor() { | ||
this.apiKey = process.env.NEXT_PUBLIC_POSTHOG_API_KEY || null; | ||
this.isEnabled = | ||
!!this.apiKey && | ||
process.env.NEXT_PUBLIC_DISABLE_POSTHOG !== "true" && | ||
process.env.NODE_ENV !== "development"; | ||
} | ||
|
||
static getInstance(): PosthogClient { | ||
if (!PosthogClient.instance) { | ||
PosthogClient.instance = new PosthogClient(); | ||
} | ||
return PosthogClient.instance; | ||
} | ||
|
||
public async captureEvent( | ||
eventName: string, | ||
properties: Record<string, any> = {}, | ||
userId?: string, | ||
organizationId?: string | ||
): Promise<boolean> { | ||
if (!this.isEnabled || !this.apiKey) { | ||
console.log(`[PostHog Disabled] Would have sent: ${eventName}`); | ||
return false; | ||
} | ||
|
||
try { | ||
const payload = { | ||
api_key: this.apiKey, | ||
event: eventName, | ||
distinct_id: userId || "server", | ||
properties: { | ||
...properties, | ||
...(organizationId | ||
? { $groups: { organization: organizationId } } | ||
: {}), | ||
}, | ||
}; | ||
|
||
const response = await fetch("https://app.posthog.com/capture/", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify(payload), | ||
}); | ||
|
||
const success = response.status === 200; | ||
if (!success) { | ||
console.error( | ||
`PostHog Error (${response.status}): ${await response.text()}` | ||
); | ||
} | ||
return success; | ||
} catch (error) { | ||
console.error("PostHog Capture Error:", error); | ||
return false; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this table will come in handy a lot, nice