Skip to content

Commit 51bf9f6

Browse files
committed
looks like client_id is necessary
1 parent 19c4926 commit 51bf9f6

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

app/controllers/uploads_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ def parse_upload_path
5151
end
5252

5353
def track_download
54-
TrackFileDownloadJob.perform_later(request.url, @filename, @model_name)
54+
TrackFileDownloadJob.perform_later(client_id, request.url, @filename, @model_name)
55+
end
56+
57+
def client_id
58+
session.id&.to_s || SecureRandom.uuid
5559
end
5660

5761
def trackable_request?

app/jobs/track_file_download_job.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
class TrackFileDownloadJob < ApplicationJob
22
queue_as :default
33

4-
def perform(file_url, file_name, model_name)
4+
def perform(client_id, file_url, file_name, model_name)
55
return if measurement_id.blank? || api_secret.blank?
66

7-
send_ga4_event(file_url, file_name, model_name)
7+
send_ga4_event(client_id, file_url, file_name, model_name)
88
end
99

1010
private
1111

12-
def send_ga4_event(file_url, file_name, model_name)
12+
def send_ga4_event(client_id, file_url, file_name, model_name)
1313
payload = {
14+
client_id: client_id,
1415
events: [{
1516
name: "file_download",
1617
params: {

spec/integration/uploads_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@
6464

6565
context "download tracking" do
6666
it "tracks downloads for trackable models with regular browsers" do
67+
allow_any_instance_of(UploadsController).to receive(:client_id).and_return("test-client-id")
68+
6769
get @document_file.attachment.url, headers: {
6870
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
6971
}
7072

7173
expect(response).to have_http_status(:ok)
7274
expect(TrackFileDownloadJob).to have_received(:perform_later).with(
73-
request.url, @document_file.attachment.filename, "document_file"
75+
"test-client-id", request.url, @document_file.attachment.filename, "document_file"
7476
)
7577

7678
get @observation_report.attachment.url, headers: {
@@ -79,7 +81,7 @@
7981

8082
expect(response).to have_http_status(:ok)
8183
expect(TrackFileDownloadJob).to have_received(:perform_later).with(
82-
request.url, @observation_report.attachment.filename, "observation_report"
84+
"test-client-id", request.url, @observation_report.attachment.filename, "observation_report"
8385
)
8486
end
8587

spec/job/track_file_download_job_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
let(:model_name) { "User" }
77
let(:measurement_id) { "G-XXXXXXXXXX" }
88
let(:api_secret) { "test_api_secret" }
9+
let(:client_id) { "test_client" }
910

1011
before do
1112
ENV["GA4_MEASUREMENT_ID"] = measurement_id
@@ -21,6 +22,7 @@
2122
context "when environment variables are present" do
2223
let(:expected_payload) do
2324
{
25+
client_id: client_id,
2426
events: [{
2527
name: "file_download",
2628
params: {
@@ -48,7 +50,7 @@
4850
json: expected_payload
4951
)
5052

51-
described_class.perform_now(file_url, file_name, model_name)
53+
described_class.perform_now(client_id, file_url, file_name, model_name)
5254
end
5355
end
5456

@@ -59,7 +61,7 @@
5961

6062
it "does not send GA4 event" do
6163
expect(HTTP).not_to receive(:post)
62-
described_class.perform_now(file_url, file_name, model_name)
64+
described_class.perform_now(client_id, file_url, file_name, model_name)
6365
end
6466
end
6567

@@ -70,7 +72,7 @@
7072

7173
it "does not send GA4 event" do
7274
expect(HTTP).not_to receive(:post)
73-
described_class.perform_now(file_url, file_name, model_name)
75+
described_class.perform_now(client_id, file_url, file_name, model_name)
7476
end
7577
end
7678
end

0 commit comments

Comments
 (0)