1

I'd like to add Google Analytics and Google Tag Manager on my web.

I'm trying to separate it as stages. (dev, prod)

_document.tsx

import Document, {Head, Main, NextScript } from 'next/document';
export default class MyDocument extends Document{
  render(){
    return (
      <html>
        <Head>
          {/* ... */}
          <script async src="http://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
          {/* ... */}
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </html>
    );
  }
}

What I expect

<script async src=`http://www.googletagmanager.com/gtag/js?id=${process.env.GTM_CONTAINER_ID}`></script>

I wanna use like above that.

0

1 Answer 1

3

Just like normal JSX, you use curly braces to embed a JavaScript expression in an attribute.

<script async src={`http://www.googletagmanager.com/gtag/js?id=${process.env.GTM_CONTAINER_ID}`}/>
Sign up to request clarification or add additional context in comments.

2 Comments

I've just forgotten it temporally. Thank you :)
No worries. I had the same question before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.