352 react ga pageviews #353

Merged
bones7242 merged 5 commits from 352-react-ga-pageviews into master 2018-02-16 16:58:59 +01:00
6 changed files with 58 additions and 61 deletions
Showing only changes of commit 0efc25a5c1 - Show all commits

View file

@ -1,5 +1,5 @@
const Handlebars = require('handlebars'); const Handlebars = require('handlebars');
const { site, analytics, claim: claimDefaults } = require('../config/speechConfig.js'); const { site, claim: claimDefaults } = require('../config/speechConfig.js');
function determineOgTitle (storedTitle, defaultTitle) { function determineOgTitle (storedTitle, defaultTitle) {
return ifEmptyReturnOther(storedTitle, defaultTitle); return ifEmptyReturnOther(storedTitle, defaultTitle);
@ -63,16 +63,16 @@ module.exports = {
const headerBoilerplate = `<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>${site.title}</title><link rel="stylesheet" href="/assets/css/reset.css" type="text/css"><link rel="stylesheet" href="/assets/css/general.css" type="text/css"><link rel="stylesheet" href="/assets/css/mediaQueries.css" type="text/css">`; const headerBoilerplate = `<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>${site.title}</title><link rel="stylesheet" href="/assets/css/reset.css" type="text/css"><link rel="stylesheet" href="/assets/css/general.css" type="text/css"><link rel="stylesheet" href="/assets/css/mediaQueries.css" type="text/css">`;
return new Handlebars.SafeString(headerBoilerplate); return new Handlebars.SafeString(headerBoilerplate);
}, },
googleAnalytics () { // googleAnalytics () {
const googleApiKey = analytics.googleId; // const googleApiKey = analytics.googleId;
const gaCode = `<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ // const gaCode = `<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), // (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) // m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); // })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '${googleApiKey}', 'auto'); // ga('create', '${googleApiKey}', 'auto');
ga('send', 'pageview');</script>`; // ga('send', 'pageview');</script>`;
return new Handlebars.SafeString(gaCode); // return new Handlebars.SafeString(gaCode);
}, // },
addOpenGraph (claim) { addOpenGraph (claim) {
const { ogTitle, ogDescription, showUrl, source, ogThumbnailContentType } = createOpenGraphDataFromClaim(claim, claimDefaults.defaultTitle, claimDefaults.defaultDescription); const { ogTitle, ogDescription, showUrl, source, ogThumbnailContentType } = createOpenGraphDataFromClaim(claim, claimDefaults.defaultTitle, claimDefaults.defaultDescription);
const thumbnail = claim.thumbnail; const thumbnail = claim.thumbnail;
@ -120,28 +120,4 @@ module.exports = {
return new Handlebars.SafeString(`${basicTwitterTags} ${twitterCard}`); return new Handlebars.SafeString(`${basicTwitterTags} ${twitterCard}`);
} }
}, },
ifConditional (varOne, operator, varTwo, options) {
switch (operator) {
case '===':
return (varOne === varTwo) ? options.fn(this) : options.inverse(this);
case '!==':
return (varOne !== varTwo) ? options.fn(this) : options.inverse(this);
case '<':
return (varOne < varTwo) ? options.fn(this) : options.inverse(this);
case '<=':
return (varOne <= varTwo) ? options.fn(this) : options.inverse(this);
case '>':
return (varOne > varTwo) ? options.fn(this) : options.inverse(this);
case '>=':
return (varOne >= varTwo) ? options.fn(this) : options.inverse(this);
case '&&':
return (varOne && varTwo) ? options.fn(this) : options.inverse(this);
case '||':
return (varOne || varTwo) ? options.fn(this) : options.inverse(this);
case 'mod3':
return ((parseInt(varOne) % 3) === 0) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
},
}; };

View file

@ -46,6 +46,7 @@
"prop-types": "^15.6.0", "prop-types": "^15.6.0",
"react": "^16.2.0", "react": "^16.2.0",
"react-dom": "^16.2.0", "react-dom": "^16.2.0",
"react-ga": "^2.4.1",
"react-redux": "^5.0.6", "react-redux": "^5.0.6",
"react-router-dom": "^4.2.2", "react-router-dom": "^4.2.2",
"redux": "^3.7.2", "redux": "^3.7.2",

View file

@ -2,6 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import { BrowserRouter, Route, Switch } from 'react-router-dom'; import { BrowserRouter, Route, Switch } from 'react-router-dom';
import withAnalytics from 'utils/googleAnalytics';
import PublishPage from 'components/PublishPage'; import PublishPage from 'components/PublishPage';
import AboutPage from 'components/AboutPage'; import AboutPage from 'components/AboutPage';
@ -13,12 +14,12 @@ const Root = ({ store }) => (
<Provider store={store}> <Provider store={store}>
<BrowserRouter> <BrowserRouter>
<Switch> <Switch>
<Route exact path="/" component={PublishPage} /> <Route exact path="/" component={withAnalytics(PublishPage)} />
<Route exact path="/about" component={AboutPage} /> <Route exact path="/about" component={withAnalytics(AboutPage)} />
<Route exact path="/login" component={LoginPage} /> <Route exact path="/login" component={withAnalytics(LoginPage)} />
<Route exact path="/:identifier/:claim" component={ShowPage} /> <Route exact path="/:identifier/:claim" component={withAnalytics(ShowPage)} />
<Route exact path="/:claim" component={ShowPage} /> <Route exact path="/:claim" component={withAnalytics(ShowPage)} />
<Route component={FourOhFourPage} /> <Route component={withAnalytics(FourOhFourPage)} />
</Switch> </Switch>
</BrowserRouter> </BrowserRouter>
</Provider> </Provider>

View file

@ -0,0 +1,39 @@
import React from 'react';
import GoogleAnalytics from 'react-ga';
const config = require('../../config/speechConfig.js');
const googleApiKey = config.analytics.googleId;
GoogleAnalytics.initialize(googleApiKey);
const withAnalytics = (WrappedComponent, options = {}) => {
const trackPage = page => {
GoogleAnalytics.set({
page,
...options,
});
GoogleAnalytics.pageview(page);
};
const HOC = class extends React.Component {
componentDidMount () {
const page = this.props.location.pathname;
// track initial page
trackPage(page);
};
componentWillReceiveProps (nextProps) {
const currentPage = this.props.location.pathname;
const nextPage = nextProps.location.pathname;
// track each new page
if (currentPage !== nextPage) {
trackPage(nextPage);
}
};
render () {
return <WrappedComponent {...this.props} />;
};
};
return HOC;
};
export default withAnalytics;

View file

@ -12,8 +12,6 @@
<meta property="og:description" content="Open-source, decentralized image and video sharing." /> <meta property="og:description" content="Open-source, decentralized image and video sharing." />
<!--google font--> <!--google font-->
<link href="https://fonts.googleapis.com/css?family=Roboto:300" rel="stylesheet"> <link href="https://fonts.googleapis.com/css?family=Roboto:300" rel="stylesheet">
<!-- google analytics -->
{{ googleAnalytics }}
</head> </head>
<body id="main-body"> <body id="main-body">
{{{ body }}} {{{ body }}}

View file

@ -1,18 +0,0 @@
<!DOCTYPE html>
<html lang="en" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
<head>
{{ placeCommonHeaderTags }}
<meta property="fb:app_id" content="1371961932852223">
{{#unless claimInfo.nsfw}}
{{{addTwitterCard claimInfo }}}
{{{addOpenGraph claimInfo }}}
{{/unless}}
<!-- google analytics -->
{{ googleAnalytics }}
</head>
<body id="showlite-body">
<script src="/assets/js/assetConstructor.js"></script>
{{{ body }}}
</body>
</html>