Added google analytics

This commit is contained in:
2019-05-30 02:55:06 +10:00
parent 3362267967
commit 873c7d241f
5 changed files with 100 additions and 0 deletions
+2
View File
@@ -4,6 +4,7 @@ import Loadable from 'react-loadable';
//other stuff
import Footer from './panels/footer.jsx';
import GA from './utilities/google_analytics.jsx';
//lazy route loading (with error handling)
const LazyRoute = (props) => {
@@ -53,6 +54,7 @@ export default class App extends React.Component {
<div className = 'central'>
<img className='banner' src={'/img/flag_scaled.png'} />
<BrowserRouter>
{ GA.init() && <GA.RouteTracker /> }
<Switch>
<LazyRoute exact path='/' component={() => import('./pages/home.jsx')} />
<LazyRoute path='/signup' component={() => import('./pages/signup.jsx')} />
@@ -0,0 +1,75 @@
import React from 'react';
import PropTypes from 'prop-types';
import ReactGA from 'react-ga';
import { Route } from 'react-router-dom';
import auth from '../../../auth.json';
class GoogleAnalytics extends React.Component {
componentDidMount() {
this.logPageChange(
this.props.location.pathname,
this.props.location.search
);
}
componentDidUpdate({location: prevLocation}) {
const { location: { pathname, search } } = this.props;
const isDifferentPathname = pathname !== prevLocation.pathname;
const idDifferentSearch = search !== prevLocation.search;
if (isDifferentPathname || isDifferentSearch) {
this.logPageChange(pathname, search);
}
}
logPageChange(pathname, search = '') {
const page = pathname + search;
const { location } = window;
ReactGA.set({
page,
location: `${location.origin}${page}`,
...this.props.options
});
ReactGA.pageview(page);
}
render() {
return null;
}
}
GoogleAnalytics.propTypes = {
location: PropTypes.shape({
pathname: PropTypes.string,
search: PropTypes.string,
}).isRequired,
options: PropTypes.object
}
const RouteTracker = () =>
<Route component={GoogleAnalytics} />
const init = (options = {}) => {
const env = auth.env || {};
const isGAEnabled = !!env.REACT_APP_GA_TRACKING_ID;
if (isGAEnabled) {
ReactGA.initialize(
env.REACT_APP_GA_TRACKING_ID, {
debug: env.REACT_APP_GA_DEBUG === 'true',
...options
}
)
}
return isGAEnabled;
}
export default {
GoogleAnalytics,
RouteTracker,
init
}