Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit 889d15a

Browse files
committed
stringify ga_event values
because when we add integers or floats to the tracker which is being send to GA, the values should be send as strings in the javascript.
1 parent 21c26a0 commit 889d15a

5 files changed

Lines changed: 18 additions & 7 deletions

File tree

lib/rack/tracker/extensions.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ def to_h
66
@table.dup
77
end unless method_defined? :to_h
88
end
9+
10+
class Hash
11+
def stringify_values
12+
inject({}) do |options, (key, value)|
13+
options[key] = value.to_s
14+
options
15+
end
16+
end
17+
end

lib/rack/tracker/google_analytics/google_analytics.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def write
1010
end
1111

1212
def event
13-
{ hitType: self.type }.merge(attributes).compact
13+
{ hitType: self.type }.merge(attributes.stringify_values).compact
1414
end
1515

1616
def attributes
@@ -20,7 +20,7 @@ def attributes
2020

2121
class Ecommerce < OpenStruct
2222
def write
23-
["ecommerce:#{self.type}", self.to_h.except(:type).compact].to_json.gsub(/\[|\]/, '')
23+
["ecommerce:#{self.type}", self.to_h.except(:type).compact.stringify_values].to_json.gsub(/\[|\]/, '')
2424
end
2525
end
2626

spec/handler/google_analytics_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ def env
4848
describe "with a event value" do
4949
def env
5050
{'tracker' => { 'google_analytics' => [
51-
Rack::Tracker::GoogleAnalytics::Send.new(category: "Users", action: "Login", label: "Standard", value: 5)
51+
Rack::Tracker::GoogleAnalytics::Send.new(category: "Users", action: "Login", label: "Standard", value: "5")
5252
]}}
5353
end
5454

5555
subject { described_class.new(env, tracker: 'somebody').render }
5656
it "will show events with values" do
57-
expect(subject).to match(%r{ga\(\"send\",{\"hitType\":\"event\",\"eventCategory\":\"Users\",\"eventAction\":\"Login\",\"eventLabel\":\"Standard\",\"eventValue\":5}\)},)
57+
expect(subject).to match(%r{ga\(\"send\",{\"hitType\":\"event\",\"eventCategory\":\"Users\",\"eventAction\":\"Login\",\"eventLabel\":\"Standard\",\"eventValue\":\"5\"}\)},)
5858
end
5959
end
6060
end
@@ -75,7 +75,7 @@ def env
7575
expect(subject).to match(%r{ga\(\"ecommerce:addItem\",#{{id: '1234', name: 'Fluffy Pink Bunnies', sku: 'DD23444', category: 'Party Toys', price: '11.99', quantity: '1'}.to_json}})
7676
end
7777
it "will add transaction" do
78-
expect(subject).to match(%r{ga\(\"ecommerce:addTransaction\",#{{id: '1234', affiliation: 'Acme Clothing', revenue: 11.99, shipping: '5', tax: '1.29', currency: 'EUR'}.to_json}})
78+
expect(subject).to match(%r{ga\(\"ecommerce:addTransaction\",#{{id: '1234', affiliation: 'Acme Clothing', revenue: '11.99', shipping: '5', tax: '1.29', currency: 'EUR'}.to_json}})
7979
end
8080
it "will submit cart" do
8181
expect(subject).to match(%r{ga\('ecommerce:send'\);})

spec/integration/google_analytics_integration_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
subject { page }
1212

1313
it "embeds the script tag with tracking event from the controller action" do
14-
expect(page).to have_content('ga("ecommerce:addItem",{"id":"1234","affiliation":"Acme Clothing","revenue":"11.99","shipping":"5","tax":"1.29"})')
14+
expect(page).to have_content('ga("ecommerce:addItem",{"id":"1234","name":"Fluffy Pink Bunnies","sku":"DD23444","category":"Party Toys","price":"11.99","quantity":"1"})')
15+
expect(page).to have_content('ga("ecommerce:addTransaction",{"id":"1234","affiliation":"Acme Clothing","revenue":"11.99","shipping":"5","tax":"1.29"})')
1516
expect(page).to have_content('ga("send",{"hitType":"event","eventCategory":"button","eventAction":"click","eventLabel":"nav-buttons","eventValue":"X"})')
1617
end
1718
end

spec/support/metal_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def facebook
2424

2525
def google_analytics
2626
tracker do |t|
27-
t.google_analytics :ecommerce, { type: 'addItem', id: '1234', affiliation: 'Acme Clothing', revenue: '11.99', shipping: '5', tax: '1.29' }
27+
t.google_analytics :ecommerce, { type: 'addTransaction', id: 1234, affiliation: 'Acme Clothing', revenue: 11.99, shipping: 5, tax: 1.29 }
28+
t.google_analytics :ecommerce, { type: 'addItem', id: 1234, name: 'Fluffy Pink Bunnies', sku: 'DD23444', category: 'Party Toys', price: 11.99, quantity: 1 }
2829
t.google_analytics :send, { type: 'event', category: 'button', action: 'click', label: 'nav-buttons', value: 'X' }
2930
end
3031
render "metal/index"

0 commit comments

Comments
 (0)