Skip to content

Commit bcce595

Browse files
Kuntz ThomasDonSchado
authored andcommitted
Allow to use custom pageview url script for GoogleAnalytics tracker. (railslove#119)
* Allow to use custom pageview url script for GoogleAnalytics tracker.
1 parent 477e3ce commit bcce595

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

lib/rack/tracker/google_analytics/google_analytics.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def enhanced_ecommerce_events
6565
events.select {|e| e.kind_of?(EnhancedEcommerce) }
6666
end
6767

68+
def pageview_url_script
69+
options[:pageview_url_script] || 'window.location.pathname + window.location.search'
70+
end
71+
6872
private
6973

7074
def tracker_option_key(key)

lib/rack/tracker/google_analytics/template/google_analytics.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
ga('ecommerce:send');
4040
<% end %>
4141
<% if tracker %>
42-
ga('send', 'pageview', window.location.pathname + window.location.search);
42+
ga('send', 'pageview', <%= pageview_url_script %>);
4343
<% end %>
4444
</script>

spec/handler/google_analytics_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,22 @@ def env
267267
expect(subject).to include %q{setTimeout(function() { ga('send', 'event', '30_seconds', 'read'); },30000)}
268268
end
269269
end
270+
271+
describe '#pageview_url_script' do
272+
context 'without custom pageview url script' do
273+
subject { described_class.new(env, {} ) }
274+
275+
it 'returns return the custom pageview url script' do
276+
expect(subject.pageview_url_script).to eql ("window.location.pathname + window.location.search")
277+
end
278+
end
279+
280+
context 'with a custom pageview url script' do
281+
subject { described_class.new(env, { pageview_url_script: "{ 'page': location.pathname + location.search + location.hash }"}) }
282+
283+
it 'returns return the custom pageview url script' do
284+
expect(subject.pageview_url_script).to eql ("{ 'page': location.pathname + location.search + location.hash }")
285+
end
286+
end
287+
end
270288
end

spec/integration/google_analytics_integration_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
expect(page.find("head")).to have_content('ga("send",{"hitType":"event","eventCategory":"button","eventAction":"click","eventLabel":"nav-buttons","eventValue":"X"})')
1717
end
1818

19+
it "will have default pageview url script" do
20+
expect(page.find("head")).to have_content("ga('send', 'pageview', window.location.pathname + window.location.search);")
21+
end
22+
1923
describe 'adjust tracker position via options' do
2024
before do
2125
setup_app(action: :google_analytics) do |tracker|
@@ -49,4 +53,18 @@
4953
expect(page.find('head')).to have_content %q{Author\\'s name}
5054
end
5155
end
52-
end
56+
57+
describe 'Use custom pageview script' do
58+
before do
59+
setup_app(action: :google_analytics) do |tracker|
60+
tracker.handler :google_analytics, { tracker: 'U-XXX-Y', pageview_url_script: "{ 'page': location.pathname + location.search + location.hash }"}
61+
end
62+
visit '/'
63+
end
64+
65+
it "will use the custom pageview script for the pageview event" do
66+
expect(page.find("head")).to have_content("ga('send', 'pageview', { 'page': location.pathname + location.search + location.hash });")
67+
end
68+
end
69+
70+
end

0 commit comments

Comments
 (0)