-
Notifications
You must be signed in to change notification settings - Fork 514
Fix EventTracker silently dormant in real browsers #1327
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
6 commits
Select commit
Hold shift + click to select a range
dfb0916
Fix EventTracker silently dormant in real browsers
BilalG1 839bee3
fix
BilalG1 a17281b
even tracker test lint fix
BilalG1 d36484a
Merge branch 'dev' into event-tracker-fix
BilalG1 fc923a9
Annotate event-tracker test callback to satisfy typecheck
BilalG1 5e4ec75
Merge branch 'dev' into event-tracker-fix
BilalG1 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
105 changes: 105 additions & 0 deletions
105
packages/template/src/lib/stack-app/apps/implementations/event-tracker.test.ts
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,105 @@ | ||
| // @vitest-environment jsdom | ||
|
|
||
| import { Result } from "@stackframe/stack-shared/dist/utils/results"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { EventTracker } from "./event-tracker"; | ||
|
|
||
| async function advancePastAccessTokenRefresh() { | ||
| await vi.advanceTimersByTimeAsync(10_000); | ||
| await Promise.resolve(); | ||
| await vi.advanceTimersByTimeAsync(10_000); | ||
| await Promise.resolve(); | ||
| } | ||
|
|
||
| function getSentEventTypes(sentBodies: string[]) { | ||
| const [body] = sentBodies; | ||
|
|
||
| const payload = JSON.parse(body); | ||
| if (typeof payload !== "object" || payload === null || !("events" in payload) || !Array.isArray(payload.events)) { | ||
| throw new Error("Expected analytics batch payload to include an events array."); | ||
| } | ||
|
|
||
| return (payload.events as { event_type: string }[]).map((event) => event.event_type); | ||
| } | ||
|
|
||
| describe("EventTracker", () => { | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("captures events when browser globals are exposed as accessor descriptors", async () => { | ||
| vi.useFakeTimers(); | ||
| document.body.innerHTML = "<button>Open project</button>"; | ||
|
|
||
| const screenDescriptor = Object.getOwnPropertyDescriptor(window, "screen"); | ||
| const historyDescriptor = Object.getOwnPropertyDescriptor(window, "history"); | ||
| expect(screenDescriptor?.value).toBeUndefined(); | ||
| expect(historyDescriptor?.value).toBeUndefined(); | ||
| expect(screenDescriptor?.get).toBeTypeOf("function"); | ||
| expect(historyDescriptor?.get).toBeTypeOf("function"); | ||
|
|
||
| const sentBodies: string[] = []; | ||
| const tracker = new EventTracker({ | ||
| projectId: "internal", | ||
| getAccessToken: async () => "access-token", | ||
| sendBatch: async (body) => { | ||
| sentBodies.push(body); | ||
| return Result.ok(new Response()); | ||
| }, | ||
| }); | ||
|
|
||
| try { | ||
| tracker.start(); | ||
| document.querySelector("button")?.dispatchEvent(new MouseEvent("click", { | ||
| bubbles: true, | ||
| clientX: 12, | ||
| clientY: 34, | ||
| })); | ||
|
|
||
| await advancePastAccessTokenRefresh(); | ||
|
|
||
| expect(getSentEventTypes(sentBodies)).toMatchInlineSnapshot(` | ||
| [ | ||
| "$page-view", | ||
| "$click", | ||
| ] | ||
| `); | ||
| } finally { | ||
| tracker.stop(); | ||
| } | ||
| }); | ||
|
|
||
| it("captures client-side navigations when history is exposed as an accessor descriptor", async () => { | ||
| vi.useFakeTimers(); | ||
|
|
||
| const historyDescriptor = Object.getOwnPropertyDescriptor(window, "history"); | ||
| expect(historyDescriptor?.value).toBeUndefined(); | ||
| expect(historyDescriptor?.get).toBeTypeOf("function"); | ||
|
|
||
| const sentBodies: string[] = []; | ||
| const tracker = new EventTracker({ | ||
| projectId: "internal", | ||
| getAccessToken: async () => "access-token", | ||
| sendBatch: async (body) => { | ||
| sentBodies.push(body); | ||
| return Result.ok(new Response()); | ||
| }, | ||
| }); | ||
|
|
||
| try { | ||
| tracker.start(); | ||
| window.history.pushState({}, "", "/projects/test-project"); | ||
|
|
||
| await advancePastAccessTokenRefresh(); | ||
|
|
||
| expect(getSentEventTypes(sentBodies)).toMatchInlineSnapshot(` | ||
| [ | ||
| "$page-view", | ||
| "$page-view", | ||
| ] | ||
| `); | ||
| } finally { | ||
| tracker.stop(); | ||
| } | ||
| }); | ||
| }); | ||
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
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.