aizu's Knowledge Base
    • Go to: aizu.my Homepage
    • Recent
    • Tags
    • Register
    • Login

    NextJS - Google Analytic code

    Ref
    1
    1
    77
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      aizu
      last edited by aizu

      File: _app.js

      import Script from "next/script";
      import Head from "next/head"
      
      const GA_MEASUREMENT_ID = 'G-XXXXXXXX'
      
      <Head>
      <Script
        src={`https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`}
        strategy="afterInteractive"
      />
      <Script id="google-analytics" strategy="afterInteractive">
        {`
          window.dataLayer = window.dataLayer || [];
          function gtag(){window.dataLayer.push(arguments);}
          gtag('js', new Date());
          gtag('config', '${GA_MEASUREMENT_ID}');
        `}
      </Script>
      </Head>
      

      Ref: https://hackernoon.com/setting-up-google-analytics-4-in-a-nextjs-project

      1 Reply Last reply Reply Quote 0
      • First post
        Last post