diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 0e42d115dd..261b2d2c91 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -13,7 +13,7 @@ def index respond_to do |format| format.html end - end + end def update_admins # Don't let admin remove themselves or they won't be able to manage roles @@ -134,6 +134,11 @@ def update_features_enabled Seek::Config.piwik_analytics_url = params[:piwik_analytics_url] Seek::Config.piwik_analytics_tracking_notice = params[:piwik_analytics_tracking_notice] + Seek::Config.custom_analytics_snippet_enabled = string_to_boolean params[:custom_analytics_snippet_enabled] + Seek::Config.custom_analytics_snippet = params[:custom_analytics_snippet] + Seek::Config.custom_analytics_tracking_notice = params[:custom_analytics_tracking_notice] + Seek::Config.custom_analytics_name = params[:custom_analytics_name] + Seek::Config.doi_minting_enabled = string_to_boolean params[:doi_minting_enabled] Seek::Config.datacite_username = params[:datacite_username] Seek::Config.datacite_password = params[:datacite_password] diff --git a/app/views/admin/features_enabled.html.erb b/app/views/admin/features_enabled.html.erb index 5f5057cbe7..db7fb3abee 100644 --- a/app/views/admin/features_enabled.html.erb +++ b/app/views/admin/features_enabled.html.erb @@ -126,7 +126,7 @@ <%= admin_checkbox_setting(:require_cookie_consent, 1, Seek::Config.require_cookie_consent, - "Add a cookie consent banner and a cookie setting page", "Show a banner asking for consent for the use of cookies. SEEK uses an essential cookie for session handling, and optional cookies for tracking if set below. + "Add a cookie consent banner and a cookie setting page", "Show a banner asking for consent for the use of cookies. SEEK uses an essential cookie for session handling, and optional cookies for tracking if set below. Some cookies might be also set by embedded content. This banner will offer 3 choices: Only necessary, Embedded content, All. Setting up this option will also add a cookie setting page accessible from the footer.") %> @@ -152,6 +152,19 @@ 'Piwik tracking notice', "Warn users about the use of analytics software.") %> + <%= admin_checkbox_setting(:custom_analytics_snippet_enabled, 1, Seek::Config.custom_analytics_snippet_enabled, + "Include a custom analytics snippet", "Adds custom snippet to the page keep track of site traffic.", + onchange: toggle_appear_javascript('custom_analytics_block')) %> + +
+ <%= admin_text_setting(:custom_analytics_name, Seek::Config.custom_analytics_name, + 'Custom analytics name', "The name of the analytics software used for tracking traffic.") %> + <%= admin_textarea_setting(:custom_analytics_snippet, Seek::Config.custom_analytics_snippet, + "Code Snippet", "A custom code snippet, taking care of site traffic.") %> + <%= admin_checkbox_setting(:custom_analytics_tracking_notice, 1, Seek::Config.custom_analytics_tracking_notice, + 'Custom tracking notice', "Warn users about the use of analytics software.") %> +
+ <%= render :partial => "admin/doi_settings" %> <%= render :partial => "admin/zenodo_settings" %> diff --git a/app/views/cookies/consent.html.erb b/app/views/cookies/consent.html.erb index 73b1c6d32d..09c3f507bf 100644 --- a/app/views/cookies/consent.html.erb +++ b/app/views/cookies/consent.html.erb @@ -37,9 +37,9 @@ <%= link_to t('cookies.buttons.embedding'), cookies_consent_path(allow: 'necessary,embedding'), method: :post, class: 'btn btn-default' %> <%= link_to t('cookies.buttons.all'), cookies_consent_path(allow: CookieConsent::OPTIONS.join(',')), - method: :post, class: 'btn btn-default' if (Seek::Config.google_analytics_enabled || Seek::Config.piwik_analytics_enabled) %> + method: :post, class: 'btn btn-default' if Seek::Config.analytics_enabled %> - \ No newline at end of file + diff --git a/app/views/general/_cookies_info.html.erb b/app/views/general/_cookies_info.html.erb index 8a1c72f11a..5911846be5 100644 --- a/app/views/general/_cookies_info.html.erb +++ b/app/views/general/_cookies_info.html.erb @@ -3,6 +3,7 @@ <% analytics = [] %> <% analytics << 'Google Analytics' if Seek::Config.google_analytics_enabled %> <% analytics << 'Matomo' if Seek::Config.piwik_analytics_enabled %> + <% analytics << Seek::Config.custom_analytics_name if Seek::Config.custom_analytics_snippet_enabled %> <% if analytics.any? %> <%= t('cookies.analytics', analytics: analytics.join(' and '), instance_name: Seek::Config.instance_name) %> <% end %> @@ -11,5 +12,5 @@ <% if Seek::Config.privacy_enabled %> See our <%= link_to 'Privacy Policy', privacy_home_path %> for more information. <% end %> - -

\ No newline at end of file + +

diff --git a/app/views/layouts/_cookies_banner.html.erb b/app/views/layouts/_cookies_banner.html.erb index dd3c307a10..1b68cfab6e 100644 --- a/app/views/layouts/_cookies_banner.html.erb +++ b/app/views/layouts/_cookies_banner.html.erb @@ -15,8 +15,8 @@ <%= link_to t('cookies.buttons.embedding'), cookies_consent_path(allow: 'necessary,embedding'), method: :post, class: 'btn btn-default' %> <%= link_to t('cookies.buttons.all'), cookies_consent_path(allow: CookieConsent::OPTIONS.join(',')), - method: :post, class: 'btn btn-primary' if (Seek::Config.google_analytics_enabled || Seek::Config.piwik_analytics_enabled) %> + method: :post, class: 'btn btn-primary' if Seek::Config.analytics_enabled %> - \ No newline at end of file + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index cff6f6db33..fcf1138e77 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -47,6 +47,13 @@ <% if cookie_consent.allow_tracking? %> <%= render partial: 'layouts/piwik' if Seek::Config.piwik_analytics_enabled %> + <% if Seek::Config.custom_analytics_snippet_enabled %> + +
+ <%= raw Seek::Config.custom_analytics_snippet %> +
+ + <% end %> <% if Seek::Config.google_analytics_enabled %> ' + Seek::Config.default :custom_analytics_tracking_notice, true Seek::Config.default :bioportal_api_key,'' Seek::Config.default :project_news_enabled,false Seek::Config.default :project_news_feed_urls,'' @@ -134,7 +138,7 @@ def load_seek_config_defaults! Seek::Config.default :contact_link, '' Seek::Config.default :funding_link, '' - + #Terms and conditions page Settings.defaults[:terms_enabled]= false Seek::Config.default :terms_page, File.read(Rails.root.join('config/default_data/terms_and_conditions_example')) diff --git a/lib/seek/config.rb b/lib/seek/config.rb index b216834f0b..ffcc4484ca 100644 --- a/lib/seek/config.rb +++ b/lib/seek/config.rb @@ -7,7 +7,7 @@ module Fallbacks def instance_admins_name_fallback instance_name end - + def instance_admins_link_fallback instance_link end @@ -529,6 +529,10 @@ def self.read_project_setting_attributes register_encrypted_setting(method) if opts && opts[:encrypt] end + def self.analytics_enabled + google_analytics_enabled || piwik_analytics_enabled || custom_analytics_snippet_enabled + end + def self.schema_org_supported? true end diff --git a/lib/seek/config_setting_attributes.yml b/lib/seek/config_setting_attributes.yml index e23642e9df..6cff135b3f 100644 --- a/lib/seek/config_setting_attributes.yml +++ b/lib/seek/config_setting_attributes.yml @@ -60,6 +60,10 @@ copasi_enabled: external_search_enabled: piwik_analytics_enabled: piwik_analytics_tracking_notice: +custom_analytics_snippet_enabled: +custom_analytics_snippet: +custom_analytics_tracking_notice: +custom_analytics_name: seek_video_link: scales: delete_asset_version_enabled: diff --git a/test/integration/cookie_consent_integration_test.rb b/test/integration/cookie_consent_integration_test.rb index 1dd8608fcb..17bbc00ee0 100644 --- a/test/integration/cookie_consent_integration_test.rb +++ b/test/integration/cookie_consent_integration_test.rb @@ -45,6 +45,20 @@ class CookieConsentIntegrationTest < ActionDispatch::IntegrationTest end end + test 'cookie consent banner shown with tracking option if custom analytics enabled' do + with_config_value(:require_cookie_consent, true) do + with_config_value(:custom_analytics_snippet_enabled, true) do + get root_path + + assert_select '#cookie-banner' do + assert_select 'a[href=?]', cookies_consent_path(allow: 'necessary') + assert_select 'a[href=?]', cookies_consent_path(allow: 'necessary,embedding') + assert_select 'a[href=?]', cookies_consent_path(allow: all_options) + end + end + end + end + test 'cookie consent banner not shown if not required' do with_config_value(:require_cookie_consent, false) do get root_path @@ -138,6 +152,19 @@ class CookieConsentIntegrationTest < ActionDispatch::IntegrationTest end end + test 'custom analytics code not present if only necessary cookies allowed' do + with_config_value(:require_cookie_consent, true) do + with_config_value(:custom_analytics_snippet_enabled, true) do + post cookies_consent_path, params: { allow: 'necessary' } + + get root_path + + assert_equal ['necessary'], CookieConsent.new(cookies).options + assert_select '#custom-tracking-script', count: 0 + end + end + end + test 'matomo analytics code not present if necessary and embedded cookies allowed' do with_config_value(:require_cookie_consent, true) do with_config_value(:piwik_analytics_enabled, true) do @@ -151,6 +178,19 @@ class CookieConsentIntegrationTest < ActionDispatch::IntegrationTest end end + test 'custom analytics code not present if necessary and embedded cookies allowed' do + with_config_value(:require_cookie_consent, true) do + with_config_value(:custom_analytics_snippet_enabled, true) do + post cookies_consent_path, params: { allow: 'necessary,embedding' } + + get root_path + + assert_equal ['necessary', 'embedding'], CookieConsent.new(cookies).options + assert_select '#custom-tracking-script', count: 0 + end + end + end + test 'matomo analytics code present if only all cookies allowed' do with_config_value(:require_cookie_consent, true) do with_config_value(:piwik_analytics_enabled, true) do @@ -164,6 +204,19 @@ class CookieConsentIntegrationTest < ActionDispatch::IntegrationTest end end + test 'custom analytics code present if only all cookies allowed' do + with_config_value(:require_cookie_consent, true) do + with_config_value(:custom_analytics_snippet_enabled, true) do + post cookies_consent_path, params: { allow: all_options } + + get root_path + + assert CookieConsent.new(cookies).allow_tracking? + assert_select '#custom-tracking-script', count: 1 + end + end + end + test 'matomo analytics code present if cookie consent not required' do with_config_value(:require_cookie_consent, false) do with_config_value(:piwik_analytics_enabled, true) do @@ -179,6 +232,21 @@ class CookieConsentIntegrationTest < ActionDispatch::IntegrationTest end end + test 'custom analytics code present if cookie consent not required' do + with_config_value(:require_cookie_consent, false) do + with_config_value(:custom_analytics_snippet_enabled, true) do + post cookies_consent_path, params: { allow: 'necessary' } + + get root_path + + cookie_consent = CookieConsent.new(cookies) + assert_equal ['necessary'], cookie_consent.options + assert cookie_consent.allow_tracking? + assert_select '#custom-tracking-script', count: 1 + end + end + end + test 'can access and use cookie consent page as anonymous user' do with_config_value(:require_cookie_consent, true) do with_config_value(:google_analytics_enabled, true) do diff --git a/test/unit/config_test.rb b/test/unit/config_test.rb index a478073237..5edd0ed1b7 100644 --- a/test/unit/config_test.rb +++ b/test/unit/config_test.rb @@ -177,6 +177,16 @@ class ConfigTest < ActiveSupport::TestCase assert_equal 'localhost/piwik/', Seek::Config.piwik_analytics_url end + test 'custom_analytics_enabled' do + assert !Seek::Config.custom_analytics_snippet_enabled + end + test 'custom analytics name' do + assert_equal 'Custom name', Seek::Config.custom_analytics_name + end + test 'custom analytics snippet' do + assert_equal '', Seek::Config.custom_analytics_snippet + end + # homepage settings test 'project_news_enabled' do assert !Seek::Config.project_news_enabled