Class: Gabba::Gabba
- Inherits:
-
Object
- Object
- Gabba::Gabba
- Defined in:
- lib/gabba/gabba.rb
Constant Summary collapse
- GOOGLE_HOST =
"www.google-analytics.com"
- BEACON_PATH =
"/__utm.gif"
- USER_AGENT =
"Gabba #{VERSION} Agent"
- VISITOR =
Custom var levels
1
- SESSION =
2
- PAGE =
3
- ESCAPES =
%w{ ' ! * ) }
Instance Attribute Summary collapse
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
-
#utmac ⇒ Object
Returns the value of attribute utmac.
-
#utmcc ⇒ Object
Returns the value of attribute utmcc.
-
#utmcs ⇒ Object
Returns the value of attribute utmcs.
-
#utmdt ⇒ Object
Returns the value of attribute utmdt.
-
#utmhn ⇒ Object
Returns the value of attribute utmhn.
-
#utmn ⇒ Object
Returns the value of attribute utmn.
-
#utmp ⇒ Object
Returns the value of attribute utmp.
-
#utmt ⇒ Object
Returns the value of attribute utmt.
-
#utmul ⇒ Object
Returns the value of attribute utmul.
-
#utmwv ⇒ Object
Returns the value of attribute utmwv.
Instance Method Summary collapse
- #add_item(order_id, item_sku, price, quantity, name = nil, category = nil, utmhid = random_id) ⇒ Object
-
#check_account_params ⇒ Object
sanity check that we have needed params to even call GA.
-
#cookie_params(utma1 = random_id, utma2 = rand(1147483647) + 1000000000, today = Time.now) ⇒ Object
create magical cookie params used by GA for its own nefarious purposes.
- #custom_var_data ⇒ Object
- #delete_custom_var(index) ⇒ Object
- #escape(t) ⇒ Object
- #event(category, action, label = nil, value = nil, utmni = false, utmhid = random_id) ⇒ Object
- #event_data(category, action, label = nil, value = nil) ⇒ Object
- #event_params(category, action, label = nil, value = nil, utmni = false, utmhid = false) ⇒ Object
-
#hey(params) ⇒ Object
makes the tracking call to Google Analytics.
-
#initialize(ga_acct, domain, agent = Gabba::USER_AGENT) ⇒ Gabba
constructor
A new instance of Gabba.
- #item_params(order_id, item_sku, name, category, price, quantity, utmhid) ⇒ Object
- #page_view(title, page, utmhid = random_id) ⇒ Object
- #page_view_params(title, page, utmhid = random_id) ⇒ Object
- #random_id ⇒ Object
- #set_custom_var(index, name, value, scope) ⇒ Object
- #transaction(order_id, total, store_name = nil, tax = nil, shipping = nil, city = nil, region = nil, country = nil, utmhid = random_id) ⇒ Object
- #transaction_params(order_id, total, store_name, tax, shipping, city, region, country, utmhid) ⇒ Object
Constructor Details
#initialize(ga_acct, domain, agent = Gabba::USER_AGENT) ⇒ Gabba
Returns a new instance of Gabba.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gabba/gabba.rb', line 27 def initialize(ga_acct, domain, agent = Gabba::USER_AGENT) @utmwv = "4.4sh" # GA version @utmcs = "UTF-8" # charset @utmul = "en-us" # language @utmn = random_id @utmhid = random_id @utmac = ga_acct @utmhn = domain @user_agent = agent @custom_vars = [] end |
Instance Attribute Details
#user_agent ⇒ Object
Returns the value of attribute user_agent.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def user_agent @user_agent end |
#utmac ⇒ Object
Returns the value of attribute utmac.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmac @utmac end |
#utmcc ⇒ Object
Returns the value of attribute utmcc.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmcc @utmcc end |
#utmcs ⇒ Object
Returns the value of attribute utmcs.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmcs @utmcs end |
#utmdt ⇒ Object
Returns the value of attribute utmdt.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmdt @utmdt end |
#utmhn ⇒ Object
Returns the value of attribute utmhn.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmhn @utmhn end |
#utmn ⇒ Object
Returns the value of attribute utmn.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmn @utmn end |
#utmp ⇒ Object
Returns the value of attribute utmp.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmp @utmp end |
#utmt ⇒ Object
Returns the value of attribute utmt.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmt @utmt end |
#utmul ⇒ Object
Returns the value of attribute utmul.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmul @utmul end |
#utmwv ⇒ Object
Returns the value of attribute utmwv.
25 26 27 |
# File 'lib/gabba/gabba.rb', line 25 def utmwv @utmwv end |
Instance Method Details
#add_item(order_id, item_sku, price, quantity, name = nil, category = nil, utmhid = random_id) ⇒ Object
162 163 164 165 |
# File 'lib/gabba/gabba.rb', line 162 def add_item(order_id, item_sku, price, quantity, name = nil, category = nil, utmhid = random_id) check_account_params hey(item_params(order_id, item_sku, name, category, price, quantity, utmhid)) end |
#check_account_params ⇒ Object
sanity check that we have needed params to even call GA
199 200 201 202 |
# File 'lib/gabba/gabba.rb', line 199 def check_account_params raise NoGoogleAnalyticsAccountError unless @utmac raise NoGoogleAnalyticsDomainError unless @utmhn end |
#cookie_params(utma1 = random_id, utma2 = rand(1147483647) + 1000000000, today = Time.now) ⇒ Object
create magical cookie params used by GA for its own nefarious purposes
194 195 196 |
# File 'lib/gabba/gabba.rb', line 194 def (utma1 = random_id, utma2 = rand(1147483647) + 1000000000, today = Time.now) "__utma=1.#{utma1}00145214523.#{utma2}.#{today.to_i}.#{today.to_i}.15;+__utmz=1.#{today.to_i}.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);" end |
#custom_var_data ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gabba/gabba.rb', line 55 def custom_var_data names = [] values = [] scopes = [] idx = 1 @custom_vars.each_with_index do |(n, v, s), i| next if !n || !v || (/\w/ !~ n) || (/\w/ !~ v) prefix = "#{i}!" if idx != i names << "#{prefix}#{escape(n)}" values << "#{prefix}#{escape(v)}" scopes << "#{prefix}#{escape(s)}" idx = i + 1 end names.empty? ? "" : "8(#{names.join('*')})9(#{values.join('*')})11(#{scopes.join('*')})" end |
#delete_custom_var(index) ⇒ Object
49 50 51 52 53 |
# File 'lib/gabba/gabba.rb', line 49 def delete_custom_var(index) raise "Index must be between 1 and 5" unless (1..5).include?(index) @custom_vars.delete_at(index) end |
#escape(t) ⇒ Object
223 224 225 226 227 228 229 |
# File 'lib/gabba/gabba.rb', line 223 def escape(t) return t if !t || (/\w/ !~ t.to_s) t.to_s.gsub(/[\*'!\)]/) do |m| "'#{ESCAPES.index(m)}" end end |
#event(category, action, label = nil, value = nil, utmni = false, utmhid = random_id) ⇒ Object
99 100 101 102 |
# File 'lib/gabba/gabba.rb', line 99 def event(category, action, label = nil, value = nil, utmni = false, utmhid = random_id) check_account_params hey(event_params(category, action, label, value, utmni, utmhid)) end |
#event_data(category, action, label = nil, value = nil) ⇒ Object
121 122 123 124 125 |
# File 'lib/gabba/gabba.rb', line 121 def event_data(category, action, label = nil, value = nil) data = "5(#{category}*#{action}" + (label ? "*#{label})" : ")") data += "(#{value})" if value data end |
#event_params(category, action, label = nil, value = nil, utmni = false, utmhid = false) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/gabba/gabba.rb', line 104 def event_params(category, action, label = nil, value = nil, utmni = false, utmhid = false) raise ArgumentError.new("utmni must be a boolean") if (utmni.class != TrueClass && utmni.class != FalseClass) { :utmwv => @utmwv, :utmn => @utmn, :utmhn => @utmhn, :utmni => (1 if utmni), # 1 for non interactive event, excluded from bounce calcs :utmt => 'event', :utme => "#{event_data(category, action, label, value)}#{custom_var_data}", :utmcs => @utmcs, :utmul => @utmul, :utmhid => utmhid, :utmac => @utmac, :utmcc => @utmcc || } end |
#hey(params) ⇒ Object
makes the tracking call to Google Analytics
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/gabba/gabba.rb', line 205 def hey(params) query = params.map {|k,v| "#{k}=#{URI.escape(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}" }.join('&') response = Net::HTTP.start(GOOGLE_HOST) do |http| request = Net::HTTP::Get.new("#{BEACON_PATH}?#{query}") request["User-Agent"] = URI.escape(user_agent) request["Accept"] = "*/*" http.request(request) end raise GoogleAnalyticsNetworkError unless response.code == "200" response end |
#item_params(order_id, item_sku, name, category, price, quantity, utmhid) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/gabba/gabba.rb', line 167 def item_params(order_id, item_sku, name, category, price, quantity, utmhid) # '1234', // utmtid URL-encoded order ID - required # 'DD44', // utmipc SKU/code - required # 'T-Shirt', // utmipn product name # 'Green Medium', // utmiva category or variation # '11.99', // utmipr unit price - required # '1' // utmiqt quantity - required { :utmwv => @utmwv, :utmn => @utmn, :utmhn => @utmhn, :utmt => 'item', :utmcs => @utmcs, :utmul => @utmul, :utmhid => utmhid, :utmac => @utmac, :utmcc => @utmcc || , :utmtid => order_id, :utmipc => item_sku, :utmipn => name, :utmiva => category, :utmipr => price, :utmiqt => quantity } end |
#page_view(title, page, utmhid = random_id) ⇒ Object
73 74 75 76 |
# File 'lib/gabba/gabba.rb', line 73 def page_view(title, page, utmhid = random_id) check_account_params hey(page_view_params(title, page, utmhid)) end |
#page_view_params(title, page, utmhid = random_id) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/gabba/gabba.rb', line 78 def page_view_params(title, page, utmhid = random_id) = { :utmwv => @utmwv, :utmn => @utmn, :utmhn => @utmhn, :utmcs => @utmcs, :utmul => @utmul, :utmdt => title, :utmhid => utmhid, :utmp => page, :utmac => @utmac, :utmcc => @utmcc || } # Add custom vars if present cvd = custom_var_data [:utme] = cvd if /\w/ =~ cvd end |
#random_id ⇒ Object
219 220 221 |
# File 'lib/gabba/gabba.rb', line 219 def random_id rand 8999999999 + 1000000000 end |
#set_custom_var(index, name, value, scope) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/gabba/gabba.rb', line 42 def set_custom_var(index, name, value, scope) raise "Index must be between 1 and 5" unless (1..5).include?(index) raise "Scope must be 1 (VISITOR), 2 (SESSION) or 3 (PAGE)" unless (1..3).include?(scope) @custom_vars[index] = [ name, value, scope ] end |
#transaction(order_id, total, store_name = nil, tax = nil, shipping = nil, city = nil, region = nil, country = nil, utmhid = random_id) ⇒ Object
127 128 129 130 |
# File 'lib/gabba/gabba.rb', line 127 def transaction(order_id, total, store_name = nil, tax = nil, shipping = nil, city = nil, region = nil, country = nil, utmhid = random_id) check_account_params hey(transaction_params(order_id, total, store_name, tax, shipping, city, region, country, utmhid)) end |
#transaction_params(order_id, total, store_name, tax, shipping, city, region, country, utmhid) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/gabba/gabba.rb', line 132 def transaction_params(order_id, total, store_name, tax, shipping, city, region, country, utmhid) # '1234', // utmtid URL-encoded order ID - required # 'Acme Clothing', // utmtst affiliation or store name # '11.99', // utmtto total - required # '1.29', // utmttx tax # '5', // utmtsp shipping # 'San Jose', // utmtci city # 'California', // utmtrg state or province # 'USA' // utmtco country { :utmwv => @utmwv, :utmn => @utmn, :utmhn => @utmhn, :utmt => 'tran', :utmcs => @utmcs, :utmul => @utmul, :utmhid => utmhid, :utmac => @utmac, :utmcc => @utmcc || , :utmtid => order_id, :utmtst => store_name, :utmtto => total, :utmttx => tax, :utmtsp => shipping, :utmtci => city, :utmtrg => region, :utmtco => country } end |