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

HYC 1935 - Filter Bots out of Download Stats #1105

Merged
merged 9 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end

gem 'browser', '~> 5.3', '>= 5.3.1'
gem 'active-fedora', '~> 14.0'
gem 'blacklight', git: 'https://github.com/UNC-Libraries/blacklight.git', branch: 'unc-development'
gem 'blacklight_advanced_search', '~> 8.0.0.alpha2'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ GEM
ruby-box
signet (~> 0.8)
typhoeus
browser (5.3.1)
builder (3.2.4)
bulkrax (6.0.1)
bagit (~> 0.4)
Expand Down Expand Up @@ -1077,6 +1078,7 @@ DEPENDENCIES
blacklight_oai_provider (= 7.0.2)
blacklight_range_limit (~> 8.3.0)
bootstrap (~> 4.0)
browser (~> 5.3, >= 5.3.1)
bulkrax (= 6.0.1)
byebug (~> 11.1.3)
capybara (~> 3.36)
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/concerns/hyc/download_analytics_behavior.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# frozen_string_literal: true
module Hyc
module DownloadAnalyticsBehavior
include HycHelper
extend ActiveSupport::Concern

included do
after_action :track_download, only: :show

def track_download
if bot_request?(request.user_agent)
Rails.logger.info("Bot request detected: #{request.user_agent}")
head :forbidden
bbpennel marked this conversation as resolved.
Show resolved Hide resolved
return
end
if Hyrax::Analytics.config.auth_token.present? && !request.url.match('thumbnail')
Rails.logger.debug("Recording download event for #{params[:id]}")
medium = request.referrer.present? ? 'referral' : 'direct'
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/hyc_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# frozen_string_literal: true
# Include hyc-specific helper code here instead of in the HyraxHelper to avoid circular dependencies
module HycHelper
def bot_request?(user_agent)
bbpennel marked this conversation as resolved.
Show resolved Hide resolved
browser = Browser.new(user_agent)
browser.bot?
end

def language_links(options)
language_link_array = options[:value].map do |lang|
lang_label = LanguagesService.label(lang)
Expand Down
29 changes: 28 additions & 1 deletion spec/controllers/hyrax/downloads_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
let(:spec_base_analytics_url) { 'https://analytics-qa.lib.unc.edu' }
let(:spec_site_id) { '5' }
let(:spec_auth_token) { 'testtoken' }
let(:bot_user_agents) { YAML.load_file(Rails.root.join('spec/fixtures/files', 'bots_fixture.yml')).keys }
let(:stub_matomo) do
stub_request(:get, "#{spec_base_analytics_url}/matomo.php").with(query: hash_including({'token_auth' => 'testtoken',
'idsite' => '5'}))
Expand Down Expand Up @@ -55,7 +56,6 @@
}
}
)

allow(controller.request).to receive(:referrer).and_return('http://example.com')
expect(controller).to respond_to(:track_download)
expect(controller.track_download).to eq 200
Expand Down Expand Up @@ -87,6 +87,33 @@
expect(stub).to have_been_requested.times(1) # must be after the method call that creates request
end

it 'does not track downloads for any bot user agent' do
bot_user_agents.each do |bot_user_agent|
bbpennel marked this conversation as resolved.
Show resolved Hide resolved
allow(controller.request).to receive(:user_agent).and_return(bot_user_agent)
allow(SecureRandom).to receive(:uuid).and_return('555')
request.env['HTTP_REFERER'] = 'http://example.com'
request.headers['User-Agent'] = bot_user_agent
stub = stub_request(:get, "#{spec_base_analytics_url}/matomo.php")
.with(query: hash_including({
'e_a' => 'DownloadIR',
'e_c' => 'Unknown',
'e_v' => 'referral',
'urlref' => 'http://example.com',
'url' => "http://test.host/downloads/#{file_set.id}"
}))
.to_return(status: 200, body: '', headers: {})

allow(Rails.logger).to receive(:info)
expect(Rails.logger).to receive(:info).with("Bot request detected: #{bot_user_agent}")

get :show, params: { id: file_set.id }


# Assert no request is sent to Matomo
expect(stub).to have_been_requested.times(0)
end
end

it 'sets the medium to direct when there is no referrer' do
allow(controller).to receive(:cookies) { { _ga: 'ga.1.2.3'} }
request.env['HTTP_REFERER'] = nil
Expand Down
Loading
Loading