Skip to content
This repository has been archived by the owner on Dec 23, 2017. It is now read-only.

Commit

Permalink
Merge pull request #863 from jmcarp/feature/history-analytics
Browse files Browse the repository at this point in the history
Feature/history analytics
  • Loading branch information
Noah Manger committed Oct 21, 2015
2 parents a72f370 + 162e189 commit 6f856d0
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 16 deletions.
6 changes: 5 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var uglify = require('gulp-uglify');


var debug = !!process.env.FEC_WEB_DEBUG;
var analytics = !!process.env.FEC_WEB_GOOGLE_ANALYTICS;
var production = ['stage', 'prod'].indexOf(process.env.FEC_WEB_ENVIRONMENT) !== -1;

// TODO(jmcarp) Restore `watch-js`
Expand Down Expand Up @@ -115,7 +116,10 @@ gulp.task('build-js', function() {
opts: {global: true},
debug: debug
})
.transform(preprocessify({DEBUG: debug}))
.transform(preprocessify({
DEBUG: debug,
ANALYTICS: analytics
}))
.transform({global: true}, stringify(['.html']))
.transform(hbsfy)
.bundle()
Expand Down
10 changes: 8 additions & 2 deletions static/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ require('jquery.inputmask/dist/inputmask/jquery.inputmask.numeric.extensions.js'
// Include vendor scripts
require('./vendor/tablist');

var filters = require('./modules/filters.js');
var charts = require('./modules/charts.js');
var charts = require('./modules/charts');
var Search = require('./modules/search');
var toggle = require('./modules/toggle');
var filters = require('./modules/filters');
var helpers = require('./modules/helpers');
var analytics = require('./modules/analytics');

charts.init();

Expand Down Expand Up @@ -187,6 +188,11 @@ $(document).ready(function() {
// perf.bar();
// @endif

// @if ANALYTICS
analytics.init();
analytics.pageView();
// @endif

filters.init();
toggle.init();
});
52 changes: 52 additions & 0 deletions static/js/modules/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

/* global ga */

var URI = require('URIjs');
var _ = require('underscore');

function init() {
(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('set', 'forceSSL', true);
ga('set', 'anonymizeIp', true);
ga('create', 'UA-48605964-22', 'auto');
}

function pageView() {
if (typeof ga === 'undefined') { return; }
var path = document.location.pathname;
if (document.location.search) {
var query = URI.parseQuery(document.location.search);
path += '?' + sortQuery(query);
}
ga('send', 'pageview', path);
}

function sortQuery(query) {
return _.chain(query)
.pairs()
.map(function(pair) {
return [pair[0], _.isArray(pair[1]) ? pair[1] : [pair[1]]];
})
.reduce(function(memo, pair) {
return memo.concat(_.map(pair[1], function(value) {
return [pair[0], value];
}));
}, [])
.sort()
.map(function(pair) {
return pair.join('=');
})
.join('&')
.value();
}

module.exports = {
init: init,
sortQuery: sortQuery,
pageView: pageView
};
2 changes: 2 additions & 0 deletions static/js/modules/election-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var L = require('leaflet');
require('leaflet-providers');

var helpers = require('./helpers');
var analytics = require('./analytics');
var utils = require('./election-utils');

var states = require('../data/us-states-10m.json');
Expand Down Expand Up @@ -222,6 +223,7 @@ ElectionLookup.prototype.search = function(e, opts) {
self.serialized = serialized;
if (opts.pushState) {
window.history.pushState(serialized, null, URI('').query(serialized).toString());
analytics.pageView();
}
}
};
Expand Down
3 changes: 2 additions & 1 deletion static/js/modules/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var $ = require('jquery');
var URI = require('URIjs');
var _ = require('underscore');
var moment = require('moment');
var tabs = require('../vendor/tablist');
var accessibility = require('fec-style/js/accessibility');

Expand All @@ -14,6 +13,7 @@ require('drmonty-datatables-responsive');

var filters = require('./filters');
var helpers = require('./helpers');
var analytics = require('./analytics');

var simpleDOM = 't<"results-info"ip>';

Expand Down Expand Up @@ -231,6 +231,7 @@ function pushQuery(params) {
params = _.extend(query, params);
var queryString = URI('').query(params).toString();
window.history.pushState(params, queryString, queryString || window.location.pathname);
analytics.pageView();
}
}

Expand Down
4 changes: 4 additions & 0 deletions static/js/vendor/tablist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
-----------------------------------------------------------------------------------------
*/

var $ = require('jquery');
var URI = require('URIjs');
var _ = require('underscore');

var events = require('fec-style/js/events');

var analytics = require('../modules/analytics');

// The class for the container div

var $container = '.tab-interface';
Expand Down Expand Up @@ -43,6 +46,7 @@ function show($target, push) {
);
var search = URI('').query(query).toString();
window.history.pushState(query, search, search || window.location.pathname);
analytics.pageView();
}

events.emit('tabs.show.' + value, {$tab: $target, $panel: $panel});
Expand Down
13 changes: 1 addition & 12 deletions templates/layouts/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,7 @@
{% block scripts %}{% endblock %}

{% if use_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('set', 'anonymizeIp', true);
ga('set', 'forceSSL', true);
ga('create', 'UA-48605964-22', 'auto');
ga('send', 'pageview');
</script>
<script id="_fed_an_ua_tag" src="https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js?agency=FEC"></script>
<script id="_fed_an_ua_tag" src="https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js?agency=FEC"></script>
{% endif %}

</body>
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/modules/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

/* global describe, it */

var chai = require('chai');
var expect = chai.expect;

var analytics = require('../../../static/js/modules/analytics');

describe('analytics', function() {
it('sorts query parameters', function() {
var query = {
cycle: [2016, 2014],
sort: 'name',
};
expect(analytics.sortQuery(query)).to.equal('cycle=2014&cycle=2016&sort=name');
});
});

0 comments on commit 6f856d0

Please sign in to comment.