Skip to content

Commit 97c55ed

Browse files
mfranciscDevtools
andauthored
add email domain (#1680)
Co-authored-by: Devtools <[email protected]>
1 parent 2a445d2 commit 97c55ed

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

workspaces/sandbox/plugins/sandbox/ANALYTICS-EVENTS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ All events include these properties:
4545

4646
## Identify & Group Calls
4747

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

5353
## Notes
5454

workspaces/sandbox/plugins/sandbox/src/types/registration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export type SignupData = {
2929
givenName: string;
3030
familyName: string;
3131
company: string;
32+
email?: string;
3233
userID?: string;
3334
accountID?: string;
3435
accountNumber?: string;

workspaces/sandbox/plugins/sandbox/src/utils/__tests__/segment-analytics.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,90 @@ describe('useSegmentAnalytics Hook', () => {
240240
expect(identify).toHaveBeenCalledTimes(1);
241241
});
242242

243+
it('should call identify with email_domain when email is provided', async () => {
244+
const identify = jest.fn();
245+
const group = jest.fn();
246+
const track = jest.fn();
247+
(AnalyticsBrowser.load as jest.Mock).mockReturnValue({
248+
identify,
249+
group,
250+
track,
251+
});
252+
253+
const user = {
254+
name: 'Test User',
255+
compliantUsername: 'testuser',
256+
username: 'testuser',
257+
givenName: 'Test',
258+
familyName: 'User',
259+
company: 'Test Co',
260+
261+
userID: 'uid-1',
262+
} as any;
263+
264+
const { rerender } = renderHook(
265+
({ u }) => useSegmentAnalytics('key', u as any),
266+
{ initialProps: { u: undefined as any } },
267+
);
268+
269+
await act(async () => {
270+
await new Promise(resolve => setTimeout(resolve, 0));
271+
});
272+
273+
rerender({ u: user });
274+
275+
await act(async () => {
276+
await new Promise(resolve => setTimeout(resolve, 0));
277+
});
278+
279+
expect(identify).toHaveBeenCalledTimes(1);
280+
expect(identify).toHaveBeenCalledWith('uid-1', {
281+
company: 'Test Co',
282+
email_domain: 'example.com',
283+
});
284+
});
285+
286+
it('should not include email_domain when email is not provided', async () => {
287+
const identify = jest.fn();
288+
const group = jest.fn();
289+
const track = jest.fn();
290+
(AnalyticsBrowser.load as jest.Mock).mockReturnValue({
291+
identify,
292+
group,
293+
track,
294+
});
295+
296+
const user = {
297+
name: 'Test User',
298+
compliantUsername: 'testuser',
299+
username: 'testuser',
300+
givenName: 'Test',
301+
familyName: 'User',
302+
company: 'Test Co',
303+
userID: 'uid-1',
304+
} as any;
305+
306+
const { rerender } = renderHook(
307+
({ u }) => useSegmentAnalytics('key', u as any),
308+
{ initialProps: { u: undefined as any } },
309+
);
310+
311+
await act(async () => {
312+
await new Promise(resolve => setTimeout(resolve, 0));
313+
});
314+
315+
rerender({ u: user });
316+
317+
await act(async () => {
318+
await new Promise(resolve => setTimeout(resolve, 0));
319+
});
320+
321+
expect(identify).toHaveBeenCalledTimes(1);
322+
expect(identify).toHaveBeenCalledWith('uid-1', {
323+
company: 'Test Co',
324+
});
325+
});
326+
243327
it('should call group once with accountID and accountNumber trait when present', async () => {
244328
const identify = jest.fn();
245329
const group = jest.fn();

workspaces/sandbox/plugins/sandbox/src/utils/segment-analytics.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ export const useSegmentAnalytics = (writeKey?: string, user?: SignupData) => {
8585
company: user?.company,
8686
};
8787

88+
// Extract email domain if email is available
89+
if (user?.email) {
90+
const emailDomain = user.email.split('@')[1];
91+
if (emailDomain) {
92+
traits.email_domain = emailDomain;
93+
}
94+
}
95+
8896
analyticsRef.current.identify(currentUserId, traits);
8997
hasIdentifiedRef.current = true;
9098
lastIdentifiedUserIdRef.current = currentUserId;

0 commit comments

Comments
 (0)