#207: add ga support

This commit is contained in:
SleepWalker 2016-10-09 21:23:00 +03:00
parent 666f5f218a
commit 3d98747c42
4 changed files with 46 additions and 25 deletions

View File

@ -1,3 +0,0 @@
{
"apiHost": "https://dev.account.ely.by"
}

4
config/template.env.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
apiHost: 'https://dev.account.ely.by',
ga: {id: 'UA-XXXXX-Y'}
};

View File

@ -22,5 +22,23 @@
<% for (var chunk in htmlWebpackPlugin.files.chunks) { %> <% for (var chunk in htmlWebpackPlugin.files.chunks) { %>
<script src="<%= htmlWebpackPlugin.files.chunks[chunk].entry %>"></script> <script src="<%= htmlWebpackPlugin.files.chunks[chunk].entry %>"></script>
<% } %> <% } %>
<% if (htmlWebpackPlugin.options.ga) {
if (!htmlWebpackPlugin.options.ga.id) {
throw new Error('ga.id is required to enable google analytics');
}
%>
<!-- Google Analytics -->
<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),
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');
ga('create', '<%- htmlWebpackPlugin.options.ga.id %>', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<% } %>
</body> </body>
</html> </html>

View File

@ -13,6 +13,13 @@ const vendor = Object.keys(require('./package.json').dependencies);
const rootPath = path.resolve('./src'); const rootPath = path.resolve('./src');
var config = {};
try {
config = require('./config/env.js');
} catch (err) {
console.error('\n\n===\nCan not find config/env.js. Running with defaults\n===\n\n', err);
}
/** /**
* TODO: https://babeljs.io/docs/plugins/ * TODO: https://babeljs.io/docs/plugins/
* TODO: отдельные конфиги для env (аля https://github.com/davezuko/react-redux-starter-kit) * TODO: отдельные конфиги для env (аля https://github.com/davezuko/react-redux-starter-kit)
@ -110,7 +117,8 @@ var webpackConfig = {
inject: false, inject: false,
minify: { minify: {
collapseWhitespace: isProduction collapseWhitespace: isProduction
} },
ga: config.ga
}), }),
new webpack.ProvidePlugin({ new webpack.ProvidePlugin({
// window.fetch polyfill // window.fetch polyfill
@ -237,19 +245,12 @@ if (isProduction) {
} }
if (!isProduction && !isTest) { if (!isProduction && !isTest) {
var config;
try {
config = require('./config/dev.json');
} catch (err) {
console.error('\n\n===\nPlease create dev.json config under ./config based on template.dev.json\n===\n\n');
throw err;
}
webpackConfig.plugins.push( webpackConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(), new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin() new webpack.NoErrorsPlugin()
); );
if (config.apiHost) {
webpackConfig.devServer = { webpackConfig.devServer = {
host: 'localhost', host: 'localhost',
port: 8080, port: 8080,
@ -263,6 +264,7 @@ if (!isProduction && !isTest) {
inline: true, inline: true,
historyApiFallback: true historyApiFallback: true
}; };
}
} }
if (isDockerized) { if (isDockerized) {