Giter VIP home page Giter VIP logo

Comments (6)

johannschopplich avatar johannschopplich commented on May 28, 2024 2

Hey there, I've implemented an optional ID parameter for useGtagConsent: 7859fe6

Make sure to set initialConsent to false in the module options. Then, you can fetch your data and initialize a specific Gtag ID:

useGtagConsent(true, { id: "XXX" })

Since no Gtag instance has been initialized until that point, it will do so for the custom ID. Hope that helps!

from nuxt-gtag.

johannschopplich avatar johannschopplich commented on May 28, 2024 1

You are right, there was a bug in my code, which I just fixed.

You won't have to add the Gtag ID to your Nuxt config, but can dynamically enable Gtag in the client.

from nuxt-gtag.

ILLuMiNaTe7 avatar ILLuMiNaTe7 commented on May 28, 2024 1

Thanks for the quick handling. After testing the code it seems you have accidentally removed the import of defineNuxtPlugin.

This causes the following error:

Uncaught ReferenceError: defineNuxtPlugin is not defined
    at plugin.client.mjs?v=2faffd2d:1:1

Thanks for fixing this :)

from nuxt-gtag.

johannschopplich avatar johannschopplich commented on May 28, 2024

We were wondering if its possible to set the GTAG ID Dynamically.

What do you mean by that? At runtime instead of build time? You can adapt the GTAG ID based on the NUXT_PUBLIC_GTAG_ID variable:

# Sets the `gtag.id` module option
NUXT_PUBLIC_GTAG_ID=G-XXXXXXXXXX

from nuxt-gtag.

ILLuMiNaTe7 avatar ILLuMiNaTe7 commented on May 28, 2024

from nuxt-gtag.

ILLuMiNaTe7 avatar ILLuMiNaTe7 commented on May 28, 2024

Hi @johannschopplich ,

I've tested the implementation however I'm encountering a weird issue that may be related to #19 .

For some reason, the script doesn't get injected into the head with your instructions. The only time it gets injected is when I do set the ID in the Nuxt config. However, then it also injects it 2 times. One time with the Nuxt config and another time with the new implementation.

As a result, the first collection for analytics also uses injection originating from Nuxt config. (G-XXXXXXX)

<script type="text/javascript" async="" src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX&amp;l=dataLayer&amp;cx=c"></script>
<script src="https://www.googletagmanager.com/gtag/js?id=G-4DJ1K7LHXI"></script>

This is my implementation:
Nuxt Config:

export default defineNuxtConfig({
  ...
  modules: [
    ...
    'nuxt-gtag'
  ],
  gtag: {
    initialConsent: false
    // id: 'G-XXXXXXXXXX' <<< If this is added it does work except for the first collection.
  }
  ...
})

Plugin:

export default defineNuxtPlugin(async () => {
   // Do some magic to get the GTAG ID:
   useGtagConsent(true, { id: 'G-4MJ1K7LSXM' })
})

from nuxt-gtag.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.