Home>

For some reason, Google Analytics is not reflected.

We are building a SPA site with React.

Because of SPA, we recognize that it is necessary to rewrite and send gtag config every time the routing is changed, and we handle them incomponentDidUpdate.

<head>
  <!-Global site tag (gtag.js)-Google Analytics->
  <script async src = "https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag () {dataLayer.push (arguments);}
    gtag ('js', new Date ());
    gtag ('config', 'GA_TRACKING_ID');
  </script>
componentDidUpdate (prevProps) {
  const gtag = window.gtag
  if (prevProps.location.pathname === this.props.location.pathname) {
    return;
  }
  if (prevProps.history.action === 'PUSH'&&typeof (gtag) === 'function') {
    console.log ('in func')
    gtag ('config', 'GA_TRACKING_ID', {
      'page_location': window.location.href,
      'page_path': this.props.location.pathname,
    });
  }
}


For the"GA_TRACKING_ID"above, all your tracking IDs are included. I've confirmed a lot, so there is no doubt.
Also,console.log ("in func")described in"App.jsx"can be confirmed from the console at the timing of page transition.
Regardingthis.props.location.pathname, we have confirmed that the URL of the current page has been read correctly. (For example,"/React/react-react-router-usecase")

I checked the network of the chrome dev tool,

  • analytics.js
  • https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID
  • _utm.gif? Whatcha

It turns out that there are communication with ga-related things.

The most strange thing was that even with SPA, index.html should be read at least once, and I thought the count would run at that time. However, in reality, it does not appear in real time on the console screen of the GA, and the status shows that there is no data received in the last 48 hours.

What else can be considered as reasons why communication with GA has failed?
Please help.

  • Answer # 1

    After about 3 days from registration, it is displayed without any problems.
    I apologize to you for a fuss over.

    My 3 day worries. . .