Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ pas.alias({ id: "42", hash: "signed-user-hash" });
pas.pageView();
```

`pageView` sends the page's origin and path, its `utm_source`, `utm_medium`,
and `utm_campaign` values, and only the origin of the referrer. Nothing else
from either URL is sent, so search terms in them stay out of PAS.

Requests queued for retry are stored in a cookie and replayed on the next
initialization. The queue keeps at most the 20 most recent requests within
3000 bytes of percent-encoded JSON: a request too large on its own is
Expand Down
24 changes: 24 additions & 0 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ describe("Client", () => {
});
});

it.each([
["https://www.google.com/search?q=secret+movie#top", "https://www.google.com"],
["https://torrents.example/search/secret-movie/1/", "https://torrents.example"],
["https://app.put.io/search?phrase=secret%20movie", "https://app.put.io"],
["https://app.put.io/files?search=secret+movie", "https://app.put.io"],
[
"android-app://com.google.android.googlequicksearchbox/https/www.google.com",
"android-app://com.google.android.googlequicksearchbox",
],
["", ""],
])("sends only the referrer origin for pageView: %s", (referrer, expected) => {
vi.spyOn(document, "referrer", "get").mockReturnValueOnce(referrer);
client.pageView();

expect(mockAPI.post).toHaveBeenCalledWith("/events", {
events: [
expect.objectContaining({
properties: expect.objectContaining({ referrer: expected }),
}),
],
});
expect(JSON.stringify(vi.mocked(mockAPI.post).mock.calls)).not.toContain("secret");
});

it("clears cached user state", () => {
client.alias({ id: 7, hash: "user_hash" });
client.clear();
Expand Down
14 changes: 13 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ const defaultConfig = {

export type Config = typeof defaultConfig;

// A referrer's path, query, and fragment can carry search terms, so PAS only
// gets where the visit came from, never which page. `URL#origin` is "null" for
// app referrers such as android-app://, so build it from protocol and host.
const referrerOrigin = (referrer: string) => {
try {
const { host, protocol } = new URL(referrer);
return `${protocol}//${host}`;
} catch {
return "";
}
};

export const createClientFactoryWithDependencies =
(
cacheFactory: typeof createCache,
Expand Down Expand Up @@ -71,7 +83,7 @@ export const createClientFactoryWithDependencies =
return track("page_viewed", {
domain: origin,
path: pathname,
referrer: document.referrer,
referrer: referrerOrigin(document.referrer),
utm_source,
utm_medium,
utm_campaign,
Expand Down