Skip to content

Commit

Permalink
Add Plausible
Browse files Browse the repository at this point in the history
  • Loading branch information
fyrk committed Sep 30, 2023
1 parent a37d023 commit d7ee991
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VITE_PLAUSIBLE_DOMAIN=
VITE_PLAUSIBLE_API_HOST=
3 changes: 3 additions & 0 deletions .github/workflows/deploy-gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jobs:
run: npm install
- name: Build
run: npm run build
env:
VITE_PLAUSIBLE_DOMAIN: ${{ vars.VITE_PLAUSIBLE_DOMAIN }}
VITE_PLAUSIBLE_API_HOST: ${{ vars.VITE_PLAUSIBLE_API_HOST }}
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
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
11 changes: 9 additions & 2 deletions src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import sync from "css-animation-sync"
import Fuse from "fuse.js"
import { debounce } from "lodash"
import { useCallback, useEffect, useState } from "preact/hooks"
import { PlausibleEvent, trackPlausibleEvent } from "../helpers/plausible"
import _TRACKS from "../tracks.json"
import TRACK_INDEX from "../tracks_index.json"
import Match from "./Match"
Expand Down Expand Up @@ -44,8 +45,8 @@ export default function App() {
sync("glitter-bg", "glitter")
}, [])

const [hasEntered, setHasEntered] = useState(false)
const [query, setQuery] = useState(window.location.hash.slice(1))

const [results, setResults] = useState<{
results: FuseResult[]
query: string
Expand Down Expand Up @@ -89,7 +90,13 @@ export default function App() {
spellcheck={false}
enterkeyhint="done"
class="w-full animate-glitter-bg-slow appearance-none rounded-2xl border-4 p-4 font-slab text-2xl uppercase tracking-widest focus:outline-none"
onInput={e => setQuery(e.currentTarget.value)}
onInput={e => {
setQuery(e.currentTarget.value)
if (!hasEntered) {
setHasEntered(true)
trackPlausibleEvent(PlausibleEvent.EnterQuery)
}
}}
onKeyUp={e => e.key === "Enter" && e.currentTarget.blur()}
/>

Expand Down
4 changes: 4 additions & 0 deletions src/app/ShareButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CheckIcon, ShareIcon } from "@heroicons/react/20/solid"
import { ClipboardIcon } from "@heroicons/react/24/outline"
import { useMemo, useState } from "preact/hooks"
import { PlausibleEvent, trackPlausibleEvent } from "../helpers/plausible"

export default function ShareButton() {
const [isCopied, setIsCopied] = useState(false)
Expand Down Expand Up @@ -30,6 +31,9 @@ export default function ShareButton() {
setTimeout(() => setIsCopied(false), 750)
})
}
trackPlausibleEvent(PlausibleEvent.Share, {
props: { type: canShare ? "share" : "copy" },
})
}}
>
<div class="flex items-center gap-1 lg:flex-col lg:gap-0">
Expand Down
1 change: 1 addition & 0 deletions src/helpers/plausible-tracker-sendbeacon
51 changes: 51 additions & 0 deletions src/helpers/plausible.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 {
EnterQuery = "Enter Query",
Share = "Share",
}

// not exported in plausible-tracker
type TrackEvent = (
eventName: string,
options?: EventOptions,
eventData?: PlausibleOptions,
) => void
let _trackPlausibleEvent: TrackEvent | undefined

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()
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) {
console.error(e)
}
}
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { render } from "preact"
import App from "./app/App"
import "./fonts.css"
import setupPlausible from "./helpers/plausible"
import "./index.css"

setupPlausible()

render(<App />, document.getElementById("app"))

0 comments on commit d7ee991

Please sign in to comment.