Skip to content

Commit 294bbe2

Browse files
Merge pull request #1608 from appwrite/tracking-snippet
add basic events autocapture
2 parents c9f170f + 09c4bc6 commit 294bbe2

File tree

13 files changed

+136
-35
lines changed

13 files changed

+136
-35
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"@sentry/sveltekit": "^8.38.0",
2828
"h3": "^1.13.0",
29+
"posthog-js": "^1.204.0",
2930
"sharp": "^0.33.5"
3031
},
3132
"devDependencies": {

pnpm-lock.yaml

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

src/lib/actions/analytics.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Analytics, type AnalyticsPlugin } from 'analytics';
22
import Plausible from 'plausible-tracker';
33
import { get } from 'svelte/store';
44
import { page } from '$app/stores';
5+
import posthogEvent from 'posthog-js';
56

67
import { ENV } from '$lib/system';
78
import { browser } from '$app/environment';
@@ -54,7 +55,10 @@ const analytics = Analytics({
5455
plugins: [plausible('appwrite.io')]
5556
});
5657

57-
export const trackEvent = async (name: string, data: object = {}) => {
58+
export const trackEvent = async (platforms: {
59+
plausible?: { name: string; data?: object };
60+
posthog?: { name: string };
61+
}) => {
5862
if (!isTrackingAllowed()) {
5963
return;
6064
}
@@ -63,9 +67,15 @@ export const trackEvent = async (name: string, data: object = {}) => {
6367
const path = currentPage.route.id ?? '';
6468

6569
if (ENV.DEV || ENV.PREVIEW) {
66-
console.log(`[Analytics] Event ${name} ${path}`, data);
70+
console.log(`[Analytics] Event`, platforms.plausible, platforms.posthog);
6771
} else {
68-
await analytics.track(name, { ...data, path });
72+
if (platforms.plausible) {
73+
await analytics.track(plausible.name, { ...platforms.plausible.data, path });
74+
}
75+
76+
if (platforms.posthog) {
77+
posthogEvent.capture(platforms.posthog.name);
78+
}
6979
}
7080
};
7181

src/lib/components/AppwriteIn100Seconds.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
<button
1818
on:click={() => {
1919
show = true;
20-
trackEvent('Appwrite in 100 seconds');
20+
trackEvent({
21+
plausible: { name: 'Appwrite in 100 seconds' },
22+
posthog: { name: 'intro-video-btn_hero_click' }
23+
});
2124
}}
2225
class="web-button is-secondary cursor-pointer"
2326
>

src/lib/components/IsLoggedIn.svelte

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
77
export let classes = '';
88
9+
const isLoggedIn = browser && 'loggedIn' in document.body.dataset;
10+
911
function getTrackingEventName() {
10-
return browser
11-
? 'loggedIn' in document.body.dataset
12-
? 'Go to console'
13-
: 'Get started'
14-
: 'Get started';
12+
return browser ? (isLoggedIn ? 'Go to console' : 'Get started') : 'Get started';
1513
}
1614
</script>
1715

1816
<a
1917
class={classNames('web-button web-u-inline-width-100-percent-mobile', classes)}
2018
href={PUBLIC_APPWRITE_DASHBOARD}
21-
on:click={() => {
22-
trackEvent(`${getTrackingEventName()} in header`);
23-
}}
19+
on:click={() =>
20+
trackEvent({
21+
plausible: { name: `${getTrackingEventName()} in header` },
22+
...(isLoggedIn ? {} : { posthog: { name: 'get-started-btn_nav_click' } })
23+
})}
2424
>
2525
<span class="hidden group-[&[data-logged-in]]/body:block">Go to Console</span>
2626
<span class="block group-[&[data-logged-in]]/body:hidden">Get started</span>

src/lib/components/PreFooter.svelte

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<a
2020
href={PUBLIC_APPWRITE_DASHBOARD}
2121
class="web-button is-transparent web-self-center"
22-
on:click={() => trackEvent('Get started in pre footer')}
22+
on:click={() => trackEvent({ plausible: { name: 'Get started in pre footer' } })}
2323
>
2424
<span class="text">Get started</span>
2525
</a>
@@ -47,7 +47,12 @@
4747
<a
4848
href={`${PUBLIC_APPWRITE_DASHBOARD}/register`}
4949
class="web-button is-secondary is-full-width-mobile web-u-cross-child-end"
50-
on:click={() => trackEvent('Get started Free plan')}
50+
on:click={() =>
51+
trackEvent({
52+
plausible: {
53+
name: 'Get started Free plan'
54+
}
55+
})}
5156
>
5257
<span class="text">Get started</span>
5358
</a>
@@ -68,7 +73,12 @@
6873
class="web-button is-full-width-mobile web-u-cross-child-end"
6974
target="_blank"
7075
rel="noopener noreferrer"
71-
on:click={() => trackEvent('Get started Pro plan')}
76+
on:click={() =>
77+
trackEvent({
78+
plausible: {
79+
name: 'Get started Pro plan'
80+
}
81+
})}
7282
>
7383
<!-- <span class="text">Start trial</span> -->
7484
<span class="text">Start building</span>

src/lib/components/ProductsSubmenu.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@
121121
<a
122122
href={product.href}
123123
use:melt={$item}
124-
on:click={() => trackEvent(`${product.name} in products submenu`)}
124+
on:click={() =>
125+
trackEvent({
126+
plausible: {
127+
name: `${product.name} in products submenu`
128+
}
129+
})}
125130
class="group flex gap-3 rounded-xl p-1 text-white outline-none transition-colors focus:bg-white/8"
126131
>
127132
<div

src/lib/components/Technologies.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<a
7676
href={platform.href}
7777
class="web-icon-button web-box-icon has-border-gradient"
78-
on:click={() => trackEvent(`${platform.name} clicked`)}
78+
on:click={() => trackEvent({ plausible: { name: `${platform.name} clicked` } })}
7979
>
8080
<img
8181
src={platform.image}

src/lib/layouts/Main.svelte

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,29 @@
2323
import InitBanner from '$lib/components/InitBanner.svelte';
2424
import { trackEvent } from '$lib/actions/analytics';
2525
import MainNav, { type NavLink } from '$lib/components/MainNav.svelte';
26+
import posthog from 'posthog-js';
2627
2728
export let omitMainId = false;
2829
let theme: 'light' | 'dark' | null = 'dark';
2930
31+
// posthog
32+
let isHeaderExperiment: boolean = false;
33+
let mounted: boolean = false;
34+
35+
onMount(async () => {
36+
if (posthog) {
37+
if (posthog.getFeatureFlag('sticky-navigation_ab-test') === 'control') {
38+
isHeaderExperiment = false;
39+
}
40+
41+
if (posthog.getFeatureFlag('sticky-navigation_ab-test') === 'sticky-nav') {
42+
isHeaderExperiment = true;
43+
}
44+
45+
mounted = true;
46+
}
47+
});
48+
3049
function setupThemeObserver() {
3150
const handleVisibility = () => {
3251
theme = getVisibleTheme();
@@ -97,7 +116,7 @@
97116
return setupThemeObserver();
98117
});
99118
100-
let navLinks: NavLink[] = [
119+
let navLinks: Array<NavLink> = [
101120
{
102121
label: 'Products',
103122
submenu: ProductsSubmenu,
@@ -249,7 +268,11 @@
249268
target="_blank"
250269
rel="noopener noreferrer"
251270
class="web-button is-text web-u-inline-width-100-percent-mobile"
252-
on:click={() => trackEvent('Star on GitHub in header')}
271+
on:click={() =>
272+
trackEvent({
273+
plausible: { name: 'Star on GitHub in header' },
274+
posthog: { name: 'github-stars_nav_click' }
275+
})}
253276
>
254277
<span class="web-icon-star" aria-hidden="true" />
255278
<span class="text">Star on GitHub</span>

src/routes/+layout.server.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/routes/+layout.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getAllChangelogEntries } from './changelog/utils';
2+
import { browser } from '$app/environment';
3+
import posthog from 'posthog-js';
4+
import { PUBLIC_POSTHOG_API_KEY } from '$env/static/public';
5+
6+
export const prerender = true;
7+
export const trailingSlash = 'never';
8+
9+
export const load = async () => {
10+
if (browser) {
11+
posthog.init(PUBLIC_POSTHOG_API_KEY, {
12+
api_host: 'https://eu.i.posthog.com',
13+
person_profiles: 'identified_only',
14+
persistence: 'memory'
15+
});
16+
}
17+
18+
return {
19+
changelogEntries: (await getAllChangelogEntries()).length
20+
};
21+
};

src/routes/+page.svelte

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,12 @@
123123
<a
124124
href="/blog/post/introducing-database-backups"
125125
class="web-hero-banner-button mb-4"
126-
on:click={() => trackEvent('Banner button click')}
126+
on:click={() => trackEvent({ plausible: { name: 'Banner button click' } })}
127127
>
128128
<span class="web-icon-star shrink-0" aria-hidden="true" />
129129
<span class="text-caption shrink-0 font-medium">New</span>
130130
<div class="web-hero-banner-button-sep" />
131131
<span class="text-caption web-u-trim-1">Introducing Database Backups</span>
132-
133132
<span class="web-icon-arrow-right shrink-0" aria-hidden="true" />
134133
</a>
135134
<Hero>
@@ -148,7 +147,11 @@
148147
<a
149148
href={PUBLIC_APPWRITE_DASHBOARD}
150149
class="web-button w-full lg:w-fit"
151-
on:click={() => trackEvent('Get started in hero')}
150+
on:click={() =>
151+
trackEvent({
152+
plausible: { name: 'Get started in hero' },
153+
posthog: { name: 'get-started-btn_hero_click' }
154+
})}
152155
>
153156
Get started
154157
</a>
@@ -159,7 +162,7 @@
159162
</section>
160163
</div>
161164
<div class="mb-12 lg:my-[7.5rem]">
162-
<section class="container web-u-padding-block-0" style="--container-size:78.75rem">
165+
<section class="web-u-padding-block-0 container" style="--container-size:78.75rem">
163166
<div class="web-media-container">
164167
<img
165168
class="block"
@@ -457,7 +460,8 @@
457460
<a
458461
href="/docs/sdks"
459462
class="web-button is-secondary"
460-
on:click={() => trackEvent('Explore all SDKs')}>Explore all SDKs</a
463+
on:click={() => trackEvent({ plausible: { name: 'Explore all SDKs' } })}
464+
>Explore all SDKs</a
461465
>
462466
</section>
463467
</div>

src/routes/products/auth/(components)/SSR.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ async function getLoggedInUser(request) {
159159
<a
160160
href={platform.href}
161161
class="platform flex size-14 items-center justify-center rounded-lg bg-white p-2"
162-
on:click={() => trackEvent(`${platform.name} clicked`)}
162+
on:click={() =>
163+
trackEvent({ plausible: { name: `${platform.name} clicked` } })}
163164
>
164165
<img
165166
src={platform.image}

0 commit comments

Comments
 (0)