-
Notifications
You must be signed in to change notification settings - Fork 2
feat: score user sessions #55
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
11 commits
Select commit
Hold shift + click to select a range
c4b7617
feat: enableSessionScoring
vitonsky f63e8d4
test: session scoring
vitonsky c367fec
feat: configure link clicks tracking
vitonsky b5e9fa5
test: session score payload
vitonsky 817983c
fix: capture only outbound links
vitonsky f5ef8ba
test: use placeholders in assertions
vitonsky 6737f1f
chore: ensure string
vitonsky dca7f0b
fix: ensure the same origin
vitonsky 5932e02
chore: delete permissions check
vitonsky 5988a81
docs: update readme
vitonsky 581b464
docs: update readme
vitonsky 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 |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |
| "lang": "en_US", | ||
| "skipWords": [ | ||
| "pageview", | ||
| "cpu", | ||
| "pageviews", | ||
| "localhost", | ||
| "uid" | ||
|
|
||
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 was deleted.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| export * from './Plausible'; | ||
| export * from './enableAutoPageviews'; | ||
| export * from './enableAutoOutboundTracking'; | ||
|
|
||
| export * from './plugins'; | ||
| export * from './transformers'; | ||
| export * from './filters'; |
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,100 @@ | ||
| /* eslint-disable spellcheck/spell-checker */ | ||
| // @vitest-environment jsdom | ||
|
|
||
| import { Plausible } from '../Plausible'; | ||
| import { enableAutoOutboundTracking } from './enableAutoOutboundTracking'; | ||
|
|
||
| const mockFetch = vi.fn(); | ||
| globalThis.fetch = mockFetch; | ||
|
|
||
| const locationSpyon = vi.spyOn(window, 'location', 'get'); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| locationSpyon.mockReturnValue(new URL('https://example.org') as any as Location); | ||
|
|
||
| mockFetch.mockReturnValue(new Response('ok')); | ||
|
|
||
| document.body.innerHTML = `<div><a id="internal" href="#foo">internal link</a> <a id="external" href="https://google.com/">external link</a> <a id="nested" href="https://example.com"><span>nested text</span></a></div>`; | ||
| }); | ||
|
|
||
| const plausible = new Plausible({ | ||
| apiHost: 'https://plausible.io', | ||
| domain: 'example.org', | ||
| }); | ||
|
|
||
| enableAutoOutboundTracking(plausible, { captureText: true }); | ||
|
|
||
| test('Ignore clicks by links with the same origin', async () => { | ||
| locationSpyon.mockReturnValue(new URL('https://example.org') as any as Location); | ||
|
|
||
| const anchorElm = document.querySelector('a#internal') as HTMLAnchorElement; | ||
| expect(anchorElm).toBeInstanceOf(HTMLAnchorElement); | ||
|
|
||
| anchorElm.click(); | ||
| expect(mockFetch).not.toHaveBeenCalled(); | ||
|
|
||
| anchorElm.href = 'https://example.org/foo'; | ||
| anchorElm.click(); | ||
| expect(mockFetch).not.toHaveBeenCalled(); | ||
|
|
||
| anchorElm.href = 'https://example.org/nested/foo'; | ||
| anchorElm.click(); | ||
| expect(mockFetch).not.toHaveBeenCalled(); | ||
|
|
||
| anchorElm.href = '/'; | ||
| anchorElm.click(); | ||
| expect(mockFetch).not.toHaveBeenCalled(); | ||
|
|
||
| anchorElm.href = 'https://google.com'; | ||
| anchorElm.click(); | ||
| expect(mockFetch).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('Capture click for simple link', async () => { | ||
| const anchorElm = document.querySelector('a#external') as HTMLAnchorElement; | ||
| expect(anchorElm).toBeInstanceOf(HTMLAnchorElement); | ||
| anchorElm.click(); | ||
|
|
||
| expect(mockFetch).toHaveBeenLastCalledWith('https://plausible.io/api/event', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'text/plain' }, | ||
| body: JSON.stringify({ | ||
| n: 'Outbound Link: Click', | ||
| u: 'https://example.org/', | ||
| d: 'example.org', | ||
| r: null, | ||
| w: 1024, | ||
| h: 0, | ||
| p: JSON.stringify({ | ||
| url: 'https://google.com/', | ||
| text: 'external link', | ||
| }), | ||
| }), | ||
| keepalive: true, | ||
| }); | ||
| }); | ||
|
|
||
| test('Capture click for link text', async () => { | ||
| const spanElm = document.querySelector('a#nested > span') as HTMLSpanElement; | ||
| expect(spanElm).toBeInstanceOf(HTMLSpanElement); | ||
| spanElm.click(); | ||
|
|
||
| expect(mockFetch).toHaveBeenLastCalledWith('https://plausible.io/api/event', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'text/plain' }, | ||
| body: JSON.stringify({ | ||
| n: 'Outbound Link: Click', | ||
| u: 'https://example.org/', | ||
| d: 'example.org', | ||
| r: null, | ||
| w: 1024, | ||
| h: 0, | ||
| p: JSON.stringify({ | ||
| url: 'https://example.com/', | ||
| text: 'nested text', | ||
| }), | ||
| }), | ||
| keepalive: true, | ||
| }); | ||
| }); |
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,31 @@ | ||
| import { Plausible } from '../Plausible'; | ||
| import { | ||
| enableLinkClicksCapture, | ||
| LinkClickPluginConfig, | ||
| } from './enableLinkClicksCapture'; | ||
|
|
||
| export const enableAutoOutboundTracking = ( | ||
| plausible: Plausible, | ||
| config: LinkClickPluginConfig = {}, | ||
| ) => { | ||
| return enableLinkClicksCapture(plausible, { | ||
| eventName: 'Outbound Link: Click', | ||
| ...config, | ||
| filter(url, text) { | ||
| // Skip links with the same origin | ||
| if (typeof window !== 'undefined') { | ||
| try { | ||
| const linkUrl = new URL(url, location.href); | ||
| if (linkUrl.origin === location.origin) return false; | ||
| } catch { | ||
| // Invalid URL, let it through to be filtered by user or tracked | ||
| } | ||
| } | ||
|
|
||
| // Apply user filter | ||
| if (config.filter) return config.filter(url, text); | ||
|
|
||
| return true; | ||
| }, | ||
| }); | ||
| }; | ||
4 changes: 2 additions & 2 deletions
4
src/enableAutoPageviews.ts → src/plugins/enableAutoPageviews.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
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,52 @@ | ||
| import { Plausible } from '../Plausible'; | ||
| import { noop } from '../utils/noop'; | ||
|
|
||
| export type LinkClickPluginConfig = { | ||
| eventName?: string; | ||
| captureText?: boolean; | ||
| filter?: (url: string, text: string) => boolean; | ||
| }; | ||
|
|
||
| export const enableLinkClicksCapture = ( | ||
| plausible: Plausible, | ||
| config: LinkClickPluginConfig = {}, | ||
| ) => { | ||
| if (typeof window === 'undefined') return noop; | ||
|
|
||
| const { | ||
| eventName = 'Link click', | ||
| captureText = false, | ||
| /** | ||
| * When filter returns `false`, the event will be dropped | ||
| */ | ||
| filter, | ||
| } = config; | ||
|
|
||
| const clickCallback = (event: MouseEvent) => { | ||
| // Iterate over all targets to find Anchor element and take its text | ||
| // We do it instead of handle target, since click may appear on nested element | ||
| const linkElement = event | ||
| .composedPath() | ||
| .find((node) => node instanceof HTMLAnchorElement); | ||
| if (!linkElement) return; | ||
|
|
||
| const url = linkElement.href; | ||
| const text = (linkElement.textContent as string | null)?.trim() ?? ''; | ||
|
|
||
| // Skip event | ||
| if (filter && !filter(url, text)) return; | ||
|
|
||
| plausible.sendEvent(eventName, { | ||
| props: { | ||
| url, | ||
| text: captureText ? text : undefined, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| document.addEventListener('click', clickCallback, { capture: true }); | ||
|
|
||
| return function cleanup() { | ||
| document.removeEventListener('click', clickCallback, { capture: true }); | ||
| }; | ||
| }; |
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.