Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change GTM ID dynamically? #20

Open
renansantos opened this issue Dec 26, 2023 · 2 comments
Open

How to change GTM ID dynamically? #20

renansantos opened this issue Dec 26, 2023 · 2 comments

Comments

@renansantos
Copy link

renansantos commented Dec 26, 2023

I need to use different GTM IDs acording to my routes.
Is the only possible configuration via defineNuxtConfig or could i configure it using a composable, for example?

@renansantos renansantos changed the title Ho to change GTM ID dynamically? How to change GTM ID dynamically? Dec 26, 2023
@devonik
Copy link

devonik commented Dec 27, 2023

Hey, i had the same request. There are two options. I did the second for my case so not 100% sure the first will work but should.

  1. This package merge the nuxt config gtm: {} with the current nuxt public runtime config public.gtm: {} see in module.ts
const moduleOptions: ModuleOptions = defu(nuxt.options.runtimeConfig.public.gtm, options)

    nuxt.options.runtimeConfig.public.gtm = moduleOptions

You should can just set the id in runtime config instead of the module config. So in runtimeConfig.public.gtm.id and then set the environment variable NUXT_PUBLIC_{whatever} in your environment to overwrite the value

  1. Use the package @gtm-support/vue-gtm directly and create your own plugin so you can initialise the gtm instance in the plugin where you have control on the runtime config

@renansantos
Copy link
Author

Thank you, @devonik !
I don't know yet wich will be better for my project. But i think both solutions will work.
I am still to understand how lots of thinks work in Nuxt.
Would you mind sharing your plugin so i can learn and create my own solution for my needs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants