I'm trying to learn how to submit a custom event to Google Analytics from a Next.js project. This documentation says I should use sendGTMEvent
:
https://nextjs.org/docs/app/building-your-application/optimizing/third-party-libraries
When I use sendGTMEvent
, it adds an item do the dataLayer
, but nothing actually gets transmitted to Google Analytics. And I don't see any mention of my custom event in Google Analytics. This is the code I have:
/// app/layout.tsx
import GoogleAnalytics from './GoogleAnalytics'
export default async function RootLayout({
children
}: {
children: React.ReactNode
}) {
return (
<html>
<body>
<GoogleAnalytics />
<main className="">{children}</main>
</body>
</html>
)
}
/// app/GoogleAnalytics.tsx
'use client'
import { GoogleTagManager } from '@next/third-parties/google'
const gtmId : string = process.env.NEXT_PUBLIC_GOOGLE_ID ? process.env.NEXT_PUBLIC_GOOGLE_ID : ""
function GA() {
return <GoogleTagManager gtmId={gtmId} />
}
export default GA
/// app/Form.tsx
'use client'
import { sendGTMEvent } from '@next/third-parties/google'
export default function Form() {
setInterval(() => {
if( typeof window === "undefined" ) {
return null
}
dataLayer.push({'event':'MyTest1','conversionValue':25});
sendGTMEvent({ event: 'MyTest2', value: 'hello-world' })
console.log('submitting' )
}, 3000);
return <div>Hello World</div>
}
When I visit my browser at http://localhost:3000
, I see the word Hello World
, which means my website loaded properly. When I open up Chrome Developer Tools>Console
, I see that every 3 seconds, the word submitting
is visible. I can see that the dataLayer
array is growing in length as more records are added to it.
But I don't see any attempts by my javascript to send data to Google. The Chrome Developer Tools>Network
tab show zero attempts to submit anything to Google besides the initial page load.
I tried reading documentation and blogs on the subject, but it's not clear to me what function I am supposed to call to actually "send information" to GA?
Additional Notes:
When I visit Google Analytics and look at Real Time Dashboards, I do not see any mention of MyTest1
or MyTest2
in this widget here:
I tried clicking into each row to drill down further, but couldn't find any mention of MyTest1
or MyTest2