9

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
  • 1
    Is it necessary to send the page path? Doesn't GTM always automatically send that info? Commented Sep 23, 2021 at 18:26

1 Answer 1

13

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',
  },
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.