Class: Analytical::Modules::Google

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/analytical/modules/google.rb

Instance Attribute Summary

Attributes included from Base

#command_store, #initialized, #options, #tracking_command_location

Instance Method Summary collapse

Methods included from Base

#init_location, #init_location?, #process_queued_commands, #protocol, #queue

Constructor Details

#initialize(options = {}) ⇒ Google

Returns a new instance of Google.



6
7
8
9
# File 'lib/analytical/modules/google.rb', line 6

def initialize(options={})
  super
  @tracking_command_location = :head_append
end

Instance Method Details

#add_item(order_id, sku, name, category, price, quantity) ⇒ Object

code.google.com/apis/analytics/docs/gaJS/gaJSApiEcommerce.html#gat.GA_Tracker._addItem String orderId Optional Order ID of the transaction to associate with item. String sku Required. Item’s SKU code. String name Required. Product name. Required to see data in the product detail report. String category Optional. Product category. String price Required. Product price. String quantity Required. Purchase quantity.



106
107
108
109
# File 'lib/analytical/modules/google.rb', line 106

def add_item(order_id, sku, name, category, price, quantity)
  data  = "'#{order_id}', '#{sku}', '#{name}', '#{category}', '#{price}', '#{quantity}'"
  "_gaq.push(['_addItem', #{data}]);"
end

#add_trans(order_id, affiliation = nil, total = nil, tax = nil, shipping = nil, city = nil, state = nil, country = nil) ⇒ Object

code.google.com/apis/analytics/docs/gaJS/gaJSApiEcommerce.html#gat.GA_Tracker._addTrans String orderId Required. Internal unique order id number for this transaction. String affiliation Optional. Partner or store affiliation (undefined if absent). String total Required. Total dollar amount of the transaction. String tax Optional. Tax amount of the transaction. String shipping Optional. Shipping charge for the transaction. String city Optional. City to associate with transaction. String state Optional. State to associate with transaction. String country Optional. Country to associate with transaction.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/analytical/modules/google.rb', line 85

def add_trans(order_id, affiliation=nil, total=nil, tax=nil, shipping=nil, city=nil, state=nil, country=nil)
  data = []
  data << "'#{order_id}'"
  data << "'#{affiliation}'"
  data << "'#{total}'"
  data << "'#{tax}'"
  data << "'#{shipping}'"
  data << "'#{city}'"
  data << "'#{state}'"
  data << "'#{country}'"

  "_gaq.push(['_addTrans', #{data.join(', ')}]);"
end

#custom_event(category, action, opt_label = nil, opt_value = nil) ⇒ Object



43
44
45
46
# File 'lib/analytical/modules/google.rb', line 43

def custom_event(category, action, opt_label=nil, opt_value=nil)
  args = [category, action, opt_label, opt_value].compact
  "_gaq.push(" + [ "_trackEvent", *args].to_json + ");"
end

#event(name, *args) ⇒ Object



36
37
38
39
40
41
# File 'lib/analytical/modules/google.rb', line 36

def event(name, *args)
  data = args.first || {}
  data = data[:value] if data.is_a?(Hash)
  data_string = !data.nil? ? ", #{data}" : ""
  "_gaq.push(['_trackEvent', \"Event\", \"#{name}\"" + data_string + "]);"
end

#init_javascript(location) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/analytical/modules/google.rb', line 11

def init_javascript(location)
  init_location(location) do
    js = <<-HTML
    <!-- Analytical Init: Google -->
    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', '#{options[:key]}']);
      _gaq.push(['_setDomainName', '#{options[:domain]}']);
      #{"_gaq.push(['_setAllowLinker', true]);" if options[:allow_linker]}
      _gaq.push(['_trackPageview']);
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    </script>
    HTML
    js
  end
end

#set(data) ⇒ Object

code.google.com/apis/analytics/docs/tracking/gaTrackingCustomVariables.html

_setCustomVar(index, name, value, opt_scope)

index — The slot for the custom variable. Required. This is a number whose value can range from 1 - 5, inclusive.

name — The name for the custom variable. Required. This is a string that identifies the custom variable and appears in the top-level Custom Variables report of the Analytics reports.

value — The value for the custom variable. Required. This is a string that is paired with a name.

opt_scope — The scope for the custom variable. Optional. As described above, the scope defines the level of user engagement with your site. It is a number whose possible values are 1 (visitor-level), 2 (session-level), or 3 (page-level). When left undefined, the custom variable scope defaults to page-level interaction.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/analytical/modules/google.rb', line 62

def set(data)
  if data.is_a?(Hash) && data.keys.any?
    index = data[:index].to_i
    name  = data[:name ]
    value = data[:value]
    scope = data[:scope]
    if (1..5).to_a.include?(index) && !name.nil? && !value.nil?
      data = "#{index}, '#{name}', '#{value}'"
      data += (1..3).to_a.include?(scope) ? ", #{scope}" : ""
      return "_gaq.push(['_setCustomVar', #{ data }]);"
    end
  end
end

#track(*args) ⇒ Object



32
33
34
# File 'lib/analytical/modules/google.rb', line 32

def track(*args)
  "_gaq.push(['_trackPageview'#{args.empty? ? ']' : ', "' + args.first + '"]'});"
end

#track_transObject

code.google.com/apis/analytics/docs/gaJS/gaJSApiEcommerce.html#gat.GA_Tracker._trackTrans Sends both the transaction and item data to the Google Analytics server. This method should be used in conjunction with the add_item and add_trans methods.



114
115
116
# File 'lib/analytical/modules/google.rb', line 114

def track_trans
  "_gaq.push(['_trackTrans']);"
end