I am tryting to add custom data layer snippet (dataLayer.push(❴’event’: “pageview”❵)) to Gatbsy GTM plugin. How do I do it? Anybod help?
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ originalLocation: document.location.protocol + '//' + document.location.hostname + document.location.pathname + document.location.search });
Use a
useEffecthook in your component/page. AuseEffectis an event that is triggered once the DOM tree is loaded (similar tocomponentDidMount,componentDidUpdate, andcomponentWillUnmountlifecycles combined).Note the
typeof window !== 'undefined'condition, a recommended statement when dealing with global objects (such aswindowordocument) in server-side rendering.