-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 %>', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is kinda an assumption? Hardly any documentation on this, other then: |
||
return _paq.push(['trackPageView']); | ||
} | ||
}); |
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 %>&rec=1" style="border:0" alt="" /></p> | ||
</noscript> | ||
<!-- End Matomo --> | ||
<% end %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'] %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/ualbertalib/jupiter/blob/master/.env_deployment_sample would be a good place to document this. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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