Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Google Analytics to static build #80

Merged
merged 1 commit into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions public/googleTagManager.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions public/index.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React from 'react';
import ReactDOMServer from 'react-dom/server';

import { gtmScript } from './googleTagManager';

export default function renderHtml(data, body) {
return `
<!DOCTYPE html>
${ReactDOMServer.renderToStaticMarkup(
<html lang="en">
<head>
{(process.env.NODE_ENV === 'production') &&
<script dangerouslySetInnerHTML={{ __html: gtmScript }} />
}
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Patrick Hooper</title>
Expand Down
5 changes: 4 additions & 1 deletion src/app/Root.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PropTypes from 'prop-types';
import React from 'react';
import { BrowserRouter } from 'react-router-dom';

import './global.css';
import Content from './layout/Content.jsx';
Expand All @@ -17,13 +18,15 @@ export default function Root({ HtmlTag, Router, routerProps }) {
}

Root.propTypes = {
HtmlTag: PropTypes.string.isRequired,
HtmlTag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]).isRequired,
Router: PropTypes.func.isRequired,
routerProps: PropTypes.shape({
location: PropTypes.string,
}),
};

Root.defaultProps = {
HtmlTag: 'div',
Router: BrowserRouter,
routerProps: {},
};
3 changes: 1 addition & 2 deletions src/dev.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'normalize.css';
import React from 'react';
import ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import { BrowserRouter } from 'react-router-dom';

import Root from './app/Root.jsx';

Expand All @@ -11,7 +10,7 @@ delete AppContainer.prototype.unstable_handleError;
function render(Component) {
ReactDOM.render(
<AppContainer>
<Component HtmlTag="div" Router={BrowserRouter} />
<Component />
</AppContainer>,
document.getElementById('app'),
);
Expand Down
17 changes: 16 additions & 1 deletion src/static.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import 'normalize.css';
import PropTypes from 'prop-types';
import React from 'react';
import { StaticRouter } from 'react-router-dom';

import { gtmNoscript } from '../public/googleTagManager';
import renderHtml from '../public/index.jsx';
import Root from './app/Root.jsx';

function Body({ children }) {
return (
<body>
<noscript dangerouslySetInnerHTML={{ __html: gtmNoscript }} />
{children}
</body>
);
}

Body.propTypes = {
children: PropTypes.node.isRequired,
};

export default function render({ path: location, webpackStats }) {
return renderHtml(
webpackStats,
<Root HtmlTag="body" Router={StaticRouter} routerProps={{ location }} />,
<Root HtmlTag={Body} Router={StaticRouter} routerProps={{ location }} />,
);
}