#Multiple Google Analytics Tags using Nuxt Scripts

3 messages · Page 1 of 1 (latest)

edgy shellBOT
#

Hi @shell vigil. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To address your question about sending events to two different Google Analytics tags using Nuxt Scripts, I'll provide some insights based on the available knowledge sources.

Your approach of using two separate useScriptGoogleAnalytics composables with different keys is correct. This allows you to load multiple instances of the same script, as mentioned in the Registry Scripts guide (https://scripts.nuxt.com/docs/guides/registry-scripts#usage):

You can provide a unique `key` to the script to load multiple instances of the same script:

```js
const { gtag, $script } = useScriptGoogleAnalytics({
  id: 'G-TR58L0EF8P',
})

const { gtag: gtag2, $script: $script2 } = useScriptGoogleAnalytics({
  key: 'gtag2',
  id: 'G-1234567890',
})

However, the issue you're experiencing with events only being handled by one tag might be related to how you're sending the events. To ensure both tags handle the events, you should send the events to each tag separately. Here's a suggested approach:

  1. Create separate proxy objects for each tag:
const { proxy: proxyAA } = useCustomScriptGtag()
const { proxy: proxyBP } = useCustomScriptGtagBp()
  1. When sending events, call both proxies:
function sendEvent(eventName, eventParams) {
  proxyAA.gtag('event', eventName, eventParams)
  proxyBP.gtag('event', eventName, eventParams)
}

This approach ensures that each event is sent to both Google Analytics properties.

It's worth noting that the Google Analytics documentation (https://scripts.nuxt.com/scripts/analytics/google-analytics#usescriptgoogleanalytics) for Nuxt Scripts recommends using the script proxy for interacting with the Google Analytics API:

#
To interact with the Google Analytics API, it's recommended to use script **proxy** (<https://scripts.nuxt.com/docs/guides/key-concepts#understanding-proxied-functions>).

```js
const { proxy } = useScriptGoogleAnalytics()

proxy.gtag('event', 'page_view')

The proxy exposes the gtag and dataLayer properties, and you should use them following Google Analytics best practices.


By following this approach and sending events to both proxies, you should be able to ensure that both of your Google Analytics tags handle the events correctly.

If you're still experiencing issues after implementing these changes, you might want to double-check your Google Analytics configuration and ensure that both properties are set up correctly to receive the events you're sending.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------