-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from codediodeio/analytics
analytics: PageView component
- Loading branch information
Showing
16 changed files
with
185 additions
and
23 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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
title: PageView Component | ||
pubDate: 2023-12-23 | ||
description: SvelteFire PageView Component API reference | ||
layout: ../../layouts/MainLayout.astro | ||
--- | ||
|
||
# PageView | ||
|
||
The `PageView` component logs a Google analytics `page_view` event when it is mounted. | ||
|
||
### Slot Props | ||
|
||
- `eventName` - (default: 'page_view') Set the current user as the userId in Google Analytics | ||
- `setUser` - (default: true) Set the current user as the userId in Google Analytics | ||
- `customParams` - (optional) custom parameters to pass to the `signIn` function | ||
|
||
### Layout Example (recommended) | ||
|
||
The most efficient way to integrate Firebase Analytics is to log events from a layout component. This will ensure that every route change is logged, both on the client and server. Make sure to `key` the `PageView` component so that it is re-mounted on every route change. | ||
|
||
```svelte | ||
<!-- +layout.svelte --> | ||
<script lang="ts"> | ||
import { page } from "$app/stores"; | ||
import { PageView } from "sveltefire"; | ||
</script> | ||
<slot /> | ||
{#key $page.route.id} | ||
<PageView /> | ||
{/key} | ||
``` | ||
|
||
### Page Example | ||
|
||
For fine-grained control, you can include `PageView` on a page-by-page basis. This is useful when sending custom parameters. | ||
|
||
|
||
```svelte | ||
<!-- +page.svelte --> | ||
<script lang="ts"> | ||
const myData = { | ||
guild: 'griffindor', | ||
currency: 'gpb' | ||
} | ||
</script> | ||
<PageView eventName="my_page_view" customParams={myData} setUser={false} /> | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<script lang="ts"> | ||
import { logEvent, setUserId, isSupported } from "firebase/analytics"; | ||
import { getFirebaseContext } from "$lib/index.js"; | ||
import { onMount } from "svelte"; | ||
export let eventName = "page_view"; | ||
export let setUser = true; | ||
export let customParams: Record<string, unknown> = {}; | ||
const analytics = getFirebaseContext().analytics!; | ||
const auth = getFirebaseContext().auth!; | ||
onMount(async () => { | ||
if (!(await isSupported())) { | ||
return; | ||
} | ||
if (setUser) { | ||
setUserId(analytics, auth.currentUser?.uid ?? null); | ||
} | ||
logEvent(analytics, eventName, { | ||
page_location: window.location.href, | ||
page_path: window.location.pathname, | ||
page_title: document.title, | ||
...customParams, | ||
}); | ||
}); | ||
</script> |
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,8 +1,8 @@ | ||
<script lang="ts"> | ||
import FirebaseApp from "$lib/components/FirebaseApp.svelte"; | ||
import { db as firestore, auth, rtdb, storage } from "./firebase.js"; | ||
import { db as firestore, auth, rtdb, storage, analytics } from "./firebase.js"; | ||
</script> | ||
|
||
<FirebaseApp {auth} {firestore} {rtdb} {storage}> | ||
<FirebaseApp {auth} {firestore} {rtdb} {storage} {analytics}> | ||
<slot /> | ||
</FirebaseApp> |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script lang="ts"> | ||
import { page } from "$app/stores"; | ||
import PageView from "$lib/components/PageView.svelte"; | ||
</script> | ||
|
||
<slot /> | ||
|
||
<h2>Analytics Layout</h2> | ||
{#key $page.route.id} | ||
<p>Logged analytics for {$page.route.id}</p> | ||
<PageView /> | ||
{/key} |
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,7 @@ | ||
import type { PageServerLoad } from './$types'; | ||
|
||
export const load: PageServerLoad = (async () => { | ||
return { | ||
title: 'Analytics Test 1 - Server Rendered', | ||
}; | ||
}) satisfies PageServerLoad; |
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,21 @@ | ||
<script lang="ts"> | ||
import type { PageData } from './$types'; | ||
import PageView from '$lib/components/PageView.svelte'; | ||
export let data: PageData; | ||
</script> | ||
|
||
<svelte:head> | ||
<title>{data.title}</title> | ||
</svelte:head> | ||
|
||
<h1>{data.title}</h1> | ||
|
||
<p> | ||
This is a test page for analytics. The title is always server rendered. | ||
</p> | ||
|
||
<a href="/analytics-test/client-side">Analytics CSR Test</a> | ||
|
||
|
||
<!-- <PageView /> --> |
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,24 @@ | ||
<script lang="ts"> | ||
import PageView from '$lib/components/PageView.svelte'; | ||
const customParams = { | ||
mood: 'happy' | ||
}; | ||
const title = 'Analytics Test Page 2 - Client Rendered'; | ||
</script> | ||
|
||
<svelte:head> | ||
<title>{title}</title> | ||
</svelte:head> | ||
|
||
<!-- <PageView {customParams} /> --> | ||
|
||
<h1>{title}</h1> | ||
|
||
<p> | ||
This is a test page for analytics. The title is always server rendered. | ||
</p> | ||
|
||
<a href="/analytics-test">Analytics SSR Test</a> | ||
|
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