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

#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

#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