Skip to content

Analytics

firekitAnalytics wraps Firebase Analytics. All methods are async and return a promise. Analytics is browser-only — calls on the server are silently ignored.

import { firekitAnalytics } from 'svelte-firekit';
// Custom event
await firekitAnalytics.logEvent('sign_up', { method: 'google' });
// E-commerce
await firekitAnalytics.logEvent('purchase', {
value: 29.99,
currency: 'USD',
items: [{ item_id: 'sku-123', item_name: 'Pro Plan' }],
});
// Content engagement
await firekitAnalytics.logEvent('select_content', {
content_type: 'article',
item_id: 'post-abc',
});
// Set the user ID (use your own app's user identifier, not Firebase UID in some cases)
await firekitAnalytics.setUserId('user-123');
// Set user properties
await firekitAnalytics.setUserProperties({
plan: 'pro',
account_type: 'business',
});
await firekitAnalytics.logScreenView('Dashboard');
await firekitAnalytics.logScreenView('Settings');

Track every navigation in SvelteKit by listening to the afterNavigate lifecycle:

src/routes/+layout.svelte
<script lang="ts">
import { afterNavigate } from '$app/navigation';
import { firekitAnalytics } from 'svelte-firekit';
import { page } from '$app/stores';
afterNavigate(() => {
firekitAnalytics.logEvent('page_view', {
page_path: $page.url.pathname,
page_title: document.title,
});
});
</script>

To comply with GDPR and other privacy regulations, update the consent state before logging events:

// Before user gives consent
await firekitAnalytics.setConsent({
analytics_storage: 'denied',
ad_storage: 'denied',
});
// After user grants consent
await firekitAnalytics.setConsent({
analytics_storage: 'granted',
ad_storage: 'granted',
});