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
4 changes: 4 additions & 0 deletions lib/rack/tracker/google_analytics/google_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def enhanced_ecommerce_events
events.select {|e| e.kind_of?(EnhancedEcommerce) }
end

def pageview_url_script
options[:pageview_url_script] || 'window.location.pathname + window.location.search'
end

private

def tracker_option_key(key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
ga('ecommerce:send');
<% end %>
<% if tracker %>
ga('send', 'pageview', window.location.pathname + window.location.search);
ga('send', 'pageview', <%= pageview_url_script %>);
<% end %>
</script>
18 changes: 18 additions & 0 deletions spec/handler/google_analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,22 @@ def env
expect(subject).to include %q{setTimeout(function() { ga('send', 'event', '30_seconds', 'read'); },30000)}
end
end

describe '#pageview_url_script' do
context 'without custom pageview url script' do
subject { described_class.new(env, {} ) }

it 'returns return the custom pageview url script' do
expect(subject.pageview_url_script).to eql ("window.location.pathname + window.location.search")
end
end

context 'with a custom pageview url script' do
subject { described_class.new(env, { pageview_url_script: "{ 'page': location.pathname + location.search + location.hash }"}) }

it 'returns return the custom pageview url script' do
expect(subject.pageview_url_script).to eql ("{ 'page': location.pathname + location.search + location.hash }")
end
end
end
end
20 changes: 19 additions & 1 deletion spec/integration/google_analytics_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
expect(page.find("head")).to have_content('ga("send",{"hitType":"event","eventCategory":"button","eventAction":"click","eventLabel":"nav-buttons","eventValue":"X"})')
end

it "will have default pageview url script" do
expect(page.find("head")).to have_content("ga('send', 'pageview', window.location.pathname + window.location.search);")
end

describe 'adjust tracker position via options' do
before do
setup_app(action: :google_analytics) do |tracker|
Expand Down Expand Up @@ -49,4 +53,18 @@
expect(page.find('head')).to have_content %q{Author\\'s name}
end
end
end

describe 'Use custom pageview script' do
before do
setup_app(action: :google_analytics) do |tracker|
tracker.handler :google_analytics, { tracker: 'U-XXX-Y', pageview_url_script: "{ 'page': location.pathname + location.search + location.hash }"}
end
visit '/'
end

it "will use the custom pageview script for the pageview event" do
expect(page.find("head")).to have_content("ga('send', 'pageview', { 'page': location.pathname + location.search + location.hash });")
end
end

end