-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add GA4 login event tracking #916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
def7797
feat: add GA4 login event tracking #913
MillenniumFalconMechanic 8af13a3
fix: use existing tool_name param and GOOGLE_SIGN_IN_PROVIDER_ID cons…
MillenniumFalconMechanic d56a070
fix: gate login tracking on settled auth status to prevent hydration …
MillenniumFalconMechanic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { useEffect, useRef } from "react"; | ||
| import { AUTH_STATUS } from "../../auth/types/auth"; | ||
| import { track } from "../../common/analytics/analytics"; | ||
| import { EVENT_NAME, EVENT_PARAM } from "../../common/analytics/entities"; | ||
| import { GOOGLE_SIGN_IN_PROVIDER_ID } from "../../google/constants"; | ||
|
MillenniumFalconMechanic marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Tracks a GA4 login event when the user transitions from unauthenticated to authenticated. | ||
| * Does not fire during initial session hydration or on mount if already authenticated. | ||
| * @param isAuthenticated - Current authentication state. | ||
| * @param status - Current auth status; tracking only begins after status has settled. | ||
| * @returns void. | ||
| */ | ||
| export function useLoginTracking( | ||
| isAuthenticated: boolean, | ||
| status: AUTH_STATUS, | ||
| ): void { | ||
| const wasAuthenticated = useRef<boolean | undefined>(undefined); | ||
| useEffect(() => { | ||
| if (status !== AUTH_STATUS.SETTLED) return; | ||
| if (wasAuthenticated.current === false && isAuthenticated) { | ||
| track(EVENT_NAME.LOGIN, { | ||
| [EVENT_PARAM.TOOL_NAME]: GOOGLE_SIGN_IN_PROVIDER_ID, | ||
| }); | ||
|
MillenniumFalconMechanic marked this conversation as resolved.
|
||
| } | ||
| wasAuthenticated.current = isAuthenticated; | ||
| }, [isAuthenticated, status]); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { jest } from "@jest/globals"; | ||
| import { renderHook } from "@testing-library/react"; | ||
| import { AUTH_STATUS } from "../src/auth/types/auth"; | ||
|
|
||
| const mockTrack = jest.fn(); | ||
|
|
||
| jest.unstable_mockModule("../src/common/analytics/analytics", () => ({ | ||
| track: mockTrack, | ||
| })); | ||
|
|
||
| const { useLoginTracking } = | ||
| await import("../src/hooks/authentication/useLoginTracking"); | ||
|
|
||
| const SETTLED = AUTH_STATUS.SETTLED; | ||
| const PENDING = AUTH_STATUS.PENDING; | ||
|
|
||
| describe("useLoginTracking", () => { | ||
| beforeEach(() => { | ||
| mockTrack.mockReset(); | ||
| }); | ||
|
|
||
| test("does not fire on initial mount when unauthenticated", () => { | ||
| renderHook(() => useLoginTracking(false, SETTLED)); | ||
| expect(mockTrack).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("does not fire on initial mount when already authenticated", () => { | ||
| renderHook(() => useLoginTracking(true, SETTLED)); | ||
| expect(mockTrack).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("fires login event on transition from unauthenticated to authenticated", () => { | ||
| const { rerender } = renderHook( | ||
| ({ isAuthenticated, status }) => | ||
| useLoginTracking(isAuthenticated, status), | ||
| { initialProps: { isAuthenticated: false, status: SETTLED } }, | ||
| ); | ||
| rerender({ isAuthenticated: true, status: SETTLED }); | ||
| expect(mockTrack).toHaveBeenCalledTimes(1); | ||
| expect(mockTrack).toHaveBeenCalledWith("login", { tool_name: "google" }); | ||
| }); | ||
|
|
||
| test("does not fire on transition from authenticated to unauthenticated", () => { | ||
| const { rerender } = renderHook( | ||
| ({ isAuthenticated, status }) => | ||
| useLoginTracking(isAuthenticated, status), | ||
| { initialProps: { isAuthenticated: true, status: SETTLED } }, | ||
| ); | ||
| rerender({ isAuthenticated: false, status: SETTLED }); | ||
| expect(mockTrack).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("fires for each login transition", () => { | ||
| const { rerender } = renderHook( | ||
| ({ isAuthenticated, status }) => | ||
| useLoginTracking(isAuthenticated, status), | ||
| { initialProps: { isAuthenticated: false, status: SETTLED } }, | ||
| ); | ||
| rerender({ isAuthenticated: true, status: SETTLED }); | ||
| rerender({ isAuthenticated: false, status: SETTLED }); | ||
| rerender({ isAuthenticated: true, status: SETTLED }); | ||
| expect(mockTrack).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| test("does not fire during session hydration when status is pending", () => { | ||
| const { rerender } = renderHook( | ||
| ({ isAuthenticated, status }) => | ||
| useLoginTracking(isAuthenticated, status), | ||
| { initialProps: { isAuthenticated: false, status: PENDING } }, | ||
| ); | ||
| // Session hydrates: isAuthenticated becomes true while status settles. | ||
| rerender({ isAuthenticated: true, status: SETTLED }); | ||
| expect(mockTrack).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.