rack-google-analytics 0.14.0 → 1.0.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +14 -35
- data/lib/google-analytics/instance_methods.rb +3 -20
- data/lib/rack-google-analytics.rb +1 -1
- data/lib/rack/google-analytics.rb +8 -21
- data/lib/rack/google-analytics/version.rb +1 -1
- data/lib/rack/templates/async.erb +22 -30
- data/lib/tracking/event.rb +4 -2
- metadata +6 -6
- data/lib/rack/templates/sync.erb +0 -12
- data/lib/tracking/custom_var.rb +0 -14
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OTMwMGRkYjg3MzcwMTFmZGRiZmJlMjMyZTM4OTdjNGE5N2UzOGRhZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZGZmMjU0YTg5MjFiM2Q1MWRmZjkxZjYwYmVkNTg5ZmE5YmMzOTQ0Mg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NjEyMjI2YjMxYTdiMTZmNGQ3ODdlZTQ2YjU0OGFmMmNmNTEwZGM1MjZiY2Zj
|
10
|
+
YTYwM2E0MTg0MGRjYWVjMDU3OWM3NDFkYzRjMTc1NDEzMmE3NTBmNGJmZDBl
|
11
|
+
ZjVmMzFlMzBkNTRjM2ZkNzZhMjFkZGY2ZWVlZWM3YmRkNzZiM2U=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YmFhNjg0YTA1YTk5YjQ0MDFlZGZjZDQ4NmJlNTNjMDhhMDBhODljMmMwZjlm
|
14
|
+
MDhhM2M3YjU2ODJmMDI5ZTYzMDNkNjBmNGY0NzYwMzFkYmM3YWNjN2Q3OGEw
|
15
|
+
ZTMxZmVhZGY5NzBlMmY5ZDg5YWMxMzRhOGM0ZDk5YmQxMjdmMDI=
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Rack google Analytics
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/kangguru/rack-google-analytics.png?branch=
|
3
|
+
[![Build Status](https://travis-ci.org/kangguru/rack-google-analytics.png?branch=analytics-js)](https://travis-ci.org/kangguru/rack-google-analytics)
|
4
4
|
|
5
5
|
Simple Rack middleware to help injecting the Google Analytics tracking code in your website.
|
6
6
|
|
7
|
-
This middleware injects
|
7
|
+
This middleware injects the Google Analytics tracking code into the correct place of any request only when the response's `Content-Type` header contains `html` (therefore `text/html` and similar).
|
8
8
|
|
9
9
|
## Usage
|
10
10
|
|
@@ -28,15 +28,7 @@ use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
|
|
28
28
|
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
|
29
29
|
```
|
30
30
|
|
31
|
-
#### Rails
|
32
|
-
|
33
|
-
```ruby
|
34
|
-
## environment.rb:
|
35
|
-
config.gem 'rack-google-analytics', :lib => 'rack/google-analytics'
|
36
|
-
config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
|
37
|
-
```
|
38
|
-
|
39
|
-
#### Rails 3.X and 4.X
|
31
|
+
#### Rails 3.X and Rails 4.X
|
40
32
|
|
41
33
|
```ruby
|
42
34
|
## application.rb:
|
@@ -45,42 +37,23 @@ config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
|
|
45
37
|
|
46
38
|
### Options
|
47
39
|
|
48
|
-
* `:async` - sets to use asynchronous tracker
|
49
|
-
* `:multiple` - sets track for multiple subdomains. (must also set :domain)
|
50
|
-
* `:domain` - sets the domain name for the GATC cookies. Defaults to `auto`. (must also set :multiple)
|
51
|
-
* `:top_level` - sets tracker for multiple top-level domains. (must also set :domain)
|
52
40
|
* `:anonymize_ip` - sets the tracker to remove the last octet from all IP addresses, see https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat?hl=de#_gat._anonymizeIp for details.
|
41
|
+
* `:domain` - sets the domain name for the GATC cookies. Defaults to `auto`. (must also set :multiple)
|
53
42
|
* `:site_speed_sample_rate` - Defines a new sample set size for Site Speed data collection, see https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiBasicConfiguration?hl=de#_gat.GA_Tracker_._setSiteSpeedSampleRate
|
54
|
-
* `:
|
55
|
-
timeouts for adjusted bounce rate tracking. See http://analytics.blogspot.ca/2012/07/tracking-adjusted-bounce-rate-in-google.html for details.
|
56
|
-
|
57
|
-
Note: since 0.2.0 this will use the asynchronous Google Analytics tracking code, for the traditional behaviour please use:
|
58
|
-
|
59
|
-
```ruby
|
60
|
-
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x', :async => false
|
61
|
-
```
|
43
|
+
* `:enhanced_link_attribution` - Enable enhanced link attribution, see https://support.google.com/analytics/answer/2558867?hl=en
|
44
|
+
* `:adjusted_bounce_rate_timeouts` - An array of times in seconds that the tracker will use to set timeouts for adjusted bounce rate tracking. See http://analytics.blogspot.ca/2012/07/tracking-adjusted-bounce-rate-in-google.html for details.
|
62
45
|
|
63
46
|
If you are not sure what's best, go with the defaults, and read here if you should opt-out.
|
64
47
|
|
65
|
-
## Custom Variable Tracking
|
66
|
-
|
67
|
-
In your application controller, you may track a custom variable. For example:
|
68
|
-
|
69
|
-
```ruby
|
70
|
-
set_ga_custom_var(1, "LoggedIn", value, GoogleAnalytics::CustomVar::SESSION_LEVEL)
|
71
|
-
```
|
72
|
-
|
73
|
-
See https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables for details.
|
74
|
-
|
75
48
|
## Event Tracking
|
76
49
|
|
77
50
|
In your application controller, you may track an event. For example:
|
78
51
|
|
79
52
|
```ruby
|
80
|
-
|
53
|
+
ga_track_event("Users", "Login", "Standard")
|
81
54
|
```
|
82
55
|
|
83
|
-
See https://developers.google.com/analytics/devguides/collection/
|
56
|
+
See https://developers.google.com/analytics/devguides/collection/analyticsjs/events
|
84
57
|
|
85
58
|
## Custom Push
|
86
59
|
|
@@ -101,6 +74,12 @@ config.middleware.use Rack::GoogleAnalytics, :tracker => lambda { |env|
|
|
101
74
|
}
|
102
75
|
```
|
103
76
|
|
77
|
+
## Special use case: Event tracking only
|
78
|
+
|
79
|
+
If you already set up your Google Analytics `analytics.js` tracker object with pageview tracking in your templates/frontend (inside the `<head>`), the only thing you might want to use the `rack-google-analytics` middleware for is to track server-side events which you can't properly track in the forntend. In that case simply use the middleware without specifying the `:tracker` option, then it will only render the event tracking code (`ga('send', hitType: 'event', ..)`) and nothing else.
|
80
|
+
|
81
|
+
config.middleware.use Rack::GoogleAnalytics
|
82
|
+
|
104
83
|
|
105
84
|
## Thread Safety
|
106
85
|
|
@@ -19,30 +19,13 @@ module GoogleAnalytics
|
|
19
19
|
|
20
20
|
protected
|
21
21
|
|
22
|
-
# Sets a custom variable on a page load
|
23
|
-
#
|
24
|
-
# e.g. writes
|
25
|
-
# _gaq.push(['_setCustomVar',
|
26
|
-
# 2, // This custom var is set to slot #2. Required parameter.
|
27
|
-
# 'Shopping Attempts', // The name of the custom variable. Required parameter.
|
28
|
-
# 'Yes', // The value of the custom variable. Required parameter.
|
29
|
-
# // (you might set this value by default to No)
|
30
|
-
# 2 // Sets the scope to session-level. Optional parameter.
|
31
|
-
# ]);
|
32
|
-
def set_ga_custom_var(slot, name, value, scope = nil)
|
33
|
-
var = GoogleAnalytics::CustomVar.new(slot, name, value, scope)
|
34
|
-
|
35
|
-
ga_custom_vars.push(var)
|
36
|
-
end
|
37
|
-
|
38
22
|
# Tracks an event or goal on a page load
|
39
23
|
#
|
40
24
|
# e.g. writes
|
41
|
-
#
|
25
|
+
# ga.('send', 'event', 'Videos', 'Play', 'Gone With the Wind');
|
42
26
|
#
|
43
|
-
def
|
44
|
-
|
45
|
-
ga_events.push(var)
|
27
|
+
def ga_track_event(category, action, label = nil, value = nil)
|
28
|
+
ga_events.push(GoogleAnalytics::Event.new(category, action, label, value))
|
46
29
|
end
|
47
30
|
|
48
31
|
def ga_push(*attributes)
|
@@ -6,11 +6,9 @@ module Rack
|
|
6
6
|
class GoogleAnalytics
|
7
7
|
|
8
8
|
EVENT_TRACKING_KEY = "google_analytics.event_tracking"
|
9
|
-
|
10
|
-
DEFAULT = { :async => true, :advertising => false, :inpage_pageid => false }
|
9
|
+
DEFAULT = { async: true, enhanced_link_attribution: false }
|
11
10
|
|
12
11
|
def initialize(app, options = {})
|
13
|
-
raise ArgumentError, "Tracker must be set!" unless valid_tracker?(options[:tracker])
|
14
12
|
@app, @options = app, DEFAULT.merge(options)
|
15
13
|
end
|
16
14
|
|
@@ -34,7 +32,8 @@ module Rack
|
|
34
32
|
# Store the events until next time
|
35
33
|
env["rack.session"][EVENT_TRACKING_KEY] = env[EVENT_TRACKING_KEY]
|
36
34
|
end
|
37
|
-
|
35
|
+
|
36
|
+
@options[:tracker] = expand_tracker(env, @options[:tracker])
|
38
37
|
|
39
38
|
@body.each { |fragment| response.write inject(fragment) }
|
40
39
|
@body.close if @body.respond_to?(:close)
|
@@ -44,29 +43,17 @@ module Rack
|
|
44
43
|
|
45
44
|
private
|
46
45
|
|
47
|
-
# tracker should be non-nil, non-empty string or a lambda
|
48
|
-
def valid_tracker?(tracker)
|
49
|
-
return false unless tracker
|
50
|
-
return (tracker.respond_to?(:call) && tracker.lambda?) || !tracker.empty?
|
51
|
-
end
|
52
|
-
|
53
46
|
def html?; @headers['Content-Type'] =~ /html/; end
|
54
47
|
|
55
48
|
def inject(response)
|
56
|
-
|
49
|
+
@tracker_options = { cookieDomain: @options[:domain] }.select{|k,v| v }.to_json
|
50
|
+
@template ||= ::ERB.new ::File.read ::File.expand_path("../templates/async.erb",__FILE__)
|
57
51
|
|
58
|
-
@template
|
59
|
-
if @options[:async]
|
60
|
-
response.gsub(%r{</head>}, @template.result(binding) + "</head>")
|
61
|
-
else
|
62
|
-
response.gsub(%r{</body>}, @template.result(binding) + "</body>")
|
63
|
-
end
|
52
|
+
response.gsub(%r{</head>}, @template.result(binding) + "</head>")
|
64
53
|
end
|
65
54
|
|
66
|
-
|
67
|
-
|
68
|
-
return tracker unless tracker.respond_to?(:call)
|
69
|
-
return tracker.call(env)
|
55
|
+
def expand_tracker(env, tracker)
|
56
|
+
tracker.respond_to?(:call) ? tracker.call(env) : tracker
|
70
57
|
end
|
71
58
|
|
72
59
|
end
|
@@ -1,46 +1,38 @@
|
|
1
1
|
<script type="text/javascript">
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
<% if @options[:tracker] %>
|
4
|
+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
5
|
+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
6
|
+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
7
|
+
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
8
|
+
|
9
|
+
ga('create', '<%= @options[:tracker] %>', <%= @tracker_options %>);
|
10
|
+
|
11
|
+
<% if @options[:enhanced_link_attribution] %>
|
12
|
+
ga('require', 'linkid', 'linkid.js');
|
7
13
|
<% end %>
|
8
|
-
|
9
|
-
<% if @options[:
|
10
|
-
|
14
|
+
|
15
|
+
<% if @options[:anonymize_ip] %>
|
16
|
+
ga('set', 'anonymizeIp', true);
|
11
17
|
<% end %>
|
12
|
-
|
13
|
-
|
14
|
-
|
18
|
+
|
19
|
+
<% if @options[:adjusted_bounce_rate_timeouts] %>
|
20
|
+
<% @options[:adjusted_bounce_rate_timeouts].each do |timeout| %>
|
21
|
+
setTimeout(ga('send', 'event', '<%= "#{timeout.to_s}_seconds" %>', 'read'),<%= timeout*1000 %>);
|
15
22
|
<% end %>
|
16
|
-
<% if @options[:anonymize_ip] %>
|
17
|
-
_gaq.push(['_gat._anonymizeIp']);
|
18
23
|
<% end %>
|
19
|
-
|
20
|
-
_gaq.push(['_setSiteSpeedSampleRate', <%= @options[:site_speed_sample_rate].to_i %>]);
|
24
|
+
|
21
25
|
<% end %>
|
22
26
|
|
23
27
|
<% if @options[:tracker_vars]
|
24
28
|
@options[:tracker_vars].each do |var|
|
25
29
|
%>
|
26
|
-
|
30
|
+
ga('send', <%= var.write() %>);
|
27
31
|
<% end
|
28
32
|
end
|
29
33
|
%>
|
30
|
-
<% if @options[:
|
31
|
-
|
32
|
-
setTimeout("_gaq.push(['_trackEvent', <%= "#{timeout.to_s}_seconds" %>, 'read'])",<%= timeout*1000 %>);
|
34
|
+
<% if @options[:tracker] %>
|
35
|
+
ga('send', 'pageview');
|
33
36
|
<% end %>
|
34
|
-
|
35
|
-
_gaq.push(['_trackPageview']);
|
36
|
-
|
37
|
-
(function() {
|
38
|
-
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
39
|
-
<% if @options[:advertising] %>
|
40
|
-
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
|
41
|
-
<% else %>
|
42
|
-
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
43
|
-
<% end %>
|
44
|
-
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
45
|
-
})();
|
37
|
+
|
46
38
|
</script>
|
data/lib/tracking/event.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require "active_support/json"
|
2
|
+
require "active_support/ordered_hash"
|
2
3
|
|
3
4
|
module GoogleAnalytics
|
4
5
|
|
5
6
|
# A Struct that mirrors the structure of a custom var defined in Google Analytics
|
6
7
|
# see https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
|
7
|
-
class Event < Struct.new(:category, :action, :label, :value
|
8
|
+
class Event < Struct.new(:category, :action, :label, :value)
|
8
9
|
|
9
10
|
def write
|
10
|
-
|
11
|
+
{ hitType: 'event', eventCategory: self.category, eventAction: self.action, eventLabel: self.label, eventValue: self.value }.select{|k,v| v }.to_json
|
11
12
|
end
|
13
|
+
|
12
14
|
end
|
13
15
|
end
|
metadata
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-google-analytics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.pre1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lee Hambley
|
8
|
+
- Lars Brillert
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
@@ -113,6 +114,7 @@ description: Simple Rack middleware for implementing google analytics tracking i
|
|
113
114
|
configurable load options.
|
114
115
|
email:
|
115
116
|
- lee.hambley@gmail.com
|
117
|
+
- lars@railslove.com
|
116
118
|
executables: []
|
117
119
|
extensions: []
|
118
120
|
extra_rdoc_files: []
|
@@ -121,14 +123,12 @@ files:
|
|
121
123
|
- lib/rack/google-analytics/version.rb
|
122
124
|
- lib/rack/google-analytics.rb
|
123
125
|
- lib/rack/templates/async.erb
|
124
|
-
- lib/rack/templates/sync.erb
|
125
126
|
- lib/rack-google-analytics.rb
|
126
|
-
- lib/tracking/custom_var.rb
|
127
127
|
- lib/tracking/event.rb
|
128
128
|
- lib/tracking/push.rb
|
129
129
|
- README.md
|
130
130
|
- LICENSE
|
131
|
-
homepage: https://github.com/
|
131
|
+
homepage: https://github.com/kangguru/rack-google-analytics
|
132
132
|
licenses:
|
133
133
|
- MIT
|
134
134
|
metadata: {}
|
@@ -143,9 +143,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
143
143
|
version: '0'
|
144
144
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
145
|
requirements:
|
146
|
-
- - ! '
|
146
|
+
- - ! '>'
|
147
147
|
- !ruby/object:Gem::Version
|
148
|
-
version:
|
148
|
+
version: 1.3.1
|
149
149
|
requirements: []
|
150
150
|
rubyforge_project:
|
151
151
|
rubygems_version: 2.1.5
|
data/lib/rack/templates/sync.erb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
<script type="text/javascript">
|
2
|
-
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
|
3
|
-
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
|
4
|
-
</script>
|
5
|
-
<script type="text/javascript">
|
6
|
-
try{
|
7
|
-
var pageTracker = _gat._getTracker(<%= @tracker.inspect %>);
|
8
|
-
<% if @options[:anonymize_ip] %>
|
9
|
-
_gat._anonymizeIp();
|
10
|
-
<% end %>
|
11
|
-
pageTracker._trackPageview();
|
12
|
-
} catch(err) {}</script>
|
data/lib/tracking/custom_var.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
module GoogleAnalytics
|
2
|
-
|
3
|
-
# A Struct that mirrors the structure of a custom var defined in Google Analytics
|
4
|
-
# see https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables
|
5
|
-
class CustomVar < Struct.new(:index, :name, :value, :opt_scope)
|
6
|
-
VISITOR_LEVEL = 1
|
7
|
-
SESSION_LEVEL = 2
|
8
|
-
PAGE_LEVEL = 3
|
9
|
-
|
10
|
-
def write
|
11
|
-
['_setCustomVar', self.index, self.name, self.value,self.opt_scope].to_json
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|