How to use react-gtm-module in react js app?

Viewed 7691

In my react app I used react-gtm-module. and I have GTM ID only.

I put the below code in each page router component

import TagManager from 'react-gtm-module';

componentDidMount(){
        const tagManagerArgs = {
            dataLayer: {
                page: '/pagepath'
            },
            gtmId: 'GTM-XXXXXX',
            
        }
        TagManager.initialize(tagManagerArgs)
       
    }

I just put the above code in each route component. By using the above code, in google analytics sometimes the path shows and sometimes not showing in Active Page.

Is it only code I have to put in my code to achieve google analytics track pages or I have to add more else? If yes then exactly what I have to add and where to add?

Definitely, I miss something in my code. But what exactly?

1 Answers

Initialise with just the id in each app, not each page

TagManager.initialize({
  gtmId: gtm_id,
});

and then in each page,

TagManager.dataLayer({
  dataLayer: {
    event: 'pageview',
    pagePath: 'page-path-here',
    pageTitle: 'page-title-here',
  },
});
Related