ga_events 1.4.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e1146cf9971d12382e0aa793841cd4dd0cd76470
4
- data.tar.gz: ff69c78fb46afe03a40685cf8f002c82161a3e27
2
+ SHA256:
3
+ metadata.gz: 2da2467a5f1cf24ef9fdcd30dfea8130a6e5a1c2a8ce6e0903143a909215e30d
4
+ data.tar.gz: b617a6d911b6666a8b1f0dabae680950565f956adccf26850a443a3109602c23
5
5
  SHA512:
6
- metadata.gz: b30b861e9b436375c7615d3f3b1aee65163a38236943eaa71482fa1ff6a6ead43929bc8e50ee91532006344995d7809122e4737e0f900e46254583b20aef5049
7
- data.tar.gz: 09e3a8b9a027f6cdeda57a5184a6d9b214edba3649bcdc6e666bf24f36d49d51b223a7c523234d47eb3b7c9a853ca77c4f92cbbdd42d54fc536430fe0a7b950d
6
+ metadata.gz: 805237d9af985bd8c4cb9d696626f4949d953dcabb3669b1b80771209811f13402f27e25aa618eda39bce2cfdf767ae9f74fa8336f789e5a5ee79a82e59e8026
7
+ data.tar.gz: 428a307570718be7d541f3c8883cc73141e88e7861e87a98ed5a4263ef8f41f657028220203298ea4ab5053fdbe9e1e85dc9fec59e26b50223ba5a94a0855a0a
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg
5
+ vendor
data/.rubocop.yml CHANGED
@@ -1,13 +1,8 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.2
2
+ TargetRubyVersion: 2.3
3
3
  Exclude:
4
4
  - 'vendor/bundle/**/*'
5
5
  DisplayCopNames: true
6
- Rails:
7
- Enabled: true
8
- Style/Encoding:
9
- EnforcedStyle: when_needed
10
- Enabled: true
11
6
  Style/AsciiComments:
12
7
  Enabled: false
13
8
  Style/Documentation:
@@ -16,13 +11,13 @@ Metrics/MethodLength:
16
11
  Enabled: false
17
12
  Style/MethodDefParentheses:
18
13
  Enabled: false
19
- Style/MultilineOperationIndentation:
14
+ Layout/MultilineOperationIndentation:
20
15
  EnforcedStyle: aligned
21
- Style/MultilineMethodCallIndentation:
16
+ Layout/MultilineMethodCallIndentation:
22
17
  EnforcedStyle: indented
23
- Style/MultilineMethodDefinitionBraceLayout:
18
+ Layout/MultilineMethodDefinitionBraceLayout:
24
19
  Enabled: true
25
- Style/MultilineHashBraceLayout:
20
+ Layout/MultilineHashBraceLayout:
26
21
  Enabled: true
27
22
  Style/SignalException:
28
23
  Enabled: false
@@ -38,3 +33,5 @@ Style/CommentAnnotation:
38
33
  Enabled: false
39
34
  Style/GuardClause:
40
35
  Enabled: false
36
+ Metrics/BlockLength:
37
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -33,3 +33,34 @@ The format suggested at http://keepachangelog.com/ is used.
33
33
  changes orchestrated by turbolinks, events are no longer processed via the
34
34
  injected DOM node. (Because the node would be cached and the event already
35
35
  processed.) Instead the the header-strategy is being used.
36
+
37
+ ## 1.4.1 - 2018-04-03
38
+
39
+ ### Fixed
40
+ - Ensure category, action and label do not include any umlauts or ß chars
41
+ - Fixed an error where events are remaining after a redirect
42
+
43
+ ## 2.0.0 - 2020-11-20
44
+
45
+ ### Removed
46
+ - Support for rubies < 2.3
47
+ - Support for rails < 4.2
48
+
49
+ ### Added
50
+ - Compatibility for Rails 5
51
+
52
+ ## 2.1.0 - 2021-09-07
53
+
54
+ ### Added
55
+ - Optional user consent flag to prevent event flushing until consent was given
56
+
57
+ ## 3.0.0 - 2022-01-10
58
+
59
+ ### Added
60
+ - Universal Analytics Adapter can be configured to use `gtag.js` instead of
61
+ `analytics.js` event syntax
62
+ - [Breaking] The constructor interface has changed due to more configuration
63
+ options that differ between `analytics.js` and `gtag.js`.
64
+ - Universal Analytics Adapter can be configured to use a custom name for the
65
+ analytics object (defaults to `window.ga()` for `analytics.js` and
66
+ `window.gtag()` for `gtag.js`)
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in ga_events.gemspec
@@ -6,4 +8,4 @@ gemspec
6
8
  gem 'pry'
7
9
  gem 'rake'
8
10
  gem 'rspec', '~> 3.1.0'
9
- gem 'rubocop', '~> 0.39.0', require: false
11
+ gem 'rubocop', '~> 0.79.0', require: false
data/README.md CHANGED
@@ -13,8 +13,8 @@ pushes it to Google Analytics via ga.js or Google Tag Manager.
13
13
 
14
14
  ## Dependencies
15
15
 
16
- * Ruby >= 1.9
17
- * Rails 3.1 onwards
16
+ * Ruby >= 2.3
17
+ * Rails 4.2 onwards
18
18
  * jQuery
19
19
 
20
20
  ## Installation
@@ -43,7 +43,9 @@ GaEvents.Event.adapter = function() {
43
43
  }
44
44
  ```
45
45
 
46
- ### Google Universal Analytics (analytics.js)
46
+ ### Google Universal Analytics
47
+
48
+ #### analytics.js
47
49
 
48
50
  ```javascript
49
51
  GaEvents.Event.adapter = function() {
@@ -51,11 +53,49 @@ GaEvents.Event.adapter = function() {
51
53
  }
52
54
  ```
53
55
 
54
- Optionally you can specify a custom method to call and a custom tracker name:
56
+ Optionally you can specify a custom send method to call and a custom tracker
57
+ name:
58
+
59
+ ```javascript
60
+ GaEvents.Event.adapter = function() {
61
+ return new GaEvents.GoogleUniversalAnalyticsAdapter(
62
+ {send_method_name: "sendNow", tracker_name: "customTracker"}
63
+ );
64
+ }
65
+ ```
66
+
67
+ #### gtag.js
55
68
 
56
69
  ```javascript
57
70
  GaEvents.Event.adapter = function() {
58
- return new GaEvents.GoogleUniversalAnalyticsAdapter("sendNow", "customTracker");
71
+ return new GaEvents.GoogleUniversalAnalyticsAdapter(
72
+ {use_gtag_variant: true}
73
+ );
74
+ }
75
+ ```
76
+
77
+ Optionally you can specify a custom tracker GA_MEASUREMENT_ID where you want
78
+ your events to be sent to:
79
+
80
+ ```javascript
81
+ GaEvents.Event.adapter = function() {
82
+ return new GaEvents.GoogleUniversalAnalyticsAdapter(
83
+ {use_gtag_variant: true, tracker_name: "GA_MEASUREMENT_ID"}
84
+ );
85
+ }
86
+ ```
87
+
88
+ #### Optional custom object name
89
+
90
+ The default names of the analytics object are `window.ga()` for `analytics.js`
91
+ and `window.gtag()` for `gtag.js`. If you have renamed your analytics object,
92
+ you can specify the name:
93
+
94
+ ```javascript
95
+ GaEvents.Event.adapter = function() {
96
+ return new GaEvents.GoogleUniversalAnalyticsAdapter(
97
+ {analytics_object_name: "analytics"} // calls window.analytics()
98
+ );
59
99
  }
60
100
  ```
61
101
 
@@ -70,6 +110,15 @@ GaEvents.Event.adapter = function() {
70
110
  }
71
111
  ```
72
112
 
113
+ ### Optional consent settings
114
+
115
+ Events are flushed immediatly by default. If you need to wait for user consent
116
+ you can set `GaEvents.Event.require_user_consent = true`.
117
+
118
+ With `require_user_consent` enabled all events are buffered until
119
+ `GaEvents.Event.user_consent_given = true` is set. Events are flushed as soon
120
+ as `GaEvents.Event.flush()` is called.
121
+
73
122
  ### Testing
74
123
 
75
124
  For your testing pleasure we included `NullAdapter`.
@@ -152,5 +201,7 @@ Yes please! Use pull requests.
152
201
  ## More docs and tools
153
202
 
154
203
  * [Google Analytics: Event Tracking](https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide)
204
+ * [Google Universal Analytics: Event Tracking (analytics.js)](https://developers.google.com/analytics/devguides/collection/analyticsjs/events)
205
+ * [Google Universal Analytics: Event Tracking (gtag.js)](https://developers.google.com/analytics/devguides/collection/gtagjs/events)
155
206
  * [Google Tag Manager: Custom Events](http://support.google.com/tagmanager/answer/2574372#GoogleAnalytics)
156
- * [Chrome Web Store: Event Tracking Tracker](https://chrome.google.com/webstore/detail/event-tracking-tracker/npjkfahkbgoagkfpkidpjdemjjmmbcim)
207
+ * [Chrome Web Store: Tag Assistant](https://chrome.google.com/webstore/detail/tag-assistant-legacy-by-g/kejbdjndbnbjgmefkgdddjlbokphdefk)
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
- #!/usr/bin/env rake
1
+ # frozen_string_literal: true
2
+
2
3
  require 'bundler/gem_tasks'
3
4
  require 'rspec/core/rake_task'
4
5
 
@@ -9,9 +9,11 @@ class GaEvents.Event
9
9
  @may_flush: false
10
10
  @header_key: "X-GA-Events"
11
11
  @html_key: "ga-events"
12
+ @require_user_consent: false
13
+ @user_consent_given: false
12
14
  klass: @
13
15
 
14
- # Decompose a event-string (ruby side) into an event object.
16
+ # Decompose an event-string (ruby side) into an event object.
15
17
  @from_string: (string) ->
16
18
  $.map string.split("$"), (part) =>
17
19
  [category, action, label, value] = part.split "|"
@@ -21,8 +23,10 @@ class GaEvents.Event
21
23
  dom_events = $("div[data-#{@html_key}]").data @html_key
22
24
  @from_string dom_events if dom_events?
23
25
 
24
- # Events should not be send to an adapter unless the DOM has finished loading.
26
+ # Events should not be sent to an adapter unless the DOM has finished loading.
25
27
  @flush: ->
28
+ return if @require_user_consent && !@user_consent_given
29
+
26
30
  if @list.length > 0 and @may_flush
27
31
  $.map @list, (event) -> event.push_to_adapter()
28
32
  @list = []
@@ -32,18 +36,27 @@ class GaEvents.Event
32
36
  @klass.list.push @
33
37
  @klass.flush()
34
38
 
39
+ escape: (str) ->
40
+ return unless str
41
+ "#{str}".replace(/ä/g, "ae")
42
+ .replace(/ö/g, "oe")
43
+ .replace(/ü/g, "ue")
44
+ .replace(/Ä/g, "Ae")
45
+ .replace(/Ö/g, "Oe")
46
+ .replace(/Ü/g, "Ue")
47
+ .replace(/ß/g, "ss")
48
+
35
49
  to_hash: ->
36
- # Category, action and label must be of type string.
37
- action: "#{@action}"
38
- category: "#{@category}"
39
- label: "#{@label}"
50
+ # Category, action and label must be escaped and of type string.
51
+ action: @escape(@action)
52
+ category: @escape(@category)
53
+ label: @escape(@label)
40
54
  # Value has to be a positive integer or defaults to 1
41
55
  value: @to_positive_integer(@value)
42
56
 
43
57
  to_positive_integer: (n) ->
44
58
  if isFinite(n) and parseInt(n) >= 0 then parseInt n else 1
45
59
 
46
- # https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#SettingUpEventTracking
47
60
  push_to_adapter: -> @klass.adapter().push @to_hash()
48
61
 
49
62
  jQuery =>
@@ -70,18 +83,45 @@ class GaEvents.GoogleTagManagerAdapter
70
83
  window.dataLayer.push data
71
84
 
72
85
  class GaEvents.GoogleUniversalAnalyticsAdapter
73
- constructor: (@method_call_name = "send", tracker_name) ->
74
- @method_call_name = "#{tracker_name}.#{@method_call_name}" if tracker_name
86
+ constructor: (options) ->
87
+ @use_gtag_variant = options?.use_gtag_variant || false
88
+ @analytics_object_name =
89
+ options?.analytics_object_name ||
90
+ (if @use_gtag_variant then "gtag" else "ga")
75
91
 
76
- push: (h) ->
77
- window.ga @method_call_name, "event", h.category, h.action, h.label,
78
- h.value, {"nonInteraction": true}
92
+ # Only relevant for analytics.js
93
+ @send_method_name = options?.send_method_name || "send"
94
+
95
+ # https://developers.google.com/analytics/devguides/migration/ua/analyticsjs-to-gtagjs#measure_pageviews_with_specified_trackers
96
+ @tracker_name = options?.tracker_name || false
97
+ push: (data) ->
98
+ if @use_gtag_variant
99
+ options = {
100
+ "event_category": data.category,
101
+ "event_label": data.label,
102
+ "value": data.value,
103
+ "non_interaction": true
104
+ }
105
+ options["send_to"] = @tracker_name if @tracker_name
106
+ window[@analytics_object_name]("event", data.action, options)
107
+ else
108
+ method_call_name =
109
+ if @tracker_name
110
+ "#{@tracker_name}.#{@send_method_name}"
111
+ else
112
+ @send_method_name
113
+ window[@analytics_object_name](
114
+ method_call_name, "event",
115
+ data.category, data.action, data.label, data.value,
116
+ {"nonInteraction": true}
117
+ )
79
118
 
80
119
  class GaEvents.GoogleAnalyticsAdapter
120
+ # https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#SettingUpEventTracking
81
121
  # Send events non_interactive => no influence on bounce rates
82
- push: (h) ->
122
+ push: (data) ->
83
123
  window._gaq.push(
84
- ["_trackEvent", h.category, h.action, h.label, h.value, true]
124
+ ["_trackEvent", data.category, data.action, data.label, data.value, true]
85
125
  )
86
126
 
87
127
  class GaEvents.NullAdapter
data/ga_events.gemspec CHANGED
@@ -1,5 +1,6 @@
1
- # encoding: utf-8
2
- require File.expand_path('../lib/ga_events/version', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('lib/ga_events/version', __dir__)
3
4
 
4
5
  Gem::Specification.new do |gem|
5
6
  gem.authors = ['Florian Dütsch', 'Sven Winkler']
@@ -24,6 +25,8 @@ Gem::Specification.new do |gem|
24
25
  gem.name = 'ga_events'
25
26
  gem.require_paths = ['lib']
26
27
  gem.version = GaEvents::VERSION
28
+ gem.licenses = ['MIT']
27
29
 
28
- gem.add_dependency 'rails', '>= 3.1'
30
+ gem.required_ruby_version = '>= 2.3'
31
+ gem.add_dependency 'rails', '>= 4.2'
29
32
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GaEvents
2
4
  class Engine < ::Rails::Engine
3
5
  config.app_middleware.use GaEvents::Middleware
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GaEvents
2
4
  Event = Struct.new(:category, :action, :label, :value) do
3
5
  # Default values are set here, see README.md for details.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # NOTE: Collecting the events is thread-safe, but will cause problems in an
2
4
  # asynchronous/evented environment.
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rack/utils'
2
4
 
3
5
  module GaEvents
@@ -35,31 +37,29 @@ module GaEvents
35
37
  private
36
38
 
37
39
  def init_event_list(env)
38
- # Handle events stored in flash
39
- # Parts borrowed from Rails:
40
- # https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_dispatch/middleware/flash.rb
41
- flash = env['rack.session'] && env['rack.session']['flash']
42
-
43
- # Fix for Rails 4
44
- flash &&= flash['flashes'] if Rails::VERSION::MAJOR > 3
45
-
46
- GaEvents::List.init(flash && flash['ga_events'])
40
+ flash = env['rack.session'] && env['rack.session']['flash'] &&
41
+ env['rack.session']['flash']['flashes']
42
+
43
+ # The key has to be removed from the flash here to ensure it does not
44
+ # remain after the finished redirect. This copies the behaviour of the
45
+ # "#use" and "#sweep" methods of the rails flash middleware:
46
+ # https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_dispatch/middleware/flash.rb#L220
47
+ GaEvents::List.init(flash&.delete('ga_events'))
47
48
  end
48
49
 
49
50
  def add_events_to_flash env, serialized_data
50
- flash_hash = env[ActionDispatch::Flash::KEY]
51
- flash_hash ||= ActionDispatch::Flash::FlashHash.new
52
- flash_hash['ga_events'] = serialized_data
53
- # Discard the flash after the action completes.
54
- flash_hash.discard('ga_events')
51
+ flash = env['rack.session'] && env['rack.session']['flash'] &&
52
+ env['rack.session']['flash']['flashes']
53
+
54
+ return unless flash
55
55
 
56
- env[ActionDispatch::Flash::KEY] = flash_hash
56
+ flash['ga_events'] = serialized_data
57
57
  end
58
58
 
59
- def normalize_response(r)
60
- r = r.body if r.respond_to?(:body)
61
- r = r.join if r.respond_to?(:join)
62
- r
59
+ def normalize_response(response)
60
+ response = response.body if response.respond_to?(:body)
61
+ response = response.join if response.respond_to?(:join)
62
+ response
63
63
  end
64
64
 
65
65
  def inject_div(response, serialized_data)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GaEvents
2
- VERSION = '1.4.0'.freeze
4
+ VERSION = '3.0.0'
3
5
  end
data/lib/ga_events.rb CHANGED
@@ -1 +1,3 @@
1
- %w(middleware engine event list version).each { |f| require "ga_events/#{f}" }
1
+ # frozen_string_literal: true
2
+
3
+ %w[middleware engine event list version].each { |f| require "ga_events/#{f}" }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe GaEvents::Middleware do
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/setup'
2
4
  require 'pry'
3
5
  require 'rspec'
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ga_events
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Dütsch
8
8
  - Sven Winkler
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-03-12 00:00:00.000000000 Z
12
+ date: 2022-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '3.1'
20
+ version: '4.2'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: '3.1'
27
+ version: '4.2'
28
28
  description: Google Analytics' Event Tracking everywhere in your Rails app)
29
29
  email:
30
30
  - florian.duetsch@nix-wie-weg.de
@@ -52,9 +52,10 @@ files:
52
52
  - spec/middleware_spec.rb
53
53
  - spec/spec_helper.rb
54
54
  homepage: https://github.com/Nix-wie-weg/ga_events
55
- licenses: []
55
+ licenses:
56
+ - MIT
56
57
  metadata: {}
57
- post_install_message:
58
+ post_install_message:
58
59
  rdoc_options: []
59
60
  require_paths:
60
61
  - lib
@@ -62,16 +63,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
63
  requirements:
63
64
  - - ">="
64
65
  - !ruby/object:Gem::Version
65
- version: '0'
66
+ version: '2.3'
66
67
  required_rubygems_version: !ruby/object:Gem::Requirement
67
68
  requirements:
68
69
  - - ">="
69
70
  - !ruby/object:Gem::Version
70
71
  version: '0'
71
72
  requirements: []
72
- rubyforge_project:
73
- rubygems_version: 2.4.5.1
74
- signing_key:
73
+ rubyforge_project:
74
+ rubygems_version: 2.7.6.2
75
+ signing_key:
75
76
  specification_version: 4
76
77
  summary: This gem allows you to annotate events everywhere in the code of your Rails
77
78
  app. A rack middleware is automatically inserted into the stack. It transports the