Next.js Discord

Discord Forum
But when i navigato to google tag assistant there is not shown any scripts. Did i miss something?Thanks in advance!","dateCreated":"2023-09-05T09:26:33.000Z","answerCount":0,"author":{"@type":"Person","name":"Mossyrose gall wasp"}}}

Adding Google Tag Manager to Nextjs Using Script

Unanswered
Mossyrose gall wasp posted this in #help-forum
Open in Discord
Mossyrose gall waspOP
Hello, I am trying to add gtm id to my nextjs project i want to sure about that i am doing that right way.

Here is my _document.js:
    <Html lang="en">
      <Head>
        <Script
          id="gtm-script"
          strategy="afterInteractive"
          dangerouslySetInnerHTML={{
            __html: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
              new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
              j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
              'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
              })(window,document,'script','dataLayer','GTM-XXXXXXX');`,
          }}
        />
      </Head>
      <body>
        <Main />
        <NextScript />
        <noscript
          dangerouslySetInnerHTML={{
            __html:
              '<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"height="0" width="0" style="display:none;visibility:hidden"></iframe>',
          }}
        />
      </body>
    </Html>


I also add _app.js file those:
 <Script id="gtm-script" strategy="afterInteractive">
          {`
            (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
            new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
            j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
            'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
            })(window,document,'script','dataLayer','GTM-XXXXXX');
          `}
  </Script>

But when i navigato to google tag assistant there is not shown any scripts. Did i miss something?

Thanks in advance!

0 Replies