Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add google/matomo analytics #602

Merged
merged 3 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snippet is to make google analytics work with turbolinks. Since turbolinks mask real page refreshes. Code mostly taken from this gist: https://gist.github.com/esBeee/545653241530f8f2c2e16371bec56f20

// Google Analytics
if (typeof gtag === 'function') {
return gtag('config', '<%= Rails.application.secrets.google_analytics_token %>', {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of this, rendering erb in a javascript file. Ties the client to close to the server. Maybe a data-attr somewhere, or set a global javascript variable when I am embedding this script in the head etc?

'page_path': window.location.pathname
});
}
// Matomo Analytics
if (window._paq != null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kinda an assumption? Hardly any documentation on this, other then:
http://reed.github.io/turbolinks-compatibility/piwik.html (old resource though?)
and reading through:
https://developer.matomo.org/guides/tracking-javascript-guide

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'] %>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples of these will be like the following:

GOOGLE_ANALYTICS_TOKEN='UA-XXXX1234-1'
MATOMO_URL='analytics.piwikserver.ca/piwik'
MATOMO_SITE_ID=8

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Added them there

matomo_url: <%= ENV['MATOMO_URL'] %>
matomo_site_id: <%= ENV['MATOMO_SITE_ID'] %>

development:
secret_key_base: c0a4bf2c5890d0fa86e1459dd189bc4c5a02f412067b610490885c8c312bf0cea5d988e075761ba7277a8291041c1b2e7cf6c373d4d6f43d4522bc48db76cc1a
Expand Down