创建后有sdk集成相关的id.
import { initializeApp } from 'firebase/app'
import { getAnalytics, logEvent } from "firebase/analytics"
export default defineNuxtPlugin(nuxtApp => {
const config = useRuntimeConfig()
const firebaseConfig = {
apiKey: "***",
projectId: "***",
appId: "***",
measurementId: "***"
}
const app = initializeApp(firebaseConfig)
const analytics = getAnalytics(app)
return {
provide: {
}
}
})
文件中的client表示的是,此插件只会在客户端生效,nuxt3是ssr框架,在plugin目录下的插件即会在server端执行,也会在client端执行。文件名中有client就表示此插件只在客户端生效执行。
import { initializeApp } from 'firebase/app'
import { getAnalytics, logEvent } from "firebase/analytics"
export default defineNuxtPlugin(nuxtApp => {
const config = useRuntimeConfig()
***
***
return {
provide: {
logEventFB: (eventName, params) => {
logEvent(analytics, eventName, params)
},
logErrorEventFB: (strCode) => {
logEvent(analytics, "apperror", { strCode: strCode })
},
logClickEventFB: (funName) => {
logEvent(analytics, "click", { funName: funName })
}
}
}
})
使用时按如下方式调用即可。
<script setup>
useNuxtApp().$logEventFB("fortest")
</script>