Repository navigation
analytics: report referrer and traffic source in both analytics versions #348
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
Open
MO-Thibault
wants to merge
3
commits into
master
Choose a base branch
from
analytics-traffic-source
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,131 @@ | ||
| import { getTrafficSource, TRAFFIC_SOURCE_KEY } from "./traffic-source"; | ||
|
|
||
| function visit(url: string, referrer = "") { | ||
| const { hostname, search } = new URL(url); | ||
| Object.defineProperty(window, "location", { | ||
| value: { hostname, search }, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(document, "referrer", { value: referrer, configurable: true }); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| sessionStorage.clear(); | ||
| }); | ||
|
|
||
| describe("getTrafficSource - classification", () => { | ||
| it("reports direct when there is no referrer", () => { | ||
| visit("https://www.site.com/"); | ||
| expect(getTrafficSource()).toEqual({ referrer: "", trafficSource: "direct" }); | ||
| }); | ||
|
|
||
| it("reports direct when the referrer is not a valid URL", () => { | ||
| visit("https://www.site.com/", "not a url"); | ||
| expect(getTrafficSource()).toEqual({ referrer: "", trafficSource: "direct" }); | ||
| }); | ||
|
|
||
| it("reports internal for the same host", () => { | ||
| visit("https://www.site.com/b", "https://www.site.com/a"); | ||
| expect(getTrafficSource().trafficSource).toBe("internal"); | ||
| }); | ||
|
|
||
| it("reports internal for another subdomain of the same site", () => { | ||
| visit("https://www.site.com/b", "https://news.site.com/a"); | ||
| expect(getTrafficSource().trafficSource).toBe("internal"); | ||
| }); | ||
|
|
||
| it("reports internal for subdomains under a two-part suffix", () => { | ||
| visit("https://www.site.co.uk/b", "https://news.site.co.uk/a"); | ||
| expect(getTrafficSource().trafficSource).toBe("internal"); | ||
| }); | ||
|
|
||
| it("reports internal before campaign when an internal link carries UTM params", () => { | ||
| visit("https://www.site.com/b?utm_source=newsletter&utm_medium=email", "https://www.site.com/a"); | ||
| expect(getTrafficSource().trafficSource).toBe("internal"); | ||
| }); | ||
|
|
||
| it("reports paid for a click id", () => { | ||
| visit("https://www.site.com/?gclid=abc", "https://www.google.com/"); | ||
| expect(getTrafficSource()).toEqual({ referrer: "www.google.com", trafficSource: "paid" }); | ||
| }); | ||
|
|
||
| it("reports paid for a paid utm_medium, case-insensitively", () => { | ||
| visit("https://www.site.com/?utm_source=fb&utm_medium=CPC"); | ||
| expect(getTrafficSource().trafficSource).toBe("paid"); | ||
| }); | ||
|
|
||
| it("reports email for utm_medium=email", () => { | ||
| visit("https://www.site.com/?utm_source=newsletter&utm_medium=email"); | ||
| expect(getTrafficSource().trafficSource).toBe("email"); | ||
| }); | ||
|
|
||
| it("reports campaign for utm_source without a paid or email medium", () => { | ||
| visit("https://www.site.com/?utm_source=partner", "https://partner.com/"); | ||
| expect(getTrafficSource().trafficSource).toBe("campaign"); | ||
| }); | ||
|
|
||
| it("reports organic_search for a search engine", () => { | ||
| visit("https://www.site.com/", "https://www.google.co.uk/"); | ||
| expect(getTrafficSource()).toEqual({ referrer: "www.google.co.uk", trafficSource: "organic_search" }); | ||
| }); | ||
|
|
||
| it("reports social for a social network", () => { | ||
| visit("https://www.site.com/", "https://t.co/xyz"); | ||
| expect(getTrafficSource()).toEqual({ referrer: "t.co", trafficSource: "social" }); | ||
| }); | ||
|
|
||
| it("reports referral for any other site", () => { | ||
| visit("https://www.site.com/", "https://blog.example.org/post?email=a@b.c"); | ||
| expect(getTrafficSource()).toEqual({ referrer: "blog.example.org", trafficSource: "referral" }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getTrafficSource - session caching", () => { | ||
| it("keeps the first external source across internal navigation", () => { | ||
| visit("https://www.site.com/a", "https://www.google.com/"); | ||
| expect(getTrafficSource().trafficSource).toBe("organic_search"); | ||
|
|
||
| visit("https://www.site.com/b", "https://www.site.com/a"); | ||
| expect(getTrafficSource()).toEqual({ referrer: "www.google.com", trafficSource: "organic_search" }); | ||
| }); | ||
|
|
||
| it("does not replace a cached external source with a later one", () => { | ||
| visit("https://www.site.com/a", "https://www.google.com/"); | ||
| getTrafficSource(); | ||
|
|
||
| visit("https://www.site.com/b", "https://www.facebook.com/"); | ||
| expect(getTrafficSource().trafficSource).toBe("organic_search"); | ||
| }); | ||
|
|
||
| it("does not cache internal", () => { | ||
| visit("https://www.site.com/b", "https://www.site.com/a"); | ||
| expect(getTrafficSource().trafficSource).toBe("internal"); | ||
| expect(sessionStorage.getItem(TRAFFIC_SOURCE_KEY)).toBeNull(); | ||
| }); | ||
|
|
||
| it("keeps a cached direct across internal navigation", () => { | ||
| visit("https://www.site.com/a"); | ||
| getTrafficSource(); | ||
|
|
||
| visit("https://www.site.com/b", "https://www.site.com/a"); | ||
| expect(getTrafficSource()).toEqual({ referrer: "", trafficSource: "direct" }); | ||
| }); | ||
|
|
||
| it("replaces a cached direct with a later external source", () => { | ||
| visit("https://www.site.com/a"); | ||
| getTrafficSource(); | ||
|
|
||
| visit("https://www.site.com/b", "https://www.facebook.com/"); | ||
| expect(getTrafficSource()).toEqual({ referrer: "www.facebook.com", trafficSource: "social" }); | ||
|
|
||
| visit("https://www.site.com/c", "https://www.site.com/b"); | ||
| expect(getTrafficSource().trafficSource).toBe("social"); | ||
| }); | ||
|
|
||
| it("recomputes when the cached value is malformed", () => { | ||
| sessionStorage.setItem(TRAFFIC_SOURCE_KEY, "{not json"); | ||
| visit("https://www.site.com/", "https://www.bing.com/"); | ||
| expect(getTrafficSource().trafficSource).toBe("organic_search"); | ||
| }); | ||
| }); |
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,115 @@ | ||
| const TRAFFIC_SOURCE_KEY = "optableTrafficSource"; | ||
|
|
||
| const SEARCH_SITES = ["google", "bing", "yahoo", "duckduckgo", "ecosia", "baidu", "yandex", "qwant", "startpage"]; | ||
| const SOCIAL_SITES = [ | ||
| "facebook", | ||
| "fb", | ||
| "instagram", | ||
| "twitter", | ||
| "x", | ||
| "t", | ||
| "linkedin", | ||
| "lnkd", | ||
| "reddit", | ||
| "pinterest", | ||
| "tiktok", | ||
| "threads", | ||
| "snapchat", | ||
| "youtube", | ||
| ]; | ||
| const PAID_CLICK_IDS = ["gclid", "gbraid", "wbraid", "fbclid", "msclkid", "ttclid", "twclid", "dclid"]; | ||
| const PAID_MEDIUMS = ["cpc", "ppc", "paid", "paidsocial", "paid_social", "display", "cpm"]; | ||
|
|
||
| // Suffixes that take two labels, so the registrable name is the third from the | ||
| // right (google.co.uk -> google) rather than the second (-> co). | ||
| const TWO_PART_SUFFIXES = ["co.uk", "com.au", "co.jp", "co.in", "com.br", "co.nz", "com.mx", "co.za", "co.kr"]; | ||
|
|
||
| type TrafficSource = "direct" | "internal" | "paid" | "campaign" | "email" | "organic_search" | "social" | "referral"; | ||
|
|
||
| type TrafficSourceProperties = { | ||
| referrer: string; | ||
| trafficSource: TrafficSource; | ||
| }; | ||
|
|
||
| // Registrable name of a host: "news.google.co.uk" -> "google", "t.co" -> "t". | ||
| // Approximate, since resolving this exactly needs the public suffix list. | ||
| function siteName(host: string): string { | ||
| const labels = host.toLowerCase().split("."); | ||
| if (labels.length < 2) return labels[0] || ""; | ||
|
|
||
| const index = TWO_PART_SUFFIXES.includes(labels.slice(-2).join(".")) ? labels.length - 3 : labels.length - 2; | ||
| return labels[index] || ""; | ||
| } | ||
|
|
||
| function referrerHost(): string { | ||
| if (!document.referrer) return ""; | ||
|
|
||
| try { | ||
| return new URL(document.referrer).hostname; | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| // Internal is checked first so that UTM-tagged links between the site's own | ||
| // pages are not reported as campaign traffic. Matching on the registrable name | ||
| // rather than the exact host keeps www.site.com -> news.site.com internal. | ||
| function classify(host: string): TrafficSource { | ||
| const site = siteName(host); | ||
| if (host && site === siteName(window.location.hostname)) return "internal"; | ||
|
|
||
| const params = new URLSearchParams(window.location.search); | ||
| const medium = (params.get("utm_medium") || "").toLowerCase(); | ||
|
|
||
| if (PAID_CLICK_IDS.some((id) => params.has(id)) || PAID_MEDIUMS.includes(medium)) return "paid"; | ||
| if (medium === "email") return "email"; | ||
| if (params.has("utm_source")) return "campaign"; | ||
| if (!host) return "direct"; | ||
|
|
||
| if (SEARCH_SITES.includes(site)) return "organic_search"; | ||
| if (SOCIAL_SITES.includes(site)) return "social"; | ||
|
|
||
| return "referral"; | ||
| } | ||
|
|
||
| /** | ||
| * Referring host and classified traffic source for the current page. | ||
| * | ||
| * The result is first-touch per tab: the first non-internal classification is | ||
| * cached in `sessionStorage` and reused for the rest of the session. Without | ||
| * that, `document.referrer` becomes the site's own hostname after the first | ||
| * internal click and every later event would report `internal`. | ||
| * | ||
| * A cached `direct` is the one exception. It is the weakest signal (in-app | ||
| * browsers and `no-referrer` policies also land there), so a later non-internal | ||
| * classification in the same tab replaces it. | ||
| * | ||
| * Only the referring hostname is reported, never the full referring URL, which | ||
| * can carry query strings with personal data. | ||
| */ | ||
| function getTrafficSource(): TrafficSourceProperties { | ||
| let cached: TrafficSourceProperties | null = null; | ||
| try { | ||
| const stored = window.sessionStorage?.getItem(TRAFFIC_SOURCE_KEY); | ||
| if (stored) cached = JSON.parse(stored) as TrafficSourceProperties; | ||
| } catch { | ||
| // sessionStorage unavailable or holding malformed JSON, fall through and recompute. | ||
| } | ||
| if (cached && cached.trafficSource !== "direct") return cached; | ||
|
|
||
| const referrer = referrerHost(); | ||
| const properties: TrafficSourceProperties = { referrer, trafficSource: classify(referrer) }; | ||
|
|
||
| if (properties.trafficSource === "internal") return cached ?? properties; | ||
|
|
||
| try { | ||
| window.sessionStorage?.setItem(TRAFFIC_SOURCE_KEY, JSON.stringify(properties)); | ||
| } catch { | ||
| // Storage blocked or full, the value is still returned for this event. | ||
| } | ||
|
|
||
| return properties; | ||
| } | ||
|
|
||
| export type { TrafficSource, TrafficSourceProperties }; | ||
| export { getTrafficSource, TRAFFIC_SOURCE_KEY }; | ||
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.
Add test suite for this file
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.
Added
lib/core/traffic-source.test.tsin 9e729f0: 19 tests covering every category, the internal-before-UTM order, subdomain matching, and the session caching rules. 100% line coverage on the file.