ga_events 0.2.0 → 1.0.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
- YTJhMGE3MTIxYTVhNDZlOGI2ZjhmZjgwNDFkZjU4M2ZhYmUxZmQzYg==
4
+ NDljN2FmMjUyMGVlZTFiY2ZjZGFlYmQ5ZWQ0NmM1NzQxMTBkNmVkMw==
5
5
  data.tar.gz: !binary |-
6
- N2QzYzE4MWM4NjY0ZjY4MWY4MDk5ZTlmYmU4ZDY1ODZiODNhYTI0Zg==
6
+ MGE5NzUxMDdkYzA3MjJjYWNhZmI4MDM0ZjcwMzc5OTc4M2RlNjdjYQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MmJmMjJlY2EwOWE4ZDA4N2QwM2QwZjZlM2M5OGIzMWUwZjEzNWFjZGY5NTc3
10
- YTNmMjkwMmI0ODViMGJlOTNhYjMyZWVhNmQ5NGEzOGRhOGEyOWUyOTRjZjEx
11
- ZjFhOTZmMzRiMWEwZTZkMjEwZDllNzlmMTA3YzhjOTQ2MzU0YTg=
9
+ ZGJlYTU3ZTExOGJmMTdlY2Q1Yzk2M2VjZDExMGNiMmYxMjg1OGExZDBmNWU4
10
+ OWJmNmJjNDZkYTk1YWI1NmY0ODFhMGFkZGIyNGIyYWU0NzVlNmE3YzM0YmEy
11
+ M2E3NjZiOTYxYzg2M2M3YjljNmNjMzExY2RjMmQ3MGMzOTBiMGQ=
12
12
  data.tar.gz: !binary |-
13
- ZDM3NmFiY2Q1OGFjMTM2ZDQ3NDE2Yzg0NDU1ZTI4MGU0NWU0Yjk3MzcwY2E2
14
- ZGY3OWVkMzcwMzdlMTM1MzgwMDhmMGE4MTllZjUyM2VjYjFmZTUzYTM4YWFj
15
- OTRkMzU1NTFjYTA2ZDA5MzNiNzJlYTliYjAwZjgwZTU4ZGRmNDQ=
13
+ YWE3ZTdkZGE3ZjkwNThhYjFhZjhhMDVjMDNjN2E5NzcyN2JhZDI2Yjg5MDA3
14
+ OTJmOTg0YWY5YzI4NjgxMTEzYTg1ODBkOTk1NWU1M2RmNmMxODU4ODUzOTU4
15
+ MjlkYTFjZmZkNWJlMzQ3MzBlOWI3YjllMzc3NThhZWVkYTZiYmE=
data/README.md CHANGED
@@ -42,7 +42,16 @@ GaEvents.Event.adapter = function() {
42
42
  }
43
43
  ```
44
44
 
45
- If you are using Google Tag Manager you can add custom events which are then passed through to Google Analytics.
45
+ For Google Universal Analytics use:
46
+
47
+ ```javascript
48
+ GaEvents.Event.adapter = function() {
49
+ return new GaEvents.GoogleUniversalAnalyticsAdapter();
50
+ }
51
+ ```
52
+
53
+ If you are using Google Tag Manager you can add custom events which are then
54
+ passed through to Google Analytics.
46
55
 
47
56
  ```javascript
48
57
  GaEvents.Event.adapter = function() {
@@ -79,6 +88,12 @@ new GaEvents.Event(category, action, label, value)
79
88
  We have taken special care of tracking events while the DOM is loading.
80
89
  Events get collected until the DOM is ready and flushed afterwards.
81
90
 
91
+ ### Default values
92
+
93
+ While collecting hundreds of thousands of events on a daily basis in
94
+ Google Analytics we found corrupted aggregated events when the event label or
95
+ value is omitted. We now enforce a default label ("-") and value (1).
96
+
82
97
  ### Too many events
83
98
 
84
99
  Use something like this snippet to get informed of bloating HTTP headers with
@@ -101,6 +116,10 @@ end
101
116
 
102
117
  Yes please! Use pull requests.
103
118
 
119
+ ### Credits
120
+
121
+ * [jhilden](https://github.com/jhilden) for ideas and Rails 4 support
122
+
104
123
  ## More docs and tools
105
124
 
106
125
  * [Google Analytics: Event Tracking](https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide)
data/TODO CHANGED
@@ -1,3 +1,2 @@
1
- v0.2
2
1
  * Tests
3
2
  * Announcement
@@ -24,34 +24,23 @@ class GaEvents.Event
24
24
  @list = []
25
25
 
26
26
  # Add all events to a queue to flush them later
27
- constructor: (@category, @action, @label, @value) ->
27
+ constructor: (@category = "-", @action = "-", @label = "-", @value = 1) ->
28
28
  @klass.list.push @
29
29
  @klass.flush()
30
30
 
31
- push_to_adapter: ->
32
- # https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#SettingUpEventTracking
33
- # Category, Action and Label must be of type string.
34
- # Value must be a positive integer.
31
+ to_hash: ->
32
+ # Category, action and label must be of type string.
33
+ action: "#{@action}"
34
+ category: "#{@category}"
35
+ label: "#{@label}"
36
+ # Value has to be a positive integer or defaults to 1
37
+ value: @to_positive_integer(@value)
35
38
 
36
- data =
37
- action: "#{@action}"
38
- category: "#{@category}"
39
- data.label = "#{@label}" if @is_present @label
39
+ to_positive_integer: (n) ->
40
+ if isFinite(n) and parseInt(n) >= 0 then parseInt n else 1
40
41
 
41
- if @is_present @value
42
- # @value is a number and of type integer.
43
- if isFinite(@value) and Number(@value) % 1 is 0
44
- # Google Analytics expects a positive integer
45
- if (value = parseInt @value) > -1
46
- data.value = value
47
- else
48
- throw "Negative integers are not supported at this time."
49
- else
50
- throw "The parameter 'value' must be of type integer."
51
-
52
- @klass.adapter().push data
53
-
54
- is_present: (value) -> value? and value != ""
42
+ # https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#SettingUpEventTracking
43
+ push_to_adapter: -> @klass.adapter().push @to_hash()
55
44
 
56
45
  jQuery =>
57
46
  @may_flush = true
@@ -67,17 +56,18 @@ class GaEvents.Event
67
56
  class GaEvents.GoogleTagManagerAdapter
68
57
  constructor: (@event = "ga_event") ->
69
58
  push: (data) ->
70
- data["event"] = @event
71
- data["non_interaction"] = true
59
+ data.event = @event
60
+ data.non_interaction = true
72
61
  window.dataLayer.push data
73
62
 
63
+ class GaEvents.GoogleUniversalAnalyticsAdapter
64
+ push: (h) -> window.ga "send", "event", h.category, h.action, h.label, h.value
65
+
74
66
  class GaEvents.GoogleAnalyticsAdapter
75
- push: (obj) ->
76
- data = ["_trackEvent", obj["category"], obj["action"]]
77
- data.push obj["label"]
78
- data.push obj["value"]
79
- data.push true # opt_noninteraction
80
- window._gaq.push data
67
+ # Send events non_interactive => no influence on bounce rates
68
+ push: (h) ->
69
+ window._gaq.push(
70
+ ["_trackEvent", h.category, h.action, h.label, h.value, true])
81
71
 
82
72
  class GaEvents.NullAdapter
83
73
  push: (obj) -> console.log obj if console?
data/ga_events.gemspec CHANGED
@@ -26,5 +26,5 @@ Gem::Specification.new do |gem|
26
26
  gem.require_paths = ["lib"]
27
27
  gem.version = GaEvents::VERSION
28
28
 
29
- gem.add_dependency 'rails', '~> 3.1'
29
+ gem.add_dependency 'rails', '>= 3.1'
30
30
  end
@@ -1,6 +1,7 @@
1
1
  module GaEvents
2
2
  class Event < Struct.new(:category, :action, :label, :value)
3
- def initialize(category, action, label = nil, value = nil)
3
+ # Default values are set here, see README.md for details.
4
+ def initialize(category = '-', action = '-', label = '-', value = 1)
4
5
  super
5
6
  GaEvents::List << self
6
7
  end
@@ -1,5 +1,5 @@
1
1
  # NOTE: Collecting the events is thread-safe, but will cause problems in an
2
- # asynchronous environment.
2
+ # asynchronous/evented environment.
3
3
 
4
4
  module GaEvents::List
5
5
  def self.<<(event)
@@ -10,6 +10,10 @@ module GaEvents
10
10
  # Parts borrowed from Rails:
11
11
  # https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_dispatch/middleware/flash.rb
12
12
  flash = env['rack.session'] && env['rack.session']['flash']
13
+
14
+ # Fix for Rails 4
15
+ flash &&= flash['flashes'] if Rails::VERSION::MAJOR > 3
16
+
13
17
  GaEvents::List.init(flash && flash['ga_events'])
14
18
 
15
19
  status, headers, response = @app.call(env)
@@ -20,7 +24,6 @@ module GaEvents
20
24
 
21
25
  # Can outgrow, headers might get too big
22
26
  serialized = GaEvents::List.to_s
23
-
24
27
  if request.xhr?
25
28
  # AJAX request
26
29
  headers['X-GA-Events'] = serialized
@@ -55,4 +58,3 @@ module GaEvents
55
58
  end
56
59
  end
57
60
  end
58
-
@@ -1,3 +1,3 @@
1
1
  module GaEvents
2
- VERSION = '0.2.0'
2
+ VERSION = '1.0.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.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Dütsch
@@ -10,20 +10,20 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-09-09 00:00:00.000000000 Z
13
+ date: 2013-10-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - ~>
26
+ - - ! '>='
27
27
  - !ruby/object:Gem::Version
28
28
  version: '3.1'
29
29
  description: Google Analytics' Event Tracking everywhere in your Rails app