Skip to content

Commit 0824368

Browse files
committed
chore: add wip analytics to nuxt
1 parent ea864a6 commit 0824368

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getAnalytics } from 'firebase/analytics'
2+
import { useFirebaseApp } from '../app/composables'
3+
4+
/**
5+
* Retrieves the Firebase analytics instance. **Returns `null` on the server** and when the platform isn't supported.
6+
*
7+
* @param name - name of the application
8+
* @returns the Analytics instance
9+
*/
10+
export function useAnalytics() {
11+
return process.client ? getAnalytics(useFirebaseApp()) : null
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { FirebaseApp } from 'firebase/app'
2+
import { isSupported, initializeAnalytics } from 'firebase/analytics'
3+
import { defineNuxtPlugin, useAppConfig } from '#app'
4+
5+
/**
6+
* Plugin to initialize the analytics module.
7+
* @experimental: NOT YET RELEASED
8+
*/
9+
export default defineNuxtPlugin(async (nuxtApp) => {
10+
const appConfig = useAppConfig()
11+
// @ts-expect-error: not implemented yet
12+
const options = appConfig.vuefireOptions.analytics
13+
const firebaseApp = nuxtApp.$firebaseApp as FirebaseApp
14+
15+
if (await isSupported()) {
16+
initializeAnalytics(firebaseApp, options)
17+
} else {
18+
console.info(
19+
'[nuxt-vuefire]: Firebase Analytics is not supported on this platform.'
20+
)
21+
}
22+
})

0 commit comments

Comments
 (0)