I\'m trying set up Google Analytics on my react site, and have come across a few packages, but none of which has the kind of set up that I have in terms of examples. Was ho
Given that google analytics is loaded and initialised with a tracking id.
Here is a solution for react-router version 4 using the
component to track page views.
{
if (typeof window.ga === 'function') {
window.ga('set', 'page', location.pathname + location.search);
window.ga('send', 'pageview');
}
return null;
}} />
You simply render this component inside the
(but not as a direct child of a
).
What happens is that whenever the location prop changes it causes a re-render of this component (not actually rendering anything) that fire a pageview.