Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
[#853] Add Google Analytics to templates
Browse files Browse the repository at this point in the history
If present in the Index settings object, the ga code snippet will be
added to generated templates.
  • Loading branch information
brew committed Jan 31, 2017
1 parent da3ceba commit 436a940
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions census/views/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@
</script>
<script data-main="/scripts/site" src="/bower_components/requirejs/require.js"></script>

{% if site.settings.google_analytics_key %}
{% if site.settings.google_analytics_key or google_analytics_key %}
<!-- configured google analytics specific to this deployment -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '{{site.settings.google_analytics_key}}']);
_gaq.push(['_setAccount', '{{ site.settings.google_analytics_key or google_analytics_key}}']);
_gaq.push(['_trackPageview']);

(function() {
Expand Down
4 changes: 2 additions & 2 deletions index/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const i18n = require('i18n-abide');

const godiGetData = require('./metalsmith-godi-getdata');
const godiDataFiles = require('./metalsmith-godi-updatedatafiles');
const godiAncillaryFiles = require('./metalsmith-godi-ancillaryfiles');
const godiIndexSettings = require('./metalsmith-godi-indexsettings'); // Add data from Index settings.
const jsonToFiles = require('metalsmith-json-to-files');

const templatePath = path.join(__dirname, '../census/views/');
Expand Down Expand Up @@ -63,7 +63,7 @@ Metalsmith(__dirname)
.use(godiGetData({domain: domain, year: 2016})) // Populate metadata with data from Survey
.use(jsonToFiles({use_metadata: true}))
.use(godiDataFiles()) // Add file metadata to each entry file populated by json-to-files
.use(godiAncillaryFiles({domain: domain}))
.use(godiIndexSettings({domain: domain})) // Add data from Index settings.
.use(markdown())
.use(permalinks())
.use(paths({property: 'paths', directoryIndex: 'index.html'}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@

const _ = require('lodash');

const debug = require('debug')('metalsmith-godi-ancillaryfiles');
const debug = require('debug')('metalsmith-godi-indexsettings');

const models = require('../census/models');

module.exports = plugin;

/**
* GODI Metalsmith plugin that locates anillary page content present in the
* Index settings object and adds each page as a file to be built by
* Metalsmith.
* GODI Metalsmith plugin that works with the Index setting data loaded from
* the CMS.
*
* Ancillary pages have a key ending with `_page`, e.g. about_page.
* It locates anillary page content present in the Index settings object and
* adds each page as a file to be built by Metalsmith. Ancillary pages have a
* key ending with `_page`, e.g. about_page.
*
* It also adds the Google Analytics code to the metalsmith metadata for use
* in templates.
*
* @return {Function}
*/

function plugin(options) {
return (files, metalsmith, done) => {
let metadata = metalsmith.metadata();

debug('Adding ancillary pages.');
models.Site.findById(options.domain)
.then(site => {
Expand All @@ -40,6 +45,11 @@ function plugin(options) {
metadata.ancillary_pages.push(keyName);
}
});

// Add GA code snippet to metadata
if (indexSettings.google_analytics_key) {
metadata.google_analytics_key = indexSettings.google_analytics_key;
}
done();
})
.catch(err => done(err));
Expand Down

0 comments on commit 436a940

Please sign in to comment.