Check out new NextJs Third-Parties Component
After watching the NextJS Conf2023, they released @next/third-parties because of people reporting in the past months after they followed the Google Analytics Documentation, it made bad perfomance issues on Lighhouse.
After following NextJs documentation guide, I was able to fully setup Google Analytics on NextJs 14.
1. To load Google Tag Manager for all routes, include the component directly in your root layout:
app/layout.tsx
import { GoogleTagManager } from '@next/third-parties/google'
export default function RootLayout({children}: {children: React.ReactNode}) {
return (
<html lang="en">
<body>{children}</body>
<GoogleTagManager gtmId="GTM-XYZ" />
</html>
)
}
2. To load Google Tag Manager for a single route, include the component in your page file:
app/page.js
import { GoogleTagManager } from '@next/third-parties/google'
export default function Page() {
return <GoogleTagManager gtmId="GTM-XYZ" />
}
Note: This is the Official NextJs Documentation.
Answer from Anda Hanise on Stack OverflowHow to add Google Tag Manager to a NextJS website
Anyone Explain Google Tag Manager to me Please in Simple
Reddit code has a Google Tag Manager "jail"? : GoogleTagManager
Google Tag Manager / Google Analytics - No Code?
Yes, you can utilise those native DOM events assuming:
-
You can readily identify the elements in question in the page DOM. ie: are there ID attributes on the buttons you want to track?
-
You only want to track these 'simple' events, and not 'custom' events.
-
You do not need to expose any backend data to GTM/GA.
At that level of complexity GTM can more or less be a point and click kind of set-up.
More on reddit.comWhat is Google Tag Manager?
Does Google Tag Manager replace Google Analytics?
Why should I start using Google Tag Manager?
Videos
Check out new NextJs Third-Parties Component
After watching the NextJS Conf2023, they released @next/third-parties because of people reporting in the past months after they followed the Google Analytics Documentation, it made bad perfomance issues on Lighhouse.
After following NextJs documentation guide, I was able to fully setup Google Analytics on NextJs 14.
1. To load Google Tag Manager for all routes, include the component directly in your root layout:
app/layout.tsx
import { GoogleTagManager } from '@next/third-parties/google'
export default function RootLayout({children}: {children: React.ReactNode}) {
return (
<html lang="en">
<body>{children}</body>
<GoogleTagManager gtmId="GTM-XYZ" />
</html>
)
}
2. To load Google Tag Manager for a single route, include the component in your page file:
app/page.js
import { GoogleTagManager } from '@next/third-parties/google'
export default function Page() {
return <GoogleTagManager gtmId="GTM-XYZ" />
}
Note: This is the Official NextJs Documentation.
In your _document.js file which is located in /pages directory add below code. Also if there is no any _document.js file then please create it.
Now add the functionality of Google Tag Manager as :
import Document, { Html, Head, Main, NextScript } from "next/document";
function MyDocument() {
return (
<Html>
<Head>
<script
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=%27+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GOOGLE_TAG_MANAGER_ID');
`,
}}
/>
</Head>
<body>
<noscript
dangerouslySetInnerHTML={{
__html: `
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-xxxx"
height="0" width="0" style="display:none;visibility:hidden"></iframe>
`,
}}
/>
<Main />
<NextScript />
</body>
</Html>
);
}
export default MyDocument;
Now you can check the functionality is working or not and please let me know if it not works. I will surely help.
I'm New to Google Tag Manager