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

Alternative analytics #1730

Merged
merged 10 commits into from
Feb 1, 2024
7 changes: 6 additions & 1 deletion app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
15 changes: 14 additions & 1 deletion app/views/admin/features_enabled.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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.") %>

Expand All @@ -152,6 +152,19 @@
'Piwik tracking notice', "Warn users about the use of analytics software.") %>
</div>

<%= 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')) %>

<div id="custom_analytics_block" class="additional_settings" style="<%= show_or_hide_block(Seek::Config.custom_analytics_snippet_enabled) %>">
<%= 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.") %>
</div>

<%= render :partial => "admin/doi_settings" %>

<%= render :partial => "admin/zenodo_settings" %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/cookies/consent.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
</div>
</div>
</div>
</div>
</div>
5 changes: 3 additions & 2 deletions app/views/general/_cookies_info.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand All @@ -11,5 +12,5 @@
<% if Seek::Config.privacy_enabled %>
See our <%= link_to 'Privacy Policy', privacy_home_path %> for more information.
<% end %>
</p>

</p>
4 changes: 2 additions & 2 deletions app/views/layouts/_cookies_banner.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
</div>
</div>
</div>
</div>
7 changes: 7 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
<!-- Custom snippet -->
<div id="custom-analytics-snippet">
<%= raw Seek::Config.custom_analytics_snippet %>
</div>
<!-- End Custom snippet -->
<% end %>
<% if Seek::Config.google_analytics_enabled %>
<script id="ga-script"/> <!-- so we can check if the ga-script is here -->
<%= analytics_init %>
Expand Down
6 changes: 5 additions & 1 deletion config/initializers/seek_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def load_seek_config_defaults!
Seek::Config.default :piwik_analytics_id_site, 1
Seek::Config.default :piwik_analytics_url, 'localhost/piwik/'
Seek::Config.default :piwik_analytics_tracking_notice, true
Seek::Config.default :custom_analytics_snippet_enabled, false
Seek::Config.default :custom_analytics_name, 'Custom name'
Seek::Config.default :custom_analytics_snippet, '<script id="custom-tracking-script"></script>'
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,''
Expand Down Expand Up @@ -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'))
Expand Down
6 changes: 5 additions & 1 deletion lib/seek/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Fallbacks
def instance_admins_name_fallback
instance_name
end

def instance_admins_link_fallback
instance_link
end
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/seek/config_setting_attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
68 changes: 68 additions & 0 deletions test/integration/cookie_consent_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/unit/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<script id="custom-tracking-script"></script>', Seek::Config.custom_analytics_snippet
end

# homepage settings
test 'project_news_enabled' do
assert !Seek::Config.project_news_enabled
Expand Down
Loading