Skip to content
Merged
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
29 changes: 26 additions & 3 deletions lib/rack/tracker/google_global/google_global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ def events
end

def trackers
options[:trackers].map { |tracker|
tracker[:id].respond_to?(:call) ? tracker.merge(id: tracker[:id].call(env)) : tracker
}.reject { |tracker| tracker[:id].nil? }
@_trackers ||= build_trackers
end

def set_options
Expand All @@ -46,6 +44,31 @@ def set_options

private

def build_trackers
options[:trackers].map(&method(:call_tracker)).reject(&method(:invalid_tracker?))
end

def call_tracker(tracker)
if tracker[:id].respond_to?(:call)
tracker.merge(id: tracker[:id].call(env))
else
tracker
end
end

def invalid_tracker?(tracker)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a very good idea!
But shouldn't we use this chance to give a warning, instead of silently ignoring the tracker? :)

if tracker[:id].to_s.strip == ''
$stdout.puts <<~WARN
WARNING: One of the trackers specified for Rack::Tracker handler 'google_global' is empty.
Trackers: #{options[:trackers]}
WARN

true
else
false
end
end

def build_set_options
value = options[:set]
value.respond_to?(:call) ? value.call(env) : value
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/tracker/google_global/template/google_global.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if trackers %>
<% if trackers.any? %>
<script async src='https://www.googletagmanager.com/gtag/js?id=<%= trackers[0][:id] %>'></script>
<script>
window.dataLayer = window.dataLayer || [];
Expand Down
27 changes: 19 additions & 8 deletions spec/integration/google_global_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,39 @@
RSpec.describe "Google Global Integration Integration" do
before do
setup_app(action: :google_global) do |tracker|
tracker.handler :google_global, trackers: [{ id: 'U-XXX-Y' }]
tracker.handler :google_global, tracker_options
end
visit '/'
end

subject { page }
let(:tracker_options) { { trackers: [{ id: 'U-XXX-Y' }] } }

it "embeds the script tag with tracking event from the controller action" do
expect(page.find("head")).to have_content('U-XXX-Y')
end

describe 'adjust tracker position via options' do
before do
setup_app(action: :google_global) do |tracker|
tracker.handler :google_global, trackers: [{ id: 'U-XXX-Y' }], position: :body
end
visit '/'
end
let(:tracker_options) { { trackers: [{ id: 'U-XXX-Y' }], position: :body } }

it "will be placed in the specified tag" do
expect(page.find("head")).to_not have_content('U-XXX-Y')
expect(page.find("body")).to have_content('U-XXX-Y')
end
end

describe "handles empty tracker id" do
let(:tracker_options) { { trackers: [{ id: nil }, { id: "" }, { id: " " }] } }

it "does not inject scripts" do
expect(page.find("head")).to_not have_content("<script async src='https://www.googletagmanager.com/gtag/js?id=")
end
end

describe "callable tracker id" do
let(:tracker_options) { { trackers: [{ id: proc { "U-XXX-Y" } }] } }

it "is injected into head with id from proc" do
expect(page.find("head")).to have_content('U-XXX-Y')
end
end
end