Skip to content

Commit eaffdf3

Browse files
authored
feat: trigger web analytics on leaf download event (#49)
* feat: trigger web analytics on leaf download event * docs: add comment * format
1 parent 4c63954 commit eaffdf3

6 files changed

Lines changed: 68 additions & 3 deletions

File tree

.vitepress/theme/components/download/components/BuildCard.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ import { ApiBuild, getBuildLink } from "../downloadApi";
33
import { Icon } from "@iconify/vue";
44
import { useDateFormat, useTimeAgo } from "@vueuse/core";
55
import { getVerStatus } from "../versionStatus";
6+
import { trackDownloadEvent } from "../umami";
67
78
const props = defineProps<{
89
build: ApiBuild;
910
version: string;
1011
}>();
12+
13+
function onDownloadClick() {
14+
trackDownloadEvent({
15+
version: props.version,
16+
});
17+
}
1118
</script>
1219

1320
<template>
@@ -32,7 +39,7 @@ const props = defineProps<{
3239
({{ useTimeAgo(build.time) }})
3340
</span>
3441

35-
<a class="file" :href="getBuildLink(version, build)">
42+
<a class="file" :href="getBuildLink(version, build)" @click="onDownloadClick">
3643
<Icon icon="lucide:file-box" class="file-icon" />
3744
{{ build.downloads.primary.name }}
3845
</a>

.vitepress/theme/components/download/components/LatestBuild.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22
import { ApiBuild, getBuildLink } from "../downloadApi";
33
import { useTranslation } from "../useTranslation";
44
import { getVerStatus } from "../versionStatus";
5+
import { trackDownloadEvent } from "../umami";
56
67
const props = defineProps<{
78
build: ApiBuild;
89
version: string;
910
}>();
1011
1112
const { t } = useTranslation();
13+
14+
function onDownloadClick() {
15+
trackDownloadEvent({
16+
version: props.version,
17+
});
18+
}
1219
</script>
1320

1421
<template>
@@ -17,7 +24,10 @@ const { t } = useTranslation();
1724
{{ build.build }}
1825
</div>
1926

20-
<a :class="['download-button', 'status-button', getVerStatus(version).cssClass]" :href="getBuildLink(version, build)"
27+
<a
28+
:class="['download-button', 'status-button', getVerStatus(version).cssClass]"
29+
:href="getBuildLink(version, build)"
30+
@click="onDownloadClick"
2131
>{{ t("actions.download") }} Leaf {{ version }}</a
2232
>
2333
</div>

.vitepress/theme/components/download/old/OldDownloadPage.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { branchesToVers, getLatestStable, getVerInfo, Version } from "../version
77
import Markdown from "../../Markdown.vue";
88
import { useData } from "vitepress";
99
import VersionInfoCard from "../components/VersionInfoCard.vue";
10+
import { trackDownloadEvent } from "../umami";
1011
1112
// States
1213
const versions = ref<Array<Version>>([]);
@@ -227,6 +228,14 @@ function refreshVersionInfo() {
227228
}
228229
}
229230
watch(selectedVersion, refreshVersionInfo);
231+
232+
function trackOldDownloadClick() {
233+
if (!selectedVersion.value || !downloadAsset.value) return;
234+
235+
trackDownloadEvent({
236+
version: selectedVersion.value.name,
237+
});
238+
}
230239
</script>
231240

232241
<template>
@@ -380,7 +389,11 @@ watch(selectedVersion, refreshVersionInfo);
380389
</div>
381390

382391
<div class="dl-download-actions">
383-
<a :href="downloadAsset.browser_download_url" class="dl-button primary download">
392+
<a
393+
:href="downloadAsset.browser_download_url"
394+
class="dl-button primary download"
395+
@click="trackOldDownloadClick"
396+
>
384397
<Icon icon="lucide:download" />
385398
<span>{{ t("actions.download") }}</span>
386399
</a>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Umami tracking function type definition.
3+
* @see https://v2.umami.is/docs/tracker-functions#event-data
4+
*/
5+
type UmamiTrack = (eventName: string, data?: Record<string, unknown>) => void;
6+
7+
type UmamiWindow = Window & {
8+
umami?: {
9+
track?: UmamiTrack;
10+
};
11+
};
12+
13+
interface DownloadEventData {
14+
version: string;
15+
}
16+
17+
export function trackDownloadEvent(data: DownloadEventData): void {
18+
if (typeof window === "undefined") return;
19+
20+
const track = (window as UmamiWindow).umami?.track;
21+
if (!track) return;
22+
23+
track("leaf-download", {
24+
version: data.version
25+
});
26+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"devDependencies": {
3+
"@types/semver": "^7.7.1",
34
"@vueuse/core": "^14.2.1",
45
"prettier": "3.8.1",
56
"sass": "^1.98.0",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)