Class: Bacchanalytics::Analytics
Class Method Summary
collapse
Instance Method Summary
collapse
#google_analytics_tracking_code, #ignored_organic_script
#response_source, #should_instrument?
Constructor Details
#initialize(app, options = {}) ⇒ Analytics
Returns a new instance of Analytics.
6
7
8
9
10
11
12
|
# File 'lib/bacchanalytics/analytics.rb', line 6
def initialize(app, options = {})
@app = app
@web_property_id = options[:web_property_id] || "UA-XXXXX-X"
@domain_name = options[:domain_name]
@ignored_organic = options[:ignored_organic]
@skip_ga_src = options[:skip_ga_src] || false
end
|
Class Method Details
.track_event(category, action, opt_label = nil, opt_value = nil, options = {}) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/bacchanalytics/analytics.rb', line 42
def self.track_event(category, action, opt_label = nil, opt_value = nil, options = {})
if opt_label.blank? || opt_value.blank?
track_event_code = "_gaq.push(['_trackEvent', '#{category}', '#{action}'])"
else
track_event_code = "_gaq.push(['_trackEvent', '#{category}', '#{action}', '#{opt_label}', '#{opt_value}'])"
end
timeout = options[:timeout] rescue nil
if timeout
"#{track_event_code};setTimeout('void(0)', #{timeout});"
else
track_event_code
end
end
|
.track_page_view_code(page) ⇒ Object
30
31
32
|
# File 'lib/bacchanalytics/analytics.rb', line 30
def self.track_page_view_code(page)
"_gaq.push(['_trackPageview', '#{page}'])"
end
|
.track_page_view_script(page) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/bacchanalytics/analytics.rb', line 34
def self.track_page_view_script(page)
<<-SCRIPT
<script type="text/javascript">
#{track_page_view_code(page)};
</script>
SCRIPT
end
|
Instance Method Details
#call(env) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/bacchanalytics/analytics.rb', line 14
def call(env)
status, , response = @app.call(env)
if should_instrument?() && (source = response_source(response))
@skip_ga_src = true if env["bacchanlytics.loaded_ga_src"]
tracking_code = google_analytics_tracking_code(@web_property_id, @domain_name)
env["bacchanalytics.loaded_ga_src"] = true
new_body = source.sub /<[hH][eE][aA][dD]\s*>/, "<head>\n\n#{tracking_code}"
["Content-Length"] = new_body.length.to_s
Rack::Response.new(new_body, status, ).finish
else
[status, , response]
end
end
|