Module: GoogleAnalytics::Rails::ViewHelpers
- Defined in:
- lib/google-analytics/rails/view_helpers.rb
Overview
All the helper methods output raw javascript with single quoted strings. This allows more flexbility in choosing when the event gets sent (on page load, on user action, etc).
The only exception is #analytics_init, which is wrapped in a <script>
tag.
Instance Method Summary collapse
-
#analytics_add_item(order_id, product_id, product_name, product_variation, unit_price, quantity) ⇒ Object
Add an item to the current transaction.
-
#analytics_add_transaction(order_id, store_name, total, tax, shipping, city, state_or_province, country) ⇒ Object
Track an ecommerce transaction.
-
#analytics_init(options = {}) ⇒ String
Initializes the Analytics javascript.
-
#analytics_set_custom_var(index, name, value, opt_scope = 3) ⇒ Object
Set a custom variable.
-
#analytics_track_event(category, action, label = nil, value = nil) ⇒ Object
Track a custom event.
-
#analytics_track_transaction ⇒ Object
Flush the current transaction.
Instance Method Details
#analytics_add_item(order_id, product_id, product_name, product_variation, unit_price, quantity) ⇒ Object
Add an item to the current transaction
153 154 155 |
# File 'lib/google-analytics/rails/view_helpers.rb', line 153 def analytics_add_item(order_id, product_id, product_name, product_variation, unit_price, quantity) analytics_render_event(GA::Events::Ecommerce::AddItem.new(order_id, product_id, product_name, product_variation, unit_price, quantity)) end |
#analytics_add_transaction(order_id, store_name, total, tax, shipping, city, state_or_province, country) ⇒ Object
Track an ecommerce transaction
147 148 149 |
# File 'lib/google-analytics/rails/view_helpers.rb', line 147 def analytics_add_transaction(order_id, store_name, total, tax, shipping, city, state_or_province, country) analytics_render_event(GA::Events::Ecommerce::AddTransaction.new(order_id, store_name, total, tax, shipping, city, state_or_province, country)) end |
#analytics_init(options = {}) ⇒ String
Initializes the Analytics javascript. Put it in the <head>
tag.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/google-analytics/rails/view_helpers.rb', line 81 def analytics_init( = {}) unless tracker = .delete(:tracker).presence tracker = GA.tracker raise ArgumentError, "Tracker must be set! Did you set GA.tracker ?" unless tracker end local = .delete(:local) || false anonymize = .delete(:anonymize) || false custom_vars = .delete(:custom_vars) || [] custom_vars = [custom_vars] unless custom_vars.is_a?(Array) link_attribution = .delete(:enhanced_link_attribution) || false domain = .delete(:domain) || (local ? "none" : "auto") events = .delete(:add_events) || [] events = [events] unless events.is_a?(Array) queue = GAQ.new # unshift => reverse order events.unshift GA::Events::TrackPageview.new([:page]) # anonymize if needed before tracking the page view events.unshift GA::Events::AnonymizeIp.new if anonymize # custom_var if needed before tracking the page view custom_vars.each do |custom_var| events.unshift custom_var end events.unshift GA::Events::SetDomainName.new(domain) if local events.unshift GA::Events::SetAllowLinker.new(true) end events.unshift GA::Events::SetAccount.new(tracker) events.unshift GA::Events::Require.new( 'inpage_linkid', '//www.google-analytics.com/plugins/ga/inpage_linkid.js' ) if link_attribution events.each do |event| queue << event end queue.to_s.html_safe end |
#analytics_set_custom_var(index, name, value, opt_scope = 3) ⇒ Object
Set a custom variable. You're allowed only 1-5 for the index. The lifetime is defined by: 1 = visitor-level 2 = session-level 3 = page-level (default)
141 142 143 |
# File 'lib/google-analytics/rails/view_helpers.rb', line 141 def analytics_set_custom_var(index, name, value, opt_scope = 3) analytics_render_event(GA::Events::SetCustomVar.new(index, name, value, opt_scope)) end |
#analytics_track_event(category, action, label = nil, value = nil) ⇒ Object
Track a custom event
130 131 132 |
# File 'lib/google-analytics/rails/view_helpers.rb', line 130 def analytics_track_event(category, action, label = nil, value = nil) analytics_render_event(GA::Events::TrackEvent.new(category, action, label, value)) end |
#analytics_track_transaction ⇒ Object
Flush the current transaction
159 160 161 |
# File 'lib/google-analytics/rails/view_helpers.rb', line 159 def analytics_track_transaction analytics_render_event(GA::Events::Ecommerce::TrackTransaction.new) end |