johannschopplich / nuxt-gtag

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

Consent management #33

Closed kaboume closed 8 months ago

kaboume commented 10 months ago

Environment

Reproduction

See Describe the bug

Describe the bug

Hi, I'm using a consent dialog to activate the analytics tracing.

My nuxt.config :

  gtag: {
    initialConsent: false,
  },

My layout file :

<script setup>
const { gtag, grantConsent, revokeConsent } = useGtag();

const rgpdConsent = useCookie("rgpdConsent", {
  default: () => -1,
});

// Init conset
if (rgpdConsent.value === 1) {
  grantConsent();
}

// Function called  in the dialog consent
const consent = (valeur) => {
  rgpdConsent.value = valeur;
  if (valeur === 1) {
    grantConsent();
  } 
};

When a user accept consent, the grandConsentf() function is called but nothing is sent to Google Analytics.

For information, if I remove the "initialConsent: false," in the nuxt.config, everything works.

did I do something wrong?

Additional context

No response

Logs

No response

johannschopplich commented 10 months ago

It seems like you haven't provided any Gtag ID in your configuration.

Can you please share a reproducible example?

kaboume commented 10 months ago

The gtag ID is in the .env file :

PORT=3000
NITRO_PORT=3000
NUXT_PUBLIC_GTAG_ID=G-XXXXXXX

For information, if I remove the "initialConsent: false," in the nuxt.config, everything works (I can see the tracing in Google Analytics)

johannschopplich commented 10 months ago

The usage seems fine and should work as intended. But without a reproduction, I can't help you. Can please create one?

Please use a template below to create a minimal reproduction: 👉 https://stackblitz.com/github/nuxt/starter/tree/v3-stackblitz 👉 https://codesandbox.io/p/github/nuxt/starter/v3-codesandbox

Edit: Can you ensure that the ID is present inside your open dialog function:

const { gtag } = useRuntimeConfig().public
console.log(gtag)
rcerljenko commented 9 months ago

It happens to me also... pretty much the same setup

jakudlaty commented 9 months ago

It happens to me also.

    gtag: {
        initialConsent: false,
    },

I provide GTAG id from env vars

johannschopplich commented 9 months ago

I have added a console error if no Gtag ID is provided: https://github.com/johannschopplich/nuxt-gtag/commit/59a63b3 (released with v1.1.2).

@kaboume @rcerljenko @jakudlaty Please create a reproducible example. Without it, I'm unable to help, since different deployment configurations means different ways to read your environment configuration. As far as my testing goes, the useHead call correctly adds the gtag.js to the DOM.