React Router BrowserRouter History Push & Google Analytics

FlyingZipper

I Have a Carousel Component and I wanted to change the URL so that google analytics could see to which steps the user went (without reloading the page).

I used React Router with its BrowserRouter component to update the route with the help of this.props.history.push(`#${value}`); and withRouter which successfully updates the URL route. But Google Analytics doesn't seem to see the difference... The website site is at this link If you wanna check the behavior: http://upscale-technology.com

Roy Scheffers

Normally your Google Analytics details are added to the HTML file triggering a page view when it is loaded. When you visit another page of the same website and Google Analytics is implemented there, it will log the new page.

In a single page application, routing is taken care of on the client without page reloads. What you are looking for is a way to let Google Analytics know that it should log a page view when a certain action is taken. It could be a click event, state change or a route change. It's up to you.

To achieve this you can use the React-GA library. Use it in the following way.

// Import it at the top of the file
import ReactGA from 'react-ga';

// Initialize it once, e.g. in your componentDidMount for example.
ReactGA.initialize('UA-000000-01');

// Whenever you want to log anything to Google Analytics, use the below. 
ReactGA.pageview('/carousel/#1');

NOTE:
Be careful where you implement the .pageview([url]) function. If you are not careful it might be triggered on a re-render. This would mean you get false page views in you metrics skewing your analytics.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

History.push works on BrowserRouter but not Router

React router push to history and preserve relative path

Onsubmit and history push is not working react router

React router history.push() not working from history.listen()

React router `this.props.history.push` - history not defined

Integrate Google Analytics with reach router in React application

issue with react-router BrowserRouter in react with typescript?

React router, codes after history.push run?

How to push to History in React Router v4?

react router is only replacing the final route on history.push

React router this.props.history.push , pushing the location but not rendering the component

React router history.push falls back to 404 route

React router history push always route to 404 page

react router doesn't re-render after history push

history.push using react-router-dom

Redux & React Router: Combing dispatch and navigation (history.push)

React router history.push is not adding path from root?

react-router Redirect vs history.push

React router not working as expected when pass state to history.push

How to push to History in React Router v5?

react-router-dom - Difference between link and history.push?

History.push a link to a new tab with react router

history.push() and Custom URL parameters using react-router

Problems that need to be refreshed when using history.push() in react router

React router not rendering the component through history.push()

Is possible to override History.push method of React-Router-Dom

Passing location into children of BrowserRouter - React Router

dynamic basename with BrowserRouter in react-router-dom

react-router-dom <Routes> and <BrowserRouter> errors