feat: add posthog student signup

This commit is contained in:
Rotimi Best
2024-04-07 22:01:36 +01:00
parent d01c1a1ba3
commit 9311a21de8
6 changed files with 43 additions and 15 deletions
@@ -63,7 +63,7 @@ export async function getOrgTeam(orgId: string) {
}; };
} }
export async function getOrganizations(userId: string) { export async function getOrganizations(userId: string, isOrgSite?: boolean, orgSiteName?: string) {
const { data, error } = await supabase const { data, error } = await supabase
.from('organizationmember') .from('organizationmember')
.select( .select(
@@ -120,19 +120,30 @@ export async function getOrganizations(userId: string) {
orgs.set(orgsArray); orgs.set(orgsArray);
if (localStorage) { // If this is a student dashboard
const lastOrgSiteName = localStorage.getItem('classroomio_org_sitename'); if (isOrgSite && orgSiteName) {
const orgData = orgsArray.find((org) => org.siteName === orgSiteName);
const lastOrg = orgsArray.find((org) => org.siteName === lastOrgSiteName); if (orgData) {
currentOrg.set(orgData);
if (lastOrg) {
currentOrg.set(lastOrg);
} }
} } else {
// Check if org was last visited in localhost
if (localStorage) {
const lastOrgSiteName = localStorage.getItem('classroomio_org_sitename');
const _currentOrg = get(currentOrg); const lastOrg = orgsArray.find((org) => org.siteName === lastOrgSiteName);
if (!_currentOrg.siteName) {
currentOrg.set(orgsArray[0]); if (lastOrg) {
currentOrg.set(lastOrg);
}
}
// Default to setting the first org in the array of orgs
const _currentOrg = get(currentOrg);
if (!_currentOrg.siteName) {
currentOrg.set(orgsArray[0]);
}
} }
} }
@@ -1,13 +1,13 @@
import { dev } from '$app/environment'; import { dev } from '$app/environment';
import posthog from 'posthog-js'; import posthog from 'posthog-js';
export const capturePosthogEvent = (event: string, properties?: Record<string, any>): void => { export const capturePosthogEvent = (event: string, properties?: Record<string, unknown>): void => {
if (dev) return; if (dev) return;
posthog.capture(event, properties); posthog.capture(event, properties);
}; };
export const identifyPosthogUser = (id: string, properties?: Record<string, any>): void => { export const identifyPosthogUser = (id: string, properties?: Record<string, unknown>): void => {
if (dev) return; if (dev) return;
posthog.identify(id, properties); posthog.identify(id, properties);
+1 -1
View File
@@ -166,7 +166,7 @@
// Set user in sentry // Set user in sentry
setAnalyticsUser(); setAnalyticsUser();
const orgRes = await getOrganizations(profileData.id); const orgRes = await getOrganizations(profileData.id, data.isOrgSite, data.orgSiteName);
// student redirect // student redirect
if (data.isOrgSite) { if (data.isOrgSite) {
@@ -8,6 +8,7 @@
import AuthUI from '$lib/components/AuthUI/index.svelte'; import AuthUI from '$lib/components/AuthUI/index.svelte';
import { currentOrg } from '$lib/utils/store/org'; import { currentOrg } from '$lib/utils/store/org';
import { capturePosthogEvent } from '$lib/utils/services/posthog'; import { capturePosthogEvent } from '$lib/utils/services/posthog';
import { globalStore } from '$lib/utils/store/app';
let formRef: HTMLFormElement; let formRef: HTMLFormElement;
let supabase = getSupabase(); let supabase = getSupabase();
@@ -40,6 +41,12 @@
capturePosthogEvent('login', { capturePosthogEvent('login', {
email: fields.email email: fields.email
}); });
if ($globalStore.isOrgSite) {
capturePosthogEvent('student_login', {
email: fields.email
});
}
} catch (error) { } catch (error) {
submitError = error.error_description || error.message; submitError = error.error_description || error.message;
loading = false; loading = false;
@@ -167,7 +167,7 @@
// Set extra metadata based off location // Set extra metadata based off location
await setMetaData(); await setMetaData();
let { data, error } = await supabase await supabase
.from('profile') .from('profile')
.update({ .update({
...fields, ...fields,
@@ -14,6 +14,7 @@
import { profile } from '$lib/utils/store/user'; import { profile } from '$lib/utils/store/user';
import { currentOrg } from '$lib/utils/store/org'; import { currentOrg } from '$lib/utils/store/org';
import { capturePosthogEvent } from '$lib/utils/services/posthog'; import { capturePosthogEvent } from '$lib/utils/services/posthog';
import { globalStore } from '$lib/utils/store/app';
let supabase = getSupabase(); let supabase = getSupabase();
let fields = Object.assign({}, SIGNUP_FIELDS); let fields = Object.assign({}, SIGNUP_FIELDS);
@@ -92,6 +93,15 @@
metadata metadata
}); });
if ($globalStore.isOrgSite) {
capturePosthogEvent('student_signed_up', {
distinct_id: $profile.id || '',
email: authUser.email,
username: regexUsernameMatch[1],
metadata
});
}
if (redirect) { if (redirect) {
goto(redirect); goto(redirect);
} else { } else {