ga_events 1.3.1 → 2.1.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: 5f618bcb8b6143f0b27f0635ba07f7a03bf43314
4
- data.tar.gz: 243dbd6ab50485f12ef605804645f31a216b57c7
2
+ SHA256:
3
+ metadata.gz: 205433864fd2efabf0cf787da0be314c9bc48fd644773d8df29f6ca66327ba43
4
+ data.tar.gz: fa05cd8ee83f4b73fa23a3562e19d0ec2fcbb1a684c7ee49c357d0e78a54c3a7
5
5
  SHA512:
6
- metadata.gz: 5f6d47efb06212449d9c5d73ef7b7a9dd7fc159ec96083791e161bccfba041e153442b3eef6d9c4cf785bf60a1d465e740b78158e4a2e14ed360ff8f07c8d65d
7
- data.tar.gz: 72d65902c21450bcb1f6e2220d7d01680a3c1745bfdd38d57785831a5c93d8bc07536082a630b2d7f45049ad1a791cb27ca9699261df1afe2931b0bd2d1c0baa
6
+ metadata.gz: 472515474d12c53c4f3d43b8c577a45776ed983149c8347940294ca1128e907d101f48054eee7c00468cafdc3f1e7aba33c3cdfa08f43d81ebc0bd25c91f884f
7
+ data.tar.gz: 483bbdb595f20e852ec8f6b74366251e91e84b76cde7454721136d42cfd31586e36c88bd015ac6cbf321a673e2779e5f29b96cb345924d7606f648e36da67aa9
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg
5
+ vendor
data/.rubocop.yml ADDED
@@ -0,0 +1,37 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - 'vendor/bundle/**/*'
5
+ DisplayCopNames: true
6
+ Style/AsciiComments:
7
+ Enabled: false
8
+ Style/Documentation:
9
+ Enabled: false
10
+ Metrics/MethodLength:
11
+ Enabled: false
12
+ Style/MethodDefParentheses:
13
+ Enabled: false
14
+ Layout/MultilineOperationIndentation:
15
+ EnforcedStyle: aligned
16
+ Layout/MultilineMethodCallIndentation:
17
+ EnforcedStyle: indented
18
+ Layout/MultilineMethodDefinitionBraceLayout:
19
+ Enabled: true
20
+ Layout/MultilineHashBraceLayout:
21
+ Enabled: true
22
+ Style/SignalException:
23
+ Enabled: false
24
+ Metrics/ClassLength:
25
+ Enabled: false
26
+ Lint/AssignmentInCondition:
27
+ Enabled: false
28
+ Metrics/ParameterLists:
29
+ Enabled: false
30
+ Style/MultilineBlockChain:
31
+ Enabled: false
32
+ Style/CommentAnnotation:
33
+ Enabled: false
34
+ Style/GuardClause:
35
+ Enabled: false
36
+ Metrics/BlockLength:
37
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -25,3 +25,26 @@ The format suggested at http://keepachangelog.com/ is used.
25
25
 
26
26
  ### Added
27
27
  - Support for turbolinks.
28
+
29
+ ## 1.4.0 - 2018-03-09
30
+
31
+ ### Fixed
32
+ - Fixed turbolinks events. To prevent older events the be processed again after page
33
+ changes orchestrated by turbolinks, events are no longer processed via the
34
+ injected DOM node. (Because the node would be cached and the event already
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 - 2018-04-03
44
+
45
+ ### Removed
46
+ - Support for rubies < 2.3
47
+ - Support for rails < 4.2
48
+
49
+ ### Added
50
+ - Compatibility for Rails 5
data/Gemfile CHANGED
@@ -1,8 +1,11 @@
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
4
6
  gemspec
5
7
 
8
+ gem 'pry'
6
9
  gem 'rake'
7
10
  gem 'rspec', '~> 3.1.0'
8
- gem 'pry'
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
@@ -70,6 +70,15 @@ GaEvents.Event.adapter = function() {
70
70
  }
71
71
  ```
72
72
 
73
+ ### Optional consent settings
74
+
75
+ Events are flushed immediatly by default. If you need to wait for user consent
76
+ you can set `GaEvents.Event.require_user_consent = true`.
77
+
78
+ With `require_user_consent` enabled all events are buffered until
79
+ `GaEvents.Event.user_consent_given = true` is set. Events are flushed as soon
80
+ as `GaEvents.Event.flush()` is called.
81
+
73
82
  ### Testing
74
83
 
75
84
  For your testing pleasure we included `NullAdapter`.
data/Rakefile CHANGED
@@ -1,7 +1,3 @@
1
- #!/usr/bin/env rake
2
- require 'bundler/gem_tasks'
3
- require 'rspec/core/rake_task'
4
-
5
- RSpec::Core::RakeTask.new('spec')
1
+ # frozen_string_literal: true
6
2
 
7
- task default: :spec
3
+ require 'bundler/gem_tasks'
@@ -9,6 +9,8 @@ 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
16
  # Decompose a event-string (ruby side) into an event object.
@@ -23,6 +25,8 @@ class GaEvents.Event
23
25
 
24
26
  # Events should not be send 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,11 +36,21 @@ 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
 
@@ -50,12 +64,17 @@ class GaEvents.Event
50
64
  @may_flush = true
51
65
  @flush()
52
66
 
53
- $(document).ajaxComplete (event, xhr) =>
67
+ process_xhr = (xhr) =>
54
68
  xhr_events = xhr.getResponseHeader @header_key
55
69
  @from_string xhr_events if xhr_events?
56
70
 
71
+ $(document).ajaxComplete((_, xhr) -> process_xhr(xhr))
72
+ $(document).on "turbolinks:request-end", (event) ->
73
+ xhr = event.originalEvent.data.xhr
74
+ process_xhr(xhr)
75
+
57
76
  @from_dom()
58
- $(document).on "turbolinks:load", => @from_dom()
77
+
59
78
 
60
79
  class GaEvents.GoogleTagManagerAdapter
61
80
  constructor: (@event = "ga_event") ->
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']
@@ -19,11 +20,11 @@ Gem::Specification.new do |gem|
19
20
  gem.homepage = 'https://github.com/Nix-wie-weg/ga_events'
20
21
 
21
22
  gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
22
- gem.executables = gem.files.grep(/^bin\//).map { |f| File.basename(f) }
23
- gem.test_files = gem.files.grep(/^(test|spec|features)\//)
23
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
24
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
24
25
  gem.name = 'ga_events'
25
26
  gem.require_paths = ['lib']
26
27
  gem.version = GaEvents::VERSION
27
28
 
28
- gem.add_dependency 'rails', '>= 3.1'
29
+ gem.add_dependency 'rails', '>= 4.2'
29
30
  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,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GaEvents
2
- class Event < Struct.new(:category, :action, :label, :value)
4
+ Event = Struct.new(:category, :action, :label, :value) do
3
5
  # Default values are set here, see README.md for details.
4
6
  def initialize(category = '-', action = '-', label = '-', value = 1)
5
7
  super
@@ -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
@@ -16,11 +18,11 @@ module GaEvents
16
18
 
17
19
  # Can outgrow, headers might get too big
18
20
  serialized = GaEvents::List.to_s
19
- if request.xhr?
21
+ if xhr_or_turbolinks?(request)
20
22
  # AJAX request
21
23
  headers['X-GA-Events'] = serialized
22
24
 
23
- elsif (300..399).include?(status)
25
+ elsif redirect?(status)
24
26
  # 30x/redirect? Then add event list to flash to survive the redirect.
25
27
  add_events_to_flash(env, serialized)
26
28
 
@@ -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)
@@ -74,5 +74,13 @@ module GaEvents
74
74
  headers.key?('Content-Type') &&
75
75
  headers['Content-Type'].include?('text/html')
76
76
  end
77
+
78
+ def redirect?(status)
79
+ (300..399).cover?(status)
80
+ end
81
+
82
+ def xhr_or_turbolinks?(request)
83
+ request.xhr? || request.env['HTTP_TURBOLINKS_REFERRER']
84
+ end
77
85
  end
78
86
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GaEvents
2
- VERSION = '1.3.1'
4
+ VERSION = '2.1.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.3.1
4
+ version: 2.1.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-01 00:00:00.000000000 Z
12
+ date: 2021-09-07 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
@@ -34,6 +34,7 @@ extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
36
  - ".gitignore"
37
+ - ".rubocop.yml"
37
38
  - CHANGELOG.md
38
39
  - Gemfile
39
40
  - LICENSE
@@ -53,7 +54,7 @@ files:
53
54
  homepage: https://github.com/Nix-wie-weg/ga_events
54
55
  licenses: []
55
56
  metadata: {}
56
- post_install_message:
57
+ post_install_message:
57
58
  rdoc_options: []
58
59
  require_paths:
59
60
  - lib
@@ -68,9 +69,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
69
  - !ruby/object:Gem::Version
69
70
  version: '0'
70
71
  requirements: []
71
- rubyforge_project:
72
- rubygems_version: 2.5.2
73
- signing_key:
72
+ rubygems_version: 3.0.3
73
+ signing_key:
74
74
  specification_version: 4
75
75
  summary: This gem allows you to annotate events everywhere in the code of your Rails
76
76
  app. A rack middleware is automatically inserted into the stack. It transports the