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

I194 google analytics #2453

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
GOOGLE_ANALYTICS_ID: ${{ secrets.GOOGLE_ANALYTICS_ID }}
GOOGLE_ANALYTICS_PROPERTY_ID: ${{ secrets.GOOGLE_ANALYTICS_PROPERTY_ID }}
GOOGLE_ACCOUNT_JSON: ${{ secrets.GOOGLE_ACCOUNT_JSON }}
GOOGLE_FONTS_KEY: ${{ secrets.GOOGLE_FONTS_KEY }}
GOOGLE_OAUTH_PRIVATE_KEY_SECRET: ${{ secrets.GOOGLE_OAUTH_PRIVATE_KEY_SECRET }}
GOOGLE_OAUTH_PRIVATE_KEY_VALUE: ${{ secrets.GOOGLE_OAUTH_PRIVATE_KEY_VALUE }}
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ GIT

GIT
remote: https://github.com/samvera/hyrax.git
revision: a726d9bc5b2ad6fcf40108bfa5c54feae7ba82cf
revision: 539874a6041ebfb8e66685d8bf20e8f26a84cf6f
branch: main_before_rails_72
specs:
hyrax (5.0.3)
hyrax (5.0.4)
active-fedora (~> 14.0)
almond-rails (~> 0.1)
awesome_nested_set (~> 3.1)
Expand Down
2 changes: 0 additions & 2 deletions app/jobs/user_stat_collection_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ class UserStatCollectionJob < ApplicationJob
queue_as :default

def perform(*_args)
return unless UserStat.exists?

importer = Hyrax::UserStatImporter.new(verbose: true, logging: true)
importer.import
UserStatCollectionJob.set(wait_until: Date.tomorrow.midnight).perform_later
Expand Down
22 changes: 17 additions & 5 deletions app/models/concerns/account_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ module AccountSettings

setting :allow_downloads, type: 'boolean', default: true
setting :allow_signup, type: 'boolean', default: true
setting :analytics_provider, type: 'string'
setting :batch_email_notifications, type: 'boolean', default: false, disabled: true
setting :analytics, type: 'boolean', default: true
setting :analytics_reporting, type: 'boolean', default: true
setting :batch_email_notifications, type: 'boolean', default: false
setting :bulkrax_field_mappings, type: 'json_editor', default: Hyku.default_bulkrax_field_mappings.to_json
setting :bulkrax_validations, type: 'boolean', disabled: true
setting :cache_api, type: 'boolean', default: false
setting :contact_email, type: 'string', default: '[email protected]'
setting :contact_email_to, type: 'string', default: '[email protected]'
setting :depositor_email_notifications, type: 'boolean', default: false, disabled: true
setting :depositor_email_notifications, type: 'boolean', default: false
setting :doi_reader, type: 'boolean', default: false
setting :doi_writer, type: 'boolean', default: false
setting :file_acl, type: 'boolean', default: true, private: true
Expand All @@ -38,7 +39,8 @@ module AccountSettings
setting :email_subject_prefix, type: 'string'
setting :enable_oai_metadata, type: 'string', disabled: true
setting :file_size_limit, type: 'string', default: 5.gigabytes.to_s
setting :google_analytics_id, type: 'string'
setting :google_analytics_id, type: 'string', default: ENV.fetch('GOOGLE_ANALYTICS_ID', '')
setting :google_analytics_property_id, type: 'string', default: ENV.fetch('GOOGLE_ANALYTICS_PROPERTY_ID', '')
setting :google_scholarly_work_types, type: 'array', disabled: true
setting :geonames_username, type: 'string', default: ''
setting :gtm_id, type: 'string'
Expand All @@ -52,7 +54,7 @@ module AccountSettings
setting :smtp_settings, type: 'hash', private: true, default: {}
setting :solr_collection_options, type: 'hash', default: solr_collection_options
setting :ssl_configured, type: 'boolean', default: true, private: true
setting :user_analytics, type: 'boolean', default: false, disabled: true
setting :user_analytics, type: 'boolean', default: false
setting :weekly_email_list, type: 'array', disabled: true
setting :yearly_email_list, type: 'array', disabled: true

Expand Down Expand Up @@ -86,6 +88,7 @@ def setting(name, args)
value = super()
value = value.nil? ? ENV.fetch("HYKU_#{name.upcase}", nil) : value
value = value.nil? ? ENV.fetch("HYRAX_#{name.upcase}", nil) : value
value = value.nil? ? ENV.fetch(name.upcase.to_s, nil) : value
value = value.nil? ? args[:default] : value
set_type(value, (args[:type]).to_s)
end
Expand Down Expand Up @@ -198,8 +201,12 @@ def reload_library_config
config.contact_email = contact_email
config.geonames_username = geonames_username
config.uploader[:maxFileSize] = file_size_limit.to_i
config.analytics = analytics
config.analytics_reporting = analytics_reporting
end

reload_hyrax_analytics

Devise.mailer_sender = contact_email

if s3_bucket.present?
Expand All @@ -226,5 +233,10 @@ def reload_library_config
ActionMailer::Base.default_url_options[:protocol] = 'https'
end
# rubocop:enable Metrics/AbcSize

def reload_hyrax_analytics
Hyrax::Analytics.config.analytics_id = google_analytics_id
Hyrax::Analytics.config.property_id = google_analytics_property_id if Hyrax::Analytics.config.respond_to? :property_id=
end
end
# rubocop:enable Metrics/ModuleLength
14 changes: 13 additions & 1 deletion app/services/hyrax/user_stat_importer_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

# OVERRIDE Hyrax v5.0.1 to use Hyrax.query_service to find the fileset instead of FileSet.find
# Remove this if https://github.com/samvera/hyrax/pull/6992 gets merged and Hyrax is updated appropriately

# Also override to #sorted_users to account for multitenancy in Hyku
module Hyrax
module UserStatImporterDecorator
UserRecord = Struct.new('UserRecord', :id, :user_key, :last_stats_update)

# Returns an array of users of that tenant sorted by the date of their last stats update.
# Users that have not been recently updated will be at the top of the array.
def sorted_users
users = []
::User.registered.for_repository.without_system_accounts.uniq.each do |user|
users.push(UserRecord.new(user.id, user.user_key, date_since_last_cache(user)))
end
users.sort_by(&:last_stats_update)
end

private

def process_files(stats, user, start_date)
Expand Down
4 changes: 2 additions & 2 deletions app/views/shared/_ga4.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%# Override Hyrax 6.0.0 to use multiple analytics configs, one app wide one and one tenant override %>
<%# Override Hyrax 5.0.3 to use multiple analytics configs, one app wide one and one tenant override %>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= Hyrax::Analytics.config.analytics_id %>"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= Hyrax::Analytics.config.analytics_id.presence || ENV.fetch('GOOGLE_ANALYTICS_ID')%>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
Expand Down
2 changes: 2 additions & 0 deletions config/locales/simple_form.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ en:
yearly_email_list: List of email addresses to email the yearly report. Leave a single space between each email
email_format: Set a list of email domains that are allowed sign up to this repository e.g (@ubiquitypress.com @gmail.com). Leave a single space between each domain.
allow_signup: Allow users to sign up to your repository
analytics: Enable or disable Google Analytics 4 to track user interactions, including clicks.
analytics_reporting: Enable or disable reporting from Google Analytics 4 to display analytics data in the dashboard. Requires developer support for setup.
enable_oai_metadata: Enable or disable OAI link
shared_login: Enable or disable shared login
file_size_limit: This should be set to at least 536870912000
Expand Down
24 changes: 17 additions & 7 deletions ops/demo-deploy.tmpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ ingress:
paths:
- path: /
pathType: ImplementationSpecific
annotations: {
kubernetes.io/ingress.class: "nginx",
nginx.ingress.kubernetes.io/proxy-body-size: "0",
cert-manager.io/cluster-issuer: letsencrypt-production-dns
}
annotations:
{
kubernetes.io/ingress.class: "nginx",
nginx.ingress.kubernetes.io/proxy-body-size: "0",
cert-manager.io/cluster-issuer: letsencrypt-production-dns,
}
tls:
- hosts:
- hyku-demo.$BASE_URL
Expand All @@ -77,12 +78,16 @@ extraEnvVars: &envVars
value: "8080"
- name: FCREPO_REST_PATH
value: rest
- name: GOOGLE_ACCOUNT_JSON
value: $GOOGLE_ACCOUNT_JSON
- name: GOOGLE_ANALYTICS_ID
value: $GOOGLE_ANALYTICS_ID
- name: GOOGLE_ANALYTICS_PROPERTY_ID
value: $GOOGLE_ANALYTICS_PROPERTY_ID
- name: GOOGLE_OAUTH_APP_NAME
value: hyku-demo
- name: GOOGLE_OAUTH_APP_VERSION
value: '1.0'
value: "1.0"
- name: GOOGLE_OAUTH_PRIVATE_KEY_SECRET
value: $GOOGLE_OAUTH_PRIVATE_KEY_SECRET
- name: GOOGLE_OAUTH_PRIVATE_KEY_PATH
Expand Down Expand Up @@ -123,6 +128,12 @@ extraEnvVars: &envVars
value: [email protected]
- name: HYKU_FILE_ACL
value: "false"
- name: HYRAX_ANALYTICS
value: "true"
- name: HYRAX_ANALYTICS_PROVIDER
value: "ga4"
- name: HYRAX_ANALYTICS_REPORTING
value: "true"
- name: HYRAX_FITS_PATH
value: /app/fits/fits.sh
- name: HYRAX_FLEXIBLE
Expand Down Expand Up @@ -215,7 +226,6 @@ redis:
auth:
password: staging


solr:
enabled: false

Expand Down
7 changes: 0 additions & 7 deletions spec/jobs/user_stat_collection_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,5 @@
expect { UserStatCollectionJob.perform_now }.to have_enqueued_job(UserStatCollectionJob)
end
end

context 'when no UserStat records exist' do
it 'does not enqueue UserStatCollectionJob' do
switch!(account)
expect { UserStatCollectionJob.perform_now }.not_to have_enqueued_job(UserStatCollectionJob)
end
end
end
end
24 changes: 7 additions & 17 deletions spec/models/concerns/account_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
it 'returns all settings except private and disabled settings' do
expect(account.public_settings(is_superadmin: true).keys.sort).to eq %i[allow_downloads
allow_signup
analytics_provider
analytics
analytics_reporting
batch_email_notifications
bulkrax_field_mappings
cache_api
contact_email
contact_email_to
depositor_email_notifications
doi_reader
doi_writer
email_domain
Expand All @@ -23,32 +26,19 @@
file_size_limit
geonames_username
google_analytics_id
google_analytics_property_id
gtm_id
oai_admin_email
oai_prefix
oai_sample_identifier
s3_bucket
smtp_settings
solr_collection_options
ssl_configured]
ssl_configured
user_analytics]
end
# rubocop:enable RSpec/ExampleLength
end

context 'when we have a field marked as superadmin only' do
before { account.superadmin_settings = %i[analytics_provider] }
context 'and we are not a super admin' do
it 'does not include that field' do
expect(account.public_settings(is_superadmin: false).keys).not_to include(:analytics_provider)
end
end

context 'and we are a super admin' do
it 'includes that field' do
expect(account.public_settings(is_superadmin: true).keys).to include(:analytics_provider)
end
end
end
end

describe '#bulkrax_field_mappings' do
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ENV['HYKU_DEFAULT_HOST'] = nil
ENV['HYKU_MULTITENANT'] = 'true'
ENV['VALKYRIE_TRANSITION'] = 'true'
ENV['HYRAX_ANALYTICS_REPORTING'] = 'false'

require 'simplecov'
SimpleCov.start('rails')
Expand Down
Loading