diff --git a/apps/dashboard/src/lib/utils/services/org/index.ts b/apps/dashboard/src/lib/utils/services/org/index.ts index 961a3e7f..01739728 100644 --- a/apps/dashboard/src/lib/utils/services/org/index.ts +++ b/apps/dashboard/src/lib/utils/services/org/index.ts @@ -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 .from('organizationmember') .select( @@ -120,19 +120,30 @@ export async function getOrganizations(userId: string) { orgs.set(orgsArray); - if (localStorage) { - const lastOrgSiteName = localStorage.getItem('classroomio_org_sitename'); + // If this is a student dashboard + if (isOrgSite && orgSiteName) { + const orgData = orgsArray.find((org) => org.siteName === orgSiteName); - const lastOrg = orgsArray.find((org) => org.siteName === lastOrgSiteName); - - if (lastOrg) { - currentOrg.set(lastOrg); + if (orgData) { + currentOrg.set(orgData); } - } + } else { + // Check if org was last visited in localhost + if (localStorage) { + const lastOrgSiteName = localStorage.getItem('classroomio_org_sitename'); - const _currentOrg = get(currentOrg); - if (!_currentOrg.siteName) { - currentOrg.set(orgsArray[0]); + const lastOrg = orgsArray.find((org) => org.siteName === lastOrgSiteName); + + 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]); + } } } diff --git a/apps/dashboard/src/lib/utils/services/posthog/index.ts b/apps/dashboard/src/lib/utils/services/posthog/index.ts index 2aa9a1d4..ef7b980a 100644 --- a/apps/dashboard/src/lib/utils/services/posthog/index.ts +++ b/apps/dashboard/src/lib/utils/services/posthog/index.ts @@ -1,13 +1,13 @@ import { dev } from '$app/environment'; import posthog from 'posthog-js'; -export const capturePosthogEvent = (event: string, properties?: Record): void => { +export const capturePosthogEvent = (event: string, properties?: Record): void => { if (dev) return; posthog.capture(event, properties); }; -export const identifyPosthogUser = (id: string, properties?: Record): void => { +export const identifyPosthogUser = (id: string, properties?: Record): void => { if (dev) return; posthog.identify(id, properties); diff --git a/apps/dashboard/src/routes/+layout.svelte b/apps/dashboard/src/routes/+layout.svelte index 3e674fca..34897b68 100644 --- a/apps/dashboard/src/routes/+layout.svelte +++ b/apps/dashboard/src/routes/+layout.svelte @@ -166,7 +166,7 @@ // Set user in sentry setAnalyticsUser(); - const orgRes = await getOrganizations(profileData.id); + const orgRes = await getOrganizations(profileData.id, data.isOrgSite, data.orgSiteName); // student redirect if (data.isOrgSite) { diff --git a/apps/dashboard/src/routes/login/+page.svelte b/apps/dashboard/src/routes/login/+page.svelte index 8ae4a8e7..be84d357 100644 --- a/apps/dashboard/src/routes/login/+page.svelte +++ b/apps/dashboard/src/routes/login/+page.svelte @@ -8,6 +8,7 @@ import AuthUI from '$lib/components/AuthUI/index.svelte'; import { currentOrg } from '$lib/utils/store/org'; import { capturePosthogEvent } from '$lib/utils/services/posthog'; + import { globalStore } from '$lib/utils/store/app'; let formRef: HTMLFormElement; let supabase = getSupabase(); @@ -40,6 +41,12 @@ capturePosthogEvent('login', { email: fields.email }); + + if ($globalStore.isOrgSite) { + capturePosthogEvent('student_login', { + email: fields.email + }); + } } catch (error) { submitError = error.error_description || error.message; loading = false; diff --git a/apps/dashboard/src/routes/onboarding/+page.svelte b/apps/dashboard/src/routes/onboarding/+page.svelte index 9ae7cb2b..be58465e 100644 --- a/apps/dashboard/src/routes/onboarding/+page.svelte +++ b/apps/dashboard/src/routes/onboarding/+page.svelte @@ -167,7 +167,7 @@ // Set extra metadata based off location await setMetaData(); - let { data, error } = await supabase + await supabase .from('profile') .update({ ...fields, diff --git a/apps/dashboard/src/routes/signup/+page.svelte b/apps/dashboard/src/routes/signup/+page.svelte index 4f039aac..af59401d 100644 --- a/apps/dashboard/src/routes/signup/+page.svelte +++ b/apps/dashboard/src/routes/signup/+page.svelte @@ -14,6 +14,7 @@ import { profile } from '$lib/utils/store/user'; import { currentOrg } from '$lib/utils/store/org'; import { capturePosthogEvent } from '$lib/utils/services/posthog'; + import { globalStore } from '$lib/utils/store/app'; let supabase = getSupabase(); let fields = Object.assign({}, SIGNUP_FIELDS); @@ -92,6 +93,15 @@ metadata }); + if ($globalStore.isOrgSite) { + capturePosthogEvent('student_signed_up', { + distinct_id: $profile.id || '', + email: authUser.email, + username: regexUsernameMatch[1], + metadata + }); + } + if (redirect) { goto(redirect); } else {