Skip to content

Commit

Permalink
Add Matomo open web analytics (#3405)
Browse files Browse the repository at this point in the history
This adds the possibility for a site to use the Matomo open web analytics (a.k.a. Piwik) for their web analytics instead of, or along with, Google Analytics. It can be enabled in the core config by setting the two required parameters `config.matomo_analytics_server` and `config.matomo_analytics_site_id`. 

It works analogue to the code for enabling Google Analytics and as such is pretty straightforward.
  • Loading branch information
brigriffin authored and parndt committed Nov 6, 2018
1 parent 4f3e43c commit 13048b1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/app/views/refinery/_head.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<%= yield :stylesheets %>
<%= render '/refinery/google_analytics' %>
<%= render '/refinery/matomo_analytics' %>
<%= javascript_include_tag 'modernizr-min' %>
18 changes: 18 additions & 0 deletions core/app/views/refinery/_matomo_analytics.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% server = Refinery::Core.matomo_analytics_server.to_s.strip %>
<% site_id = Refinery::Core.matomo_analytics_site_id.to_s.strip %>
<% unless local_request? or admin? or server =~ /^(analytics.example.org)?$/ or site_id =~ /^$/ -%>
<!-- Matomo -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//<%= server %>/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '<%= 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 -->
<% end -%>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ Refinery::Core.configure do |config|
# Google Analytics are made.
# config.google_analytics_page_code = <%= Refinery::Core.google_analytics_page_code.inspect %>

# This activates Matomo open web analytics tracking within your website. If the server config is
# left blank or set to analytics.example.org then the javascript tracking code will not be loaded.
# config.matomo_analytics_server = <%= Refinery::Core.matomo_analytics_server.inspect %>
# config.matomo_analytics_site_id = <%= Refinery::Core.matomo_analytics_site_id.inspect %>

# Enable/disable authenticity token on frontend
# config.authenticity_token_on_frontend = <%= Refinery::Core.authenticity_token_on_frontend.inspect %>

Expand Down
5 changes: 4 additions & 1 deletion core/lib/refinery/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Core
include ActiveSupport::Configurable

config_accessor :rescue_not_found, :base_cache_key, :site_name,
:google_analytics_page_code, :authenticity_token_on_frontend,
:google_analytics_page_code, :matomo_analytics_server,
:matomo_analytics_site_id, :authenticity_token_on_frontend,
:javascripts, :stylesheets, :mounted_path,
:force_ssl, :backend_route,
:visual_editor_javascripts, :visual_editor_stylesheets,
Expand All @@ -13,6 +14,8 @@ module Core
self.base_cache_key = :refinery
self.site_name = "Company Name"
self.google_analytics_page_code = "UA-xxxxxx-x"
self.matomo_analytics_server = "analytics.example.org"
self.matomo_analytics_site_id = "123"
self.authenticity_token_on_frontend = false
self.javascripts = []
self.stylesheets = []
Expand Down

0 comments on commit 13048b1

Please sign in to comment.