1import React from 'react';
2import { useBasepath } from 'react-static';
3import PropTypes from 'prop-types';
4
5let Analytics = {};
6
7if (typeof document !== 'undefined') {
8 Analytics = require('react-router-ga').default;
9} else {
10 Analytics = React.Fragment;
11}
12
13const GoogleAnalytics = ({ children, ...rest }) => {
14 const basename = `/${useBasepath() || ''}`;
15 if (typeof document !== 'undefined') {
16 // fragment doesn't like it when you try to give it attributes
17 return (
18 <Analytics {...rest} basename={basename}>
19 {children}
20 </Analytics>
21 );
22 }
23 return <Analytics>{children}</Analytics>;
24};
25
26GoogleAnalytics.propTypes = {
27 children: PropTypes.element,
28};
29
30export default GoogleAnalytics;