Skip to content

Commit

Permalink
fix: improve segment group properties
Browse files Browse the repository at this point in the history
  • Loading branch information
scopsy committed Dec 25, 2024
1 parent 53dd3ee commit f2a6ba6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
24 changes: 24 additions & 0 deletions apps/dashboard/src/components/auth/questionnaire-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { ROUTES } from '../../utils/routes';
import { useMutation } from '@tanstack/react-query';
import { useOrganization, useUser } from '@clerk/clerk-react';
import { useEnvironment, useFetchEnvironments } from '../../context/environment/hooks';
import { useSegment } from '../../context/segment';
import { useAuth } from '../../context/auth/hooks';

interface QuestionnaireFormData {
jobTitle: JobTitleEnum;
Expand Down Expand Up @@ -241,6 +243,8 @@ export function QuestionnaireForm() {
}

function useSubmitQuestionnaire() {
const { currentUser, currentOrganization } = useAuth();
const segment = useSegment();
const track = useTelemetry();
const navigate = useNavigate();
const { currentEnvironment } = useEnvironment();
Expand Down Expand Up @@ -271,6 +275,26 @@ function useSubmitQuestionnaire() {
companySize: data.companySize,
organizationType: data.organizationType,
});

if (currentUser && currentOrganization) {
segment.identify(currentUser, {
organizationType: data.organizationType,
jobTitle: data.jobTitle,
companySize: data.companySize,
});

segment.group(
{
id: currentOrganization?._id,
name: currentOrganization?.name,
createdAt: currentOrganization?.createdAt,
},
{
organizationType: data.organizationType,
companySize: data.companySize,
}
);
}
},
onSuccess: () => {
navigate(ROUTES.USECASE_SELECT);
Expand Down
15 changes: 14 additions & 1 deletion apps/dashboard/src/utils/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class SegmentService {
}
}

identify(user: IUserEntity) {
identify(user: IUserEntity, extraProperties?: Record<string, unknown>) {
if (!this.isSegmentEnabled()) {
return;
}
Expand All @@ -70,6 +70,19 @@ export class SegmentService {
firstName: user.firstName,
lastName: user.lastName,
avatar: user.profilePicture,
...(extraProperties || {}),
});
}

group(organization: { id: string; name: string; createdAt: string }, extraProperties?: Record<string, unknown>) {
if (!this.isSegmentEnabled()) {
return;
}

this._segment?.group(organization.id, {
name: organization.name,
createdAt: organization.createdAt,
...(extraProperties || {}),
});
}

Expand Down

0 comments on commit f2a6ba6

Please sign in to comment.