Skip to content

Commit

Permalink
Merge pull request #24 from sul-dlss/google-analytics
Browse files Browse the repository at this point in the history
Google analytics
  • Loading branch information
jkeck authored Jul 8, 2019
2 parents b092cbf + d811702 commit 5ccd0eb
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ gem 'config'

gem 'okcomputer'

gem 'jquery-rails'

group :production do
gem 'mysql2', '~> 0.5'
gem 'newrelic_rpm'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ GEM
jaro_winkler (1.5.3)
jbuilder (2.9.1)
activesupport (>= 4.2.0)
jquery-rails (4.3.5)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -322,6 +326,7 @@ DEPENDENCIES
dlss-capistrano
honeybadger
jbuilder (~> 2.5)
jquery-rails
listen (>= 3.0.5, < 3.2)
mysql2 (~> 0.5)
newrelic_rpm
Expand Down
62 changes: 62 additions & 0 deletions app/assets/javascripts/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
GoogleAnalytics = (function() {
function GoogleAnalytics() {}

GoogleAnalytics.load = function() {
GoogleAnalytics.analyticsId = GoogleAnalytics.getAnalyticsId();
(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', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', GoogleAnalytics.analyticsId, 'auto');
ga('set', 'anonymizeIp', true);
};

GoogleAnalytics.trackPageview = function(url) {
if (!GoogleAnalytics.isLocalRequest()) {
return ga('send', {
hitType: 'pageview',
page: GoogleAnalytics.getPath()
}
);
}
};

GoogleAnalytics.isLocalRequest = function() {
return GoogleAnalytics.documentDomainIncludes('local');
};

GoogleAnalytics.documentDomainIncludes = function(str) {
return document.domain.indexOf(str) !== -1;
};

GoogleAnalytics.getAnalyticsId = function() {
return $('[data-analytics-id]').data('analytics-id');
};

// Remove the protocol and the host and only return the path with any params
GoogleAnalytics.getPath = function() {
return location.href
.replace(location.protocol + '//' + location.host, '');
};

return GoogleAnalytics;

})();

$(document).on('turbolinks:load', function(){
GoogleAnalytics.load();
if (GoogleAnalytics.analyticsId){
GoogleAnalytics.trackPageview();
}
});
3 changes: 2 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require jquery3
//= require jquery_ujs
//= require activestorage
//= require turbolinks
//= require_tree .
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</head>

<body>
<div data-analytics-id="<%= Settings.GOOGLE_ANALYTICS_ID %>"></div>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOOGLE_ANALYTICS_ID:

0 comments on commit 5ccd0eb

Please sign in to comment.