Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions workspaces/sandbox/plugins/sandbox/ANALYTICS-EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ All events include these properties:

## Identify & Group Calls

| Call Type | Platform | Trigger | ID Used | Traits/Properties |
| ------------ | -------- | ------------------------------------------- | ----------- | ------------------------------------------ |
| **Identify** | Segment | First time `userID` available in session | `userID` | `{ company: [user company] }` |
| **Group** | Segment | First time `accountID` available in session | `accountID` | `{ ebs: [account number] }` (when present) |
| Call Type | Platform | Trigger | ID Used | Traits/Properties |
| ------------ | -------- | ------------------------------------------- | ----------- | ------------------------------------------------------------------------------- |
| **Identify** | Segment | First time `userID` available in session | `userID` | `{ company: [user company], email_domain: [user email domain] }` (when present) |
| **Group** | Segment | First time `accountID` available in session | `accountID` | `{ ebs: [account number] }` (when present) |

## Notes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type SignupData = {
givenName: string;
familyName: string;
company: string;
email?: string;
userID?: string;
accountID?: string;
accountNumber?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,90 @@ describe('useSegmentAnalytics Hook', () => {
expect(identify).toHaveBeenCalledTimes(1);
});

it('should call identify with email_domain when email is provided', async () => {
const identify = jest.fn();
const group = jest.fn();
const track = jest.fn();
(AnalyticsBrowser.load as jest.Mock).mockReturnValue({
identify,
group,
track,
});

const user = {
name: 'Test User',
compliantUsername: 'testuser',
username: 'testuser',
givenName: 'Test',
familyName: 'User',
company: 'Test Co',
email: '[email protected]',
userID: 'uid-1',
} as any;

const { rerender } = renderHook(
({ u }) => useSegmentAnalytics('key', u as any),
{ initialProps: { u: undefined as any } },
);

await act(async () => {
await new Promise(resolve => setTimeout(resolve, 0));
});

rerender({ u: user });

await act(async () => {
await new Promise(resolve => setTimeout(resolve, 0));
});

expect(identify).toHaveBeenCalledTimes(1);
expect(identify).toHaveBeenCalledWith('uid-1', {
company: 'Test Co',
email_domain: 'example.com',
});
});

it('should not include email_domain when email is not provided', async () => {
const identify = jest.fn();
const group = jest.fn();
const track = jest.fn();
(AnalyticsBrowser.load as jest.Mock).mockReturnValue({
identify,
group,
track,
});

const user = {
name: 'Test User',
compliantUsername: 'testuser',
username: 'testuser',
givenName: 'Test',
familyName: 'User',
company: 'Test Co',
userID: 'uid-1',
} as any;

const { rerender } = renderHook(
({ u }) => useSegmentAnalytics('key', u as any),
{ initialProps: { u: undefined as any } },
);

await act(async () => {
await new Promise(resolve => setTimeout(resolve, 0));
});

rerender({ u: user });

await act(async () => {
await new Promise(resolve => setTimeout(resolve, 0));
});

expect(identify).toHaveBeenCalledTimes(1);
expect(identify).toHaveBeenCalledWith('uid-1', {
company: 'Test Co',
});
});

it('should call group once with accountID and accountNumber trait when present', async () => {
const identify = jest.fn();
const group = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ export const useSegmentAnalytics = (writeKey?: string, user?: SignupData) => {
company: user?.company,
};

// Extract email domain if email is available
if (user?.email) {
const emailDomain = user.email.split('@')[1];
if (emailDomain) {
traits.email_domain = emailDomain;
}
}

analyticsRef.current.identify(currentUserId, traits);
hasIdentifiedRef.current = true;
lastIdentifiedUserIdRef.current = currentUserId;
Expand Down