I'm trying to implement window.data.layer.push in my react application so I can use it with Google Tag Manager but I am unsure where it would go. this is my base app.js file
// import the GTM Module so you can access it
import TagManager from "react-gtm-module";
const tagManagerArgs = {
gtmId: "GTM-P8J3MKF"
};
TagManager.initialize(tagManagerArgs)
const { persistor, store } = configureStore();
const MGMW = () => (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
{/*<LoginScene className='main-background' />*/}
<LayoutBase />
<GlobalModals />
</PersistGate>
</Provider>
);
const rootElement = document.getElementById('app-root');
if (rootElement) render(<MGMW />, rootElement);
export default MGMW;
You must use useEffect hook.
useEffect(() => {
/*script to be executed here*/
}, [])
This is the componentDidMount version of React Hooks. More on the subject here: https://reactjs.org/docs/hooks-effect.html
There is an NPM package that can help you with this: https://www.npmjs.com/package/react-gtm-module