Skip to content

Commit

Permalink
Merge pull request #602 from murny/ga/piwik-analytics
Browse files Browse the repository at this point in the history
Add google/matomo analytics
  • Loading branch information
mbarnett authored Apr 4, 2018
2 parents b201f16 + 2e783db commit e325996
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env_deployment_sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ SECRET_KEY_BASE=33d83bc35b707a1f70ac9475cdaabd4224fca0d3edc07e0ce6db13515d1c1e3a
SAML_PRIVATE_KEY=
SAML_CERTIFICATE=
ROLLBAR_TOKEN=
GOOGLE_ANALYTICS_TOKEN=
MATOMO_URL=
MATOMO_SITE_ID=

# Postgres environment variables
PGDATA=/var/lib/postgresql/data/pgdata
Expand Down
12 changes: 12 additions & 0 deletions app/assets/javascripts/jupiter/analytics.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$(document).on('turbolinks:load', function() {
// Google Analytics
if (typeof gtag === 'function') {
return gtag('config', '<%= Rails.application.secrets.google_analytics_token %>', {
'page_path': window.location.pathname
});
}
// Matomo Analytics
if (window._paq != null) {
return _paq.push(['trackPageView']);
}
});
34 changes: 34 additions & 0 deletions app/views/application/_analytics.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<% if Rails.application.secrets.google_analytics_token.present? %>
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= Rails.application.secrets.google_analytics_token %>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
</script>
<!-- End Google Analytics -->
<% end %>

<% if Rails.application.secrets.matomo_url.present? && Rails.application.secrets.matomo_site_id.present? %>
<!-- Matomo -->
<script type="text/javascript">
var _paq = _paq || [];
// tracker methods like "setCustomDimension" should be called before "trackPageView"
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//<%= Rails.application.secrets.matomo_url %>/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', <%= Rails.application.secrets.matomo_site_id %>]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->

<!-- Matomo Image Tracker -->
<noscript>
<p><img src="//<%= Rails.application.secrets.matomo_url %>/piwik.php?idsite=<%= Rails.application.secrets.matomo_site_id %>&amp;rec=1" style="border:0" alt="" /></p>
</noscript>
<!-- End Matomo -->
<% end %>
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

<%= render partial: 'analytics' %>

<%= yield(:head) %>
</head>

Expand Down
3 changes: 3 additions & 0 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ shared:
ezid_default_shoulder: <%= ENV['EZID_DEFAULT_SHOULDER'] %>
ezid_user: <%= ENV['EZID_USER'] %>
ezid_password: <%= ENV['EZID_PASSWORD'] %>
google_analytics_token: <%= ENV['GOOGLE_ANALYTICS_TOKEN'] %>
matomo_url: <%= ENV['MATOMO_URL'] %>
matomo_site_id: <%= ENV['MATOMO_SITE_ID'] %>

development:
secret_key_base: c0a4bf2c5890d0fa86e1459dd189bc4c5a02f412067b610490885c8c312bf0cea5d988e075761ba7277a8291041c1b2e7cf6c373d4d6f43d4522bc48db76cc1a
Expand Down

0 comments on commit e325996

Please sign in to comment.