diff --git a/app/assets/javascripts/matomo.js b/app/assets/javascripts/matomo.js new file mode 100644 index 0000000..fc1afca --- /dev/null +++ b/app/assets/javascripts/matomo.js @@ -0,0 +1,22 @@ +var _paq = window._paq || []; + +document.addEventListener('DOMContentLoaded', function() { + const { matomoUrl: url, matomoSite: site } = document.documentElement.dataset + console.log(url, site); + if ( url == undefined || site == undefined ) return; + + _paq.push(['trackPageView']); + _paq.push(['enableLinkTracking']); + _paq.push(['setTrackerUrl', url]); + _paq.push(['setSiteId', site]); + + const d = document, + g = d.createElement('script'), + s = d.getElementsByTagName('script')[0] + + g.type = 'text/javascript' + g.async = true + g.defer = true + g.src = url + s.parentNode.insertBefore(g, s) +}) diff --git a/app/lib/matomo.rb b/app/lib/matomo.rb new file mode 100644 index 0000000..4a142bb --- /dev/null +++ b/app/lib/matomo.rb @@ -0,0 +1,24 @@ +class Matomo + module Helpers + def matomo_tag + config = Matomo + return unless config.enabled? + render partial: 'matomo', locals: { config: config } + end + end + + cattr_reader :url, :path, :site + + def self.load + @@url = ENV.fetch 'MATOMO_URL' + @@site = ENV.fetch 'MATOMO_SITE' + @@disabled = ENV['MATOMO_DISABLED'] + end + + def self.enabled? + self.url && self.site && @@disabled.nil? + end + + ActionView::Base.include Helpers + self.load +end diff --git a/app/views/application/_matomo.erb b/app/views/application/_matomo.erb new file mode 100644 index 0000000..0352703 --- /dev/null +++ b/app/views/application/_matomo.erb @@ -0,0 +1,4 @@ + + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ddaa028..25a532b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,5 +1,6 @@ - +<% data = Matomo.enabled? ? { matomo_url: Matomo.url, matomo_site: Matomo.site } : {} %> +<%= content_tag :html, data: data do %> CryptCheck <%= stylesheet_link_tag 'application', media: 'all' %> @@ -8,8 +9,9 @@ <%= yield :head %> - <%= render partial: 'headers' %> - <%= render partial: 'flash' %> - <%= yield %> + <%= render partial: 'headers' %> + <%= render partial: 'flash' %> + <%= yield %> + <%= render partial: 'matomo', locals: { config: Matomo } if Matomo.enabled? %> - +<% end %> diff --git a/config/environments/production.rb b/config/environments/production.rb index a008b61..3d44dd0 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -25,7 +25,7 @@ Rails.application.configure do config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present? # Compress JavaScripts and CSS. - config.assets.js_compressor = :uglifier + config.assets.js_compressor = Uglifier.new harmony: true # config.assets.css_compressor = :sass # Do not fallback to assets pipeline if a precompiled asset is missed. diff --git a/config/initializers/matomo.rb b/config/initializers/matomo.rb new file mode 100644 index 0000000..c34ceef --- /dev/null +++ b/config/initializers/matomo.rb @@ -0,0 +1 @@ +require 'matomo' diff --git a/config/puma.rb b/config/puma.rb index 2e621cd..eed08f7 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -1,21 +1,35 @@ -threads_count = ENV.fetch 'RAILS_MAX_THREADS', 5 -threads threads_count, threads_count +threads_count = ENV['RAILS_THREADS'] +if threads_count + min_threads_count = max_threads_count = threads_count +else + min_threads_count = ENV.fetch 'RAILS_MIN_THREADS', 5 + max_threads_count = ENV.fetch 'RAILS_MAX_THREADS', 5 +end +threads min_threads_count, max_threads_count env = ENV.fetch 'RAILS_ENV', 'development' environment env -workers ENV.fetch('WORKER', 4).to_i +ROOT = Dir.pwd + +workers = ENV['RAILS_WORKERS']&.to_i +workers ||= 4 if env == 'production' +workers workers + +bind = ENV['RAILS_LISTEN'] +port = ENV['RAILS_PORT'] + +puts env: env if env == 'production' - listen = ENV.fetch('LISTEN') { 'unix://' + File.join(Dir.pwd, 'tmp/sockets/puma.sock') } - port = ENV['PORT'] + bind ||= 'unix://' + File.join(ROOT, 'tmp/sockets/puma.sock') else - listen = ENV['LISTEN'] - port = ENV.fetch 'PORT', 3000 + bind ||= 'tcp://[::]:3000' end -port(port) if port -bind listen if listen +puts(port: port, bind: bind) +port port if port +bind bind if bind -pidfile File.join Dir.pwd, 'tmp/pids/puma.pid' +pidfile ENV['PUMA_PID'] || File.join(ROOT, 'tmp/pids/puma.pid') plugin :tmp_restart