最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use react-gtm-module in react js app? - Stack Overflow

programmeradmin3浏览0评论

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

I put the below code in each page router ponent

import TagManager from 'react-gtm-module';

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

I just put the above code in each route ponent. 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?

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

I put the below code in each page router ponent

import TagManager from 'react-gtm-module';

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

I just put the above code in each route ponent. 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?

Share Improve this question asked Mar 27, 2021 at 8:40 Yuvraj ChaudhariYuvraj Chaudhari 2412 gold badges5 silver badges10 bronze badges 1
  • 1 Is it necessary to send the page path? Doesn't GTM always automatically send that info? – Alex von Brandenfels Commented Sep 23, 2021 at 18:26
Add a ment  | 

1 Answer 1

Reset to default 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',
  },
});
发布评论

评论列表(0)

  1. 暂无评论