Next.js Discord

Discord Forum

Google Analytics detected by Google Assistant Tag. But data is not on Analytics Dashboard

Unanswered
Polar bear posted this in #help-forum
Open in Discord
Polar bearOP
Hello, I have my google analytics tag in production in my next project, I can see that it works with the google assistant tag, but when I go to my dashboard, I can't see actually any data.
I have checked:
1. The measurement id I entered in my code is correct and properly set.
2. The data stream URL I set in the dashboard is equal to my production URL

My GoogleAnalytics component at app/GoogleAnalytics.js
'use client';

import { GoogleAnalytics } from '@next/third-parties/google';
import { useEffect, useState, useCallback } from 'react';

export default function Analytics() {
  const [hasConsent, setHasConsent] = useState(false);
  const gaId = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID;

  const checkConsent = useCallback(() => {
    if (typeof window !== 'undefined' && window._iub && window._iub.cs) {
      try {
        const rawIubendaConsent = window._iub.cs.consent;
        const consentPurposes = rawIubendaConsent?.purposes;

        const analyticsConsent = consentPurposes?.[3] === true || 
                                  consentPurposes?.[4] === true || 
                                  consentPurposes?.tracking === true;

        if (analyticsConsent !== hasConsent) {
          setHasConsent(analyticsConsent);
        }
      } catch (error) {
        setHasConsent(false);
      }
    }
  }, [hasConsent]);

  useEffect(() => {
    checkConsent();

    const consentListeners = [
      'consent.changed', 
      'iubenda_consent_given', 
      'consentGiven'
    ];

    consentListeners.forEach(eventName => {
      window.addEventListener(eventName, checkConsent);
    });

    const intervalCheck = setInterval(checkConsent, 3000);

    return () => {
      consentListeners.forEach(eventName => {
        window.removeEventListener(eventName, checkConsent);
      });
      clearInterval(intervalCheck);
    };
  }, [checkConsent]);

  if (!gaId) {
    return null;
  }

  return hasConsent ? <GoogleAnalytics gaId={gaId} /> : null;
}

6 Replies

Polar bearOP
My layout page:
// app/layout.js
import Providers from "./providers/SessionProvider";  

// Custom imports
import "./globals.css";
import Analytics from './GoogleAnalytics';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <Providers>  
          {children}
        </Providers>
      </body>
      <Analytics />
    </html>
  );
}
Do i have to put the <Analytics /> component into <body> here </body> or into <body> <Providers> here </Providers> </body>
Or just to keep where it is
As you can see, I can see events in the Tag Assistant
but still no data on the dashboard.
I know it can take up to 48h to show the data, but I waited for them, and still the data didn't show