-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Plausible (use sendBeacon, more events)
- Loading branch information
Showing
5 changed files
with
118 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "src/helpers/plausible-tracker-sendbeacon"] | ||
path = src/helpers/plausible-tracker-sendbeacon | ||
url = https://github.com/orgrosua/plausible-tracker.git |
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
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 |
---|---|---|
@@ -1,39 +1,86 @@ | ||
import * as Sentry from "@sentry/react" | ||
import Plausible, { EventOptions, PlausibleOptions } from "plausible-tracker" | ||
// use client from PR [0] | ||
// until plausible-tracker supports navigator.sendBeacon [1] | ||
// and doesn't break target="_blank" [2] | ||
// [0] https://github.com/plausible/plausible-tracker/pull/54 | ||
// [1] https://github.com/plausible/plausible-tracker/issues/12 | ||
// [2] https://github.com/plausible/plausible-tracker/issues/12 | ||
import Plausible, { | ||
EventOptions, | ||
PlausibleOptions, | ||
} from "./plausible-tracker-sendbeacon/src" | ||
|
||
export enum PlausibleEvent { | ||
LoginClick = "Login click", // User clicked Login button | ||
AuthFailed = "Authentication failed", // Authentication after clicking Login button failed | ||
AuthError = "Authentication error", // Authentication error reported by Spotify after redirection | ||
Authenticated = "Authenticated", // User entered page already authenticated (i.e. after redirect from Spotify or page reload) | ||
Logout = "Logout", // User clicked Logout button | ||
PlaylistsUpdated = "Playlists updated", // Playlists updated successfully | ||
} | ||
|
||
// not exported in plausible-tracker | ||
type TrackEvent = ( | ||
eventName: string, | ||
options?: EventOptions, | ||
eventData?: PlausibleOptions, | ||
) => void | ||
|
||
let _trackPlausibleEvent: TrackEvent | undefined | ||
|
||
export const trackPlausibleEvent: TrackEvent = ( | ||
eventName: string, | ||
options?: EventOptions, | ||
eventData?: PlausibleOptions, | ||
) => { | ||
try { | ||
if (!_trackPlausibleEvent) setupPlausible() | ||
_trackPlausibleEvent && _trackPlausibleEvent(eventName, options, eventData) | ||
} catch (e) { | ||
Sentry.captureException(e) | ||
} | ||
} | ||
|
||
export default function setupPlausible() { | ||
if (import.meta.env.VITE_PLAUSIBLE_DOMAIN) { | ||
const plausible = Plausible({ | ||
domain: import.meta.env.VITE_PLAUSIBLE_DOMAIN, | ||
apiHost: | ||
import.meta.env.VITE_PLAUSIBLE_API_HOST || "https://plausible.io", | ||
trackLocalhost: true, | ||
useSendBeacon: true, | ||
}) | ||
plausible.enableAutoPageviews() | ||
// breaks target="_blank" https://github.com/plausible/plausible-tracker/issues/12 | ||
//plausible.enableAutoOutboundTracking() | ||
plausible.enableAutoOutboundTracking() | ||
_trackPlausibleEvent = plausible.trackEvent | ||
} | ||
} | ||
|
||
export const trackPlausibleEvent = ( | ||
eventName: PlausibleEvent, | ||
eventData?: EventOptions, | ||
options?: PlausibleOptions, | ||
) => { | ||
try { | ||
if (!_trackPlausibleEvent) setupPlausible() | ||
_trackPlausibleEvent && _trackPlausibleEvent(eventName, eventData, options) | ||
} catch (e) { | ||
Sentry.captureException(e) | ||
} | ||
} | ||
|
||
const floorToFirstDigit = (n: number) => { | ||
if (n === 0) return 0 | ||
const x = 10 ** Math.floor(Math.log10(n)) | ||
return Math.floor(n / x) * x | ||
} | ||
|
||
const formatRoundPercent = (n: number) => { | ||
if (n <= 0) return "0%" | ||
if (n >= 1) return "100%" | ||
return `<${Math.ceil(n / 0.1) * 10}%` | ||
} | ||
|
||
export const trackPlausibleEventPlaylistsUpdated = ( | ||
replacedPlaylistsCount: number, | ||
replacedTrackCount: number, | ||
totalTrackCount: number, | ||
selectionCategories: { [name: string]: string }, | ||
) => { | ||
trackPlausibleEvent(PlausibleEvent.PlaylistsUpdated, { | ||
props: { | ||
"count-playlists": floorToFirstDigit(replacedPlaylistsCount), | ||
"count-tracks": floorToFirstDigit(replacedTrackCount), | ||
"percent-selected": formatRoundPercent( | ||
replacedTrackCount / totalTrackCount, | ||
), | ||
...selectionCategories, | ||
}, | ||
}) | ||
} |