Skip to content

Commit 92eb09c

Browse files
Refactor second experiment to use outbound beacon to conclude experimentation
TODO: the javascript for the event listener needs a bit more refinement in terms of the data collection it performs - there may not need to be any additional data in the payload, because the controller just needs to be called - no parameters are passed.
1 parent 25f851d commit 92eb09c

11 files changed

Lines changed: 31 additions & 33 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class BeaconController < ApplicationController
2+
def outbound
3+
ab_finished(:result_format)
4+
head :ok
5+
end
6+
end

app/controllers/record_controller.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@ class RecordController < ApplicationController
33

44
include RecordHelper
55

6-
# The out method is used for concluding A/B testing experiments that rely on the user following outbound links.
7-
#
8-
# @param url String a URL where the URL will be redirected
9-
def out
10-
if params[:url].present?
11-
ab_finished(:result_format)
12-
13-
redirect_to params[:url], allow_other_host: true
14-
else
15-
redirect_to '/'
16-
end
17-
end
18-
196
def view
207
id = params[:id]
218
index = ENV.fetch('TIMDEX_INDEX', nil)

app/helpers/search_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def format_highlight_label(field_name)
1818

1919
def link_to_result(result)
2020
if result[:source_link].present?
21-
Rails.logger.debug(outbound_path(url: result[:source_link]))
2221
link_to(result[:title], result[:source_link])
2322
else
2423
result[:title]

app/javascript/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import "@hotwired/turbo-rails"
33
import "controllers"
44
import "loading_spinner"
5+
import "outbound_beacon"
56

67
// Show the progress bar after 200 milliseconds, not the default 500
78
Turbo.config.drive.progressBarDelay = 200;

app/javascript/outbound_beacon.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const resultsContainer = document.querySelector('.results-list');
2+
3+
if (resultsContainer) {
4+
resultsContainer.addEventListener('click', (event) => {
5+
const link = event.target.closest('a');
6+
7+
// Discard clicks that aren't on links
8+
if (!link || !resultsContainer.contains(link)) return;
9+
10+
const data = new FormData();
11+
data.append('experiment', 'result_format');
12+
console.log(data);
13+
navigator.sendBeacon('/beacon', data);
14+
});
15+
}

app/views/record/out.html.erb

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/views/search/_result.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858

5959
<% if Feature.enabled?(:record_link) %>
6060
<div class="result-get">
61+
<% if @fulfillment == 'link' %>
62+
<%= link_to 'View full record', 'https://example.org', class: 'button' %>
63+
<% end %>
6164
<%= view_record(result) %>
6265
</div>
6366
<% end %>

app/views/search/_result_primo.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<h3 class="record-title">
77
<span class="sr">Title: </span>
88
<% if result[:links]&.find { |link| link['kind'] == 'full record' } %>
9-
<% Rails.logger.debug(outbound_path(url: result[:links].find { |link| link['kind'] == 'full record' }['url'])) %>
109
<%= link_to(result[:title], result[:links].find { |link| link['kind'] == 'full record' }['url']) %>
1110
<% else %>
1211
<%= result[:title] %>
@@ -60,6 +59,9 @@
6059
</div>
6160

6261
<div class="result-get">
62+
<% if @fulfillment == 'link' %>
63+
<%= link_to 'View full record', 'https://example.org', class: 'button' %>
64+
<% end %>
6365
<% if result[:links].present? %>
6466
<% result[:links].each do |link| %>
6567
<% if link['kind'].downcase == 'full record' %>

config/importmap.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
pin "application", preload: true
44
pin "loading_spinner", preload: true
5+
pin "outbound_beacon", preload: true
56
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
67
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
78
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true

config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
get 'lookup', to: 'libkey#lookup'
77

8-
get 'out', to: 'record#out', as: "outbound"
9-
108
get 'record/(:id)',
119
to: 'record#view',
1210
as: 'record',
@@ -18,5 +16,7 @@
1816

1917
mount Split::Dashboard, at: 'split'
2018

19+
post 'beacon', to: 'beacon#outbound'
20+
2121
get 'robots.txt', to: 'robots#robots'
2222
end

0 commit comments

Comments
 (0)