nuxt / scripts

Third-Party Scripts Meets Nuxt Developer Experience.
https://scripts.nuxt.com
MIT License
342 stars 45 forks source link

useScriptGoogleAnalytics TS Type Not Working #325

Open benfeather opened 5 days ago

benfeather commented 5 days ago

📚 What are you trying to do?

I'm trying to use the googleTagManager registry script and the useScriptGoogleAnalytics composable.

I have the registry script set up like so:

modules: [
    '@nuxt/scripts',
],

scripts: {
    registry: {
        googleTagManager: {
            id: process.env.GOOGLE_TAG_MANAGER_ID ?? '',
        },
    },
},

In my app.vue file, I'm using the composable like so:

const { proxy } = useScriptGoogleAnalytics()

proxy.gtag('consent', 'default', {
    ad_personalization: 'denied',
    ad_storage: 'denied',
    ad_user_data: 'denied',
    analytics_storage: 'denied',
})

But the gtag type shows an error in my IDE: "Expected 2 arguments, but got 3."

However, the modules/composable works as expected (I have tested it with the tag assistant).

How can I get the types working?

🔍 What have you tried?

No response

ℹī¸ Additional context

No response

benfeather commented 4 days ago

This is my current workaround, but ideally, it wouldn't be required:

npm i -D @types/gtag.js

export function useGtag() {
    const { proxy } = useScriptGoogleAnalytics()

    return {
        gtag: proxy.gtag as Gtag.Gtag,
    }
}