Google Tag Manager plug-in captures incorrect page title #13968
Labels
good first issue
Issue that doesn't require previous experience with Gatsby
help wanted
Issue with a clear description that the community can help with.
status: confirmed
Issue with steps to reproduce the bug that’s been verified by at least one reviewer.
type: bug
An issue or pull request relating to a bug in Gatsby
Description
Google Analytics captures incorrect page title when using Google Tag Manager via gatsby-plugin-google-tagmanager. See solution below.
Steps to reproduce
Expected result
Page navigation (internal and external) would correctly get recorded as page views with the correct titles.
Actual result
Incorrect titles are captured. This is the same issue as discussed here where the title is a page view behind because Helmet hasn't had a chance to update the page title yet.
Environment
Not relevant to the issue but for completeness
Solution
In gatsby-browser.js fire a custom GTM event in onRouteUpdate -
window.dataLayer.push({'event': 'page-change'})
. Note the "page-change" is arbitrary.In GTM create a trigger for that custom event and wire GA (and other relevant plug-ins) to it. Don't wire those plug-ins to the built in page view event nor a custom history change event.
My gatsby-browser.js code is based on the recent PR to the gtag plug-in.
Suggested Changes
At a minimum the README should be updated. It may also make sense to fire the custom event from the plug-in and tell people to wire it in the GTM UI.
The text was updated successfully, but these errors were encountered: