I use this piece to initialize GTM in my Next.js app:
const tagManagerArgs = {
gtmId: "GTM-XXXXXX"
};
TagManager.initialize(tagManagerArgs);
But when I am trying to start an app with it I am getting the error:
ReferenceError: document is not defined
at Object.dataScript (/Users/username/work/projectname/node_modules/react-gtm-module/dist/TagManager.js:11:18)
How to solve this?
Wrapping the piece of TagManager initializer with a checker helped:
if (process.browser) {
TagManager.initialize(tagManagerArgs);
}
You can initialize TagManager right when the website has mounted to ensure that the DOM api is available
useEffect(()=>{
TagManager.initialize({gtmId: 'GTM-XXXXXX'})
},[])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With