-
Notifications
You must be signed in to change notification settings - Fork 59
Analytics Testing & Exploration [AARD-1937]
#1237
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
17 commits
Select commit
Hold shift + click to select a range
406db1d
feat: improve analytics tracking
rutmanz 4abef18
feat: add analytics testing
rutmanz 39eb23f
fix: firefox doesn't do analytics
rutmanz dbe2237
feat: restructure analytics tests
rutmanz 9c56fb1
fix: cleanup and format analytics tests
rutmanz 2a5118c
Merge remote-tracking branch 'origin/dev' into zachr/1937/analytics-2
rutmanz 9b0aa31
fix: remove test artifacts
rutmanz 80b8c2d
fix: refactoring issues
rutmanz 42aacca
Merge remote-tracking branch 'origin/dev' into zachr/1937/analytics-2
rutmanz 4168c3c
fix: remove git dependencies
rutmanz 3a0a934
Merge remote-tracking branch 'origin/dev' into zachr/1937/analytics-2
rutmanz 52874f1
fix: remove redundant test
rutmanz 3bf2c0a
Merge remote-tracking branch 'origin/dev' into zachr/1937/analytics-2
rutmanz 7ab57e1
fix: update bun lockfile
rutmanz 0c6b034
fix: biome issues
rutmanz 74f4acf
Merge branch 'dev' into zachr/1937/analytics-2
rutmanz fd963e0
Merge branch 'dev' into zachr/1937/analytics-2
PepperLola 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,3 +28,6 @@ dist-ssr | |
| *.sw? | ||
|
|
||
| yarn.lock | ||
|
|
||
| test-results | ||
| src/test/**/__screenshots__ | ||
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| declare interface Window { | ||
| convertAuthToken(code: string): void | ||
| gtag: () => void | ||
| gtag?: (command: "config" | "set" | "get" | "event" | "consent", ...args: unknown[]) => void | ||
| dataLayer?: unknown[][] | ||
| } |
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
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
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
Empty file.
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 @@ | ||
| import { install } from "@haensl/google-analytics" | ||
| import { server } from "@vitest/browser/context" | ||
| import { HttpResponse, http } from "msw" | ||
| import { setupWorker } from "msw/browser" | ||
| import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, expectTypeOf, type Mock, test, vi } from "vitest" | ||
| import AnalyticsSystem from "@/systems/analytics/AnalyticsSystem.ts" | ||
| import PreferencesSystem from "@/systems/preferences/PreferencesSystem.ts" | ||
|
|
||
| type RequestType = Parameters<Parameters<typeof http.get>[1]>[0] | ||
| const tagID = "G-6XNCRD7QNC" | ||
|
|
||
| describe("Analytics", () => { | ||
| const gtagRequestMock: Mock<(req: RequestType) => void> = vi.fn(() => {}) | ||
|
|
||
| const restHandlers = [ | ||
| http.post("https://www.google-analytics.com/g/collect", req => { | ||
| gtagRequestMock(req) | ||
| return HttpResponse.text("") | ||
| }), | ||
| ] | ||
|
|
||
| const webMocks = setupWorker(...restHandlers) | ||
|
|
||
| // Start server before all tests | ||
| beforeAll(async () => await webMocks.start({ onUnhandledRequest: "bypass", quiet: true })) | ||
|
|
||
| // Close server after all tests | ||
| afterAll(() => webMocks.stop()) | ||
|
|
||
| // Reset handlers after each test `important for test isolation` | ||
| afterEach(() => webMocks.resetHandlers()) | ||
| beforeEach(() => { | ||
| vi.resetAllMocks() | ||
| }) | ||
|
|
||
| afterEach(() => {}) | ||
|
|
||
| const mockRequestParametersHandle = () => { | ||
| return new Promise<URLSearchParams>(resolve => { | ||
| gtagRequestMock.mockImplementationOnce(req => { | ||
| resolve(new URL(req.request.url).searchParams) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| describe("With gtag Script", () => { | ||
| beforeAll(async () => { | ||
| vi.useFakeTimers() | ||
| const script = document.createElement("script") | ||
| script.src = "https://www.googletagmanager.com/gtag/js?id=" + tagID | ||
| document.head.appendChild(script) | ||
| await vi.waitUntil(() => window.dataLayer != null, { timeout: 3000 }) | ||
| install() // gtag is a function defined here to push to the datalayer object | ||
| }) | ||
|
|
||
| test("google analytics loaded", async () => { | ||
| expect(window.gtag).toBeDefined() | ||
| expect(window.dataLayer).toBeDefined() | ||
| expectTypeOf(window.gtag!).toBeFunction() | ||
| expectTypeOf(window.dataLayer!).toBeArray() | ||
| }) | ||
|
|
||
| test("gtag calls fetch with appropriate values", async ({ skip }) => { | ||
| skip(server.browser == "firefox", "Firefox blocks Google Analytics") | ||
| PreferencesSystem.setGlobalPreference("ReportAnalytics", true) | ||
|
|
||
| const initialParams = mockRequestParametersHandle() | ||
|
|
||
| const gtagSpy: Mock<NonNullable<typeof window.gtag>> = vi.spyOn(window, "gtag") | ||
| const system = new AnalyticsSystem() | ||
| expect(gtagSpy).toHaveBeenCalled() | ||
| await initialParams.then(params => { | ||
| expect(params.get("tid")).toBe(tagID) | ||
| }) | ||
| gtagSpy.mockClear() | ||
|
|
||
| const eventParams = mockRequestParametersHandle() | ||
| system.event("APS Calls per Minute", {}) | ||
|
|
||
| expect(gtagSpy).toHaveBeenCalled() | ||
| await eventParams.then(params => { | ||
| expect(params.get("tid")).toBe(tagID) | ||
| expect(params.get("en")).toBe("APS Calls per Minute") | ||
| }) | ||
| }, 20000) | ||
| }) | ||
|
|
||
| describe("Without gtag Script", () => { | ||
| beforeEach(() => { | ||
| window.dataLayer = undefined | ||
| window.gtag = undefined | ||
| install() | ||
| }) | ||
|
|
||
| test("gtag propagates to dataLayer", () => { | ||
| const initialSize = window.dataLayer!.length | ||
| window.gtag!("event", "test", { a: 2 }) | ||
| expect(window.dataLayer!.length).toBe(initialSize + 1) | ||
| const lastDatalayerItem = window.dataLayer![window.dataLayer!.length - 1] | ||
| expect(lastDatalayerItem[0]).toBe("event") | ||
| expect(lastDatalayerItem[1]).toBe("test") | ||
| expect(lastDatalayerItem[2]).toStrictEqual(expect.objectContaining({ a: 2 })) | ||
| }) | ||
| }) | ||
| }) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you put a note in #1190 to fix this if this PR gets merged first.