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
useEffect
hook in your component/page. AuseEffect
is an event that is triggered once the DOM tree is loaded (similar tocomponentDidMount
,componentDidUpdate
, andcomponentWillUnmount
lifecycles combined).Note the
typeof window !== 'undefined'
condition, a recommended statement when dealing with global objects (such aswindow
ordocument
) in server-side rendering.