ga_events 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODBkOTkyODRlNzNiN2Y0YmExMDdkZmVmOGYyZThjZTliMzVjMjIyYw==
4
+ YTJhMGE3MTIxYTVhNDZlOGI2ZjhmZjgwNDFkZjU4M2ZhYmUxZmQzYg==
5
5
  data.tar.gz: !binary |-
6
- ZTY5NzA1ODEyZjZiZmJjZjhiZjgxYzM3MzdhMzQ1M2E0NzRmOWE2Mg==
6
+ N2QzYzE4MWM4NjY0ZjY4MWY4MDk5ZTlmYmU4ZDY1ODZiODNhYTI0Zg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YjIzMWFjMjU0MjZjYmE0ZDQ2MGZlMWZlZTUyMzI0OTQ4YzA1MzVlNDcwNzNl
10
- ZDQyMmQ5ZDIxYjJjOTgyNmJiMjhiZDc1MTkxOWJjOTA4N2RkZWE3ZThiZGI5
11
- NmZjY2RhZmQzZjA5Yzc0MjMyYjFiZjE3OGY4OTE1NWI5N2YzY2I=
9
+ MmJmMjJlY2EwOWE4ZDA4N2QwM2QwZjZlM2M5OGIzMWUwZjEzNWFjZGY5NTc3
10
+ YTNmMjkwMmI0ODViMGJlOTNhYjMyZWVhNmQ5NGEzOGRhOGEyOWUyOTRjZjEx
11
+ ZjFhOTZmMzRiMWEwZTZkMjEwZDllNzlmMTA3YzhjOTQ2MzU0YTg=
12
12
  data.tar.gz: !binary |-
13
- ZmNmNDRlMTI1ZDQ3NTk5YThjZjE4MTY3Zjk2NThlMmQ5ZjhjMjFmYWU2OWVl
14
- ZGQ1MjljNmMzZGI5ZWM2ZjZmODM1YTYxZGQ0NTIwNTZlNGQwM2YzYWE0NDc0
15
- NzM1ZGI0ZWU1ZTFiODVkYTZiNmQ2MWE1YjRmYjliYThmN2E2YTA=
13
+ ZDM3NmFiY2Q1OGFjMTM2ZDQ3NDE2Yzg0NDU1ZTI4MGU0NWU0Yjk3MzcwY2E2
14
+ ZGY3OWVkMzcwMzdlMTM1MzgwMDhmMGE4MTllZjUyM2VjYjFmZTUzYTM4YWFj
15
+ OTRkMzU1NTFjYTA2ZDA5MzNiNzJlYTliYjAwZjgwZTU4ZGRmNDQ=
data/README.md CHANGED
@@ -6,13 +6,19 @@ This gem alllows you to annotate events everywhere in the code of your Rails
6
6
  app.
7
7
  A rack middleware is automatically inserted into the stack. It transports
8
8
  the event data to the client. Normal requests get a DIV injected, Ajax requests
9
- get a data-pounded custom HTTP header appended.
9
+ get a data-pounded custom HTTP header appended. In case of redirects the data
10
+ survives inside Rails' flash.
10
11
  The asset pipeline-ready CoffeeScript extracts this data on the client-side and
11
12
  pushes it to Google Analytics via ga.js or Google Tag Manager.
12
13
 
14
+ ## Dependencies
15
+
16
+ * Rails 3.1 onwards
17
+ * jQuery
18
+
13
19
  ## Installation
14
20
 
15
- GaEvents works with Rails 3.1 onwards. You can add it to your `Gemfile` with:
21
+ Add it to your `Gemfile` with:
16
22
 
17
23
  ```ruby
18
24
  gem 'ga_events'
@@ -20,7 +26,7 @@ gem 'ga_events'
20
26
 
21
27
  Run the `bundle` command to install it.
22
28
 
23
- Add to the top of your `application.js`:
29
+ Add to the top of your `application.js` (but after requiring jQuery):
24
30
 
25
31
  ```javascript
26
32
  //= require ga_events.js
@@ -8,6 +8,9 @@ module GaEvents
8
8
  def to_s
9
9
  [category, action, label, value].join('|')
10
10
  end
11
+
12
+ def self.from_string(str)
13
+ new(*str.split('|'))
14
+ end
11
15
  end
12
16
  end
13
-
@@ -14,8 +14,10 @@ module GaEvents::List
14
14
  data.present?
15
15
  end
16
16
 
17
- def self.init
17
+ # Init list, optionally with a string of serialized events
18
+ def self.init(str = nil)
18
19
  Thread.current[:ga_events] = []
20
+ (str || '').split('$').each { |s| GaEvents::Event.from_string(s) }
19
21
  end
20
22
 
21
23
  def self.data
@@ -2,15 +2,19 @@ require 'rack/utils'
2
2
 
3
3
  module GaEvents
4
4
  class Middleware
5
-
6
5
  def initialize(app)
7
6
  @app = app
8
7
  end
9
8
  def call(env)
10
- GaEvents::List.init
9
+ # Handle events stored in flash
10
+ # Parts borrowed from Rails:
11
+ # https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_dispatch/middleware/flash.rb
12
+ flash = env['rack.session'] && env['rack.session']['flash']
13
+ GaEvents::List.init(flash && flash['ga_events'])
14
+
11
15
  status, headers, response = @app.call(env)
12
- headers = Rack::Utils::HeaderHash.new(headers)
13
16
 
17
+ headers = Rack::Utils::HeaderHash.new(headers)
14
18
  if GaEvents::List.present?
15
19
  request = Rack::Request.new(env)
16
20
 
@@ -18,10 +22,19 @@ module GaEvents
18
22
  serialized = GaEvents::List.to_s
19
23
 
20
24
  if request.xhr?
25
+ # AJAX request
21
26
  headers['X-GA-Events'] = serialized
27
+
28
+ elsif (300..399).include?(status)
29
+ # 30x/redirect? Then add event list to flash to survive the redirect.
30
+ flash_hash = env[ActionDispatch::Flash::KEY]
31
+ flash_hash ||= ActionDispatch::Flash::FlashHash.new
32
+ flash_hash['ga_events'] = serialized
33
+ env[ActionDispatch::Flash::KEY] = flash_hash
34
+
22
35
  elsif is_html?(status, headers)
23
36
  body = response
24
- body = body.each.to_a.join('') if body.respond_to?(:each)
37
+ body = body.each.to_a.join if body.respond_to?(:each)
25
38
  body = body.sub('</body>',
26
39
  "<div data-ga-events='#{serialized}'></div>\\0")
27
40
  response = [body]
@@ -1,3 +1,3 @@
1
1
  module GaEvents
2
- VERSION = "0.1.7"
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ga_events
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Dütsch
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-02 00:00:00.000000000 Z
13
+ date: 2013-09-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  version: '0'
69
69
  requirements: []
70
70
  rubyforge_project:
71
- rubygems_version: 2.0.3
71
+ rubygems_version: 2.0.7
72
72
  signing_key:
73
73
  specification_version: 4
74
74
  summary: This gem allows you to annotate events everywhere in the code of your Rails