Recently I finished a brand new Next.js 13 website using the latest App Router solution (I know that is not production ready but I love to learn new things by real projects) and before to go live I setup a new Google Tag Manager with all the needed tags... but how to add it to the new app directory?
Inspired by the new @vercel/analytics react component, I've added one in my layout.tsx
root component called Analytics
:
// layout.tsx
<html lang="it">
<body>
<Suspense>
<Analytics />
</Suspense>
...
</body>
I wrapped it in a Suspense
boundary to avoid the "deopted into client-side rendering" error for my static pages.
And this is its content:
// Analytics.tsx
"use client"
import { GTM_ID, pageview } from "lib/gtm"
import { usePathname, useSearchParams } from "next/navigation"
import Script from "next/script"
import { useEffect } from "react"
export default function Analytics() {
const pathname = usePathname()
const searchParams = useSearchParams()
useEffect(() => {
if (pathname) {
pageview(pathname)
}
}, [pathname, searchParams])
if (process.env.NEXT_PUBLIC_VERCEL_ENV !== "production") {
return null
}
return (
<>
<noscript>
<iframe
src={`https://www.googletagmanager.com/ns.html?id=${GTM_ID}`}
height="0"
width="0"
style={{ display: "none", visibility: "hidden" }}
/>
</noscript>
<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_ID}');
`,
}}
/>
</>
)
}
The idea is similar to the with-google-tag-manager example with the pages
solution.
Instead of using both the custom _document.tsx
and the _app.tsx
files I added all the configuration in our client
component and thanks to the native Script
tag, GTM is visible everywhere but loaded once after the required scripts.
The next views are triggered on page change event and in the new approach this is achieved monitoring the current pathname
(and the searchParams
too if needed).
And that's all... now you're ready to test and publish your GTM configuration and collect all your user views ;)
Top comments (7)
I continue to get an error at window.dataLayer.push (cannot read properties of undefined). For some reason, with this setup, dataLayer is not a property of window by the time the useEffect runs. You run into this problem?
ah! only getting the error in development because of
process.env.NEXT_PUBLIC_VERCEL_ENV !== 'production'
not set so returnsnull
, but useEffect still runs and tries to firepageview(pathname)
any way, besides checking for window.dataLayer in lib/gtm, to get around that?
you can remove the checking on Analytics component and add the checking on pageView() method instead. so it not break the rule of hook.
export const GTM_ID = process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID;
export const pageview = (url: string) => {
if (process.env.NEXT_PUBLIC_VERCEL_ENV !== "production") {
return;
}
// @ts-ignore
window.dataLayer.push({
event: "pageview",
page: url,
});
};
FTR, using this lib/gtm.js meant I received an error in VS Code /
analytics.tsx
:"lib/gtm"' has no exported member named 'gtmId'. Did you mean 'GTM_ID'?
; replacinggtmId
withGTM_ID
resolved that error inanalytics.tsx
and GTM now loads nicely. Cheers.Hi, thanks I updated the post using the original
GTM_ID
variable.Thanks for the article. Very clear and well-written.
You've referenced
lib/gtm
inAnalytics.tsx
. How do you deal withpageview
in that file? Would be great to get some insight into that part of your solution.Cheers!
Hi, thank you!
You can find it on the current with-google-tag-manager example ๐