johannschopplich / nuxt-gtag

🔸 Google Analytics & Ads integration made easy
https://developers.google.com/tag-platform/gtagjs
MIT License
302 stars 9 forks source link

Update initalize to have own settings #76

Closed matheusm closed 4 months ago

matheusm commented 5 months ago

Describe the feature

Hi team,

I would like to suggest an enhancement to the gtag configuration, allowing the ability to start with gtag disabled and then dynamically set the options later. This would provide more flexibility and control over the initialization process. Here's a proposed implementation:

  1. nuxt.config.ts:
gtag: {
  enabled: false,
},
  1. composable/gtm.ts:
initialize({
    id: ...,
    tags: [...],
   ...moduleOptions,
})

In my case, user tags are only available when the component is rendered because I need to fetch this data from an API.

Thanks for considering this enhancement!

Best regards, Matheus Martins

Additional information

Final checks

johannschopplich commented 5 months ago

Hi there! Have you checked if that isn't already supported? See the multi-tenancy support guide:

You can even leave the Google tag ID in your Nuxt config blank and set it dynamically later in your application by passing your ID as the first argument to initialize. This is especially useful if you want to use a custom ID for each user or if your app manages multiple tenants.

const { gtag, initialize } = useGtag()

function acceptTracking() {
  initialize('G-XXXXXXXXXX')
}
matheusm commented 5 months ago

Yes, I saw it and tried to implement it, but I encountered some issues. @johannschopplich

Basically, the initialize function adds the script to the page, but it doesn't trigger the pageview base event. Is that correct?

johannschopplich commented 4 months ago

Basically, the initialize function adds the script to the page, but it doesn't trigger the pageview base event. Is that correct?

Yes, it does and also initializes the dataLayer object required for Gtag to work. A pageview event is not emitted by this Nuxt module; this is handled by Gtag and your Analytics configuration.

matheusm commented 4 months ago

One suggestion I have is to enhance the current setup. While the ID and pageview event are initialized in nuxt.config, it would be beneficial to also handle this tracking directly within the initialize function if its possible

By the way, thanks for helping! Best regrads

johannschopplich commented 4 months ago

While the ID and pageview event are initialized in nuxt.config, it would be beneficial to also handle this tracking directly within the initialize function if its possible.

Can't you call the event manually? I want to keep the behavior as opinionless, as possible:

initialize('G-XXXXXXXXXX')
useTrackEvent('pageview')
matheusm commented 4 months ago

Excellent! I am in complete agreement. The inclusion of an illustrative example within the documentation would be quite advantageous. This addition would undoubtedly facilitate comprehension for all users.