title: React SPA - GTM Analytics React-Helmet Previous Page Title
body: <p>I am using Google Tag Manager to push pageview events to the datalayer for Analytics tracking. This is happening in <strong>componentDidMount()</strong> (and sometimes <strong>componentWillReceiveProps()</strong> if I am listening for query string parameter changes with the location prop using withRouter).</p>
  
  <p>I am using <a href="https://github.com/nfl/react-helmet" rel="noreferrer">react-helmet</a> to dynamically update my title and other tags as components change. </p>
  
  <p>I have noticed an issue that I am getting the improper page title in Analytics. It's frequently showing the page title of the previous page, instead of the one I am currently on. It appears that react-helmet is not updating the  tag until after <strong>componentDidMount()</strong>.</p>
  
  <p><strong>Page View Event Function Example</strong></p>
  
  <p><em>My analytics tag in GTM is called every time this event is pushed to the data layer</em></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const firePageViewEvent = () =&gt; {
        console.log("Pageview event fired (from tracking script)");
  
        if (window &amp;&amp; window.dataLayer) {
            console.log("window and dataLayer exist, pushing pageview event.");
            let dataLayer = window.dataLayer || [];
            dataLayer.push({
                'event': 'reactPageViewEvent'
            });
        } else {
            console.log("window or dataLater does not exist, cannot push pageview event.");
        }
  };</code></pre>
  </div>
  </div>
  </p>