-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrument history api (push/popstate) (#140)
* Add pushState tracking + test * Preserve pushState arity * Add popstate handler, tests * Instrument history first time we track a page view
- Loading branch information
1 parent
be18e7f
commit 17c6b53
Showing
4 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,19 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>03 PushState</title> | ||
</head> | ||
<body> | ||
<button | ||
onClick="history.pushState({page: 2}, '', '/03_pushState/part_2')" | ||
> | ||
Part 2 | ||
</button> | ||
<script | ||
id="counterscale-script" | ||
data-site-id="your-unique-site-id" | ||
src="http://localhost:3004/tracker.js" | ||
defer | ||
></script> | ||
</body> | ||
</html> |
This file contains 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,66 @@ | ||
import { test, expect, Request } from "@playwright/test"; | ||
|
||
test.describe("03_pushState", () => { | ||
test("tracks pushState and popState events as pageviews", async ({ | ||
page, | ||
}) => { | ||
// Listen for all console events and handle errors | ||
page.on("console", (msg) => { | ||
if (msg.type() === "error") | ||
console.log(`Error text: "${msg.text()}"`); | ||
}); | ||
|
||
const collectRequests: Request[] = []; | ||
page.on("request", (request) => { | ||
if (request.url().includes("/collect")) { | ||
collectRequests.push(request); | ||
} | ||
}); | ||
|
||
await page.goto("http://localhost:3004/03_pushState/"); | ||
expect(collectRequests).toHaveLength(1); | ||
|
||
let request = collectRequests[0]; | ||
expect(request).toBeTruthy(); | ||
expect(request.method()).toBe("GET"); | ||
let params = new URLSearchParams(request.url().split("?")[1]); | ||
expect(params.get("sid")).toBe("your-unique-site-id"); | ||
expect(params.get("h")).toBe("http://localhost"); // drops port | ||
expect(params.get("p")).toBe("/03_pushState/"); | ||
expect(params.get("r")).toBe(""); | ||
|
||
// click <button> to initiate pushState | ||
await page.click("button"); | ||
|
||
// assert url changed | ||
expect(page.url()).toBe("http://localhost:3004/03_pushState/part_2"); | ||
|
||
expect(collectRequests).toHaveLength(2); | ||
request = collectRequests[1]; | ||
expect(request).toBeTruthy(); | ||
expect(request.method()).toBe("GET"); | ||
params = new URLSearchParams(request.url().split("?")[1]); | ||
expect(params.get("sid")).toBe("your-unique-site-id"); | ||
expect(params.get("h")).toBe("http://localhost"); // drops port | ||
expect(params.get("p")).toBe("/03_pushState/part_2"); | ||
expect(params.get("r")).toBe(""); | ||
|
||
// back button (popState) | ||
await page.goBack({ | ||
// NOTE: "load" already fired because the page is not being reloaded, so need | ||
// to wait on networkidle instead | ||
waitUntil: "networkidle", | ||
}); | ||
|
||
expect(page.url()).toBe("http://localhost:3004/03_pushState/"); | ||
expect(collectRequests).toHaveLength(3); | ||
request = collectRequests[2]; | ||
expect(request).toBeTruthy(); | ||
expect(request.method()).toBe("GET"); | ||
params = new URLSearchParams(request.url().split("?")[1]); | ||
expect(params.get("sid")).toBe("your-unique-site-id"); | ||
expect(params.get("h")).toBe("http://localhost"); // drops port | ||
expect(params.get("p")).toBe("/03_pushState/"); | ||
expect(params.get("r")).toBe(""); | ||
}); | ||
}); |
This file contains 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