ga_events 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8733925b29ae71da3da081d25679727857f92ee7bcb5390c72b97a87fc3c9b53
4
+ data.tar.gz: 67d86fd9ea6ced7564d851b40faa957d2138a92977a5d3197a2a6f7bdcdd7068
5
+ SHA512:
6
+ metadata.gz: 0ffaa24b43857c82f369e98861b82ddd99dca42bc88b97148d4a055210e0ed76b2f82961932e83e03b76264e091a7e698577b07a69cb2efef6d40b92616965c0
7
+ data.tar.gz: 59e2857a160797e12bfec180e10aadae8f66633e303cb9c41e739a77669ee03f2b13c22faea9f10df263e1d43f2eef172f6c5d03cbca90b7a9944597c8390a77
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg
5
+ vendor
@@ -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
@@ -15,3 +15,36 @@ The format suggested at http://keepachangelog.com/ is used.
15
15
 
16
16
  ### Fixed
17
17
  - Error returns white page
18
+
19
+ ## 1.2.0 - 2014-12-18
20
+
21
+ ### Added
22
+ - A Test Adapter that pushes events to a `window.events` array.
23
+
24
+ ## 1.3.0 - 2017-04-27
25
+
26
+ ### Added
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
data/Rakefile CHANGED
@@ -1,7 +1,5 @@
1
- #!/usr/bin/env rake
2
- require 'bundler/gem_tasks'
3
- require 'rspec/core/rake_task'
1
+ # frozen_string_literal: true
4
2
 
5
- RSpec::Core::RakeTask.new('spec')
3
+ require 'bundler/gem_tasks'
6
4
 
7
5
  task default: :spec
@@ -11,12 +11,16 @@ class GaEvents.Event
11
11
  @html_key: "ga-events"
12
12
  klass: @
13
13
 
14
- # Decompose a dom-string (ruby side) into an event object.
14
+ # Decompose a event-string (ruby side) into an event object.
15
15
  @from_string: (string) ->
16
16
  $.map string.split("$"), (part) =>
17
17
  [category, action, label, value] = part.split "|"
18
18
  new @(category, action, label, value)
19
19
 
20
+ @from_dom: ->
21
+ dom_events = $("div[data-#{@html_key}]").data @html_key
22
+ @from_string dom_events if dom_events?
23
+
20
24
  # Events should not be send to an adapter unless the DOM has finished loading.
21
25
  @flush: ->
22
26
  if @list.length > 0 and @may_flush
@@ -28,11 +32,21 @@ class GaEvents.Event
28
32
  @klass.list.push @
29
33
  @klass.flush()
30
34
 
35
+ escape: (str) ->
36
+ return unless str
37
+ "#{str}".replace(/ä/g, "ae")
38
+ .replace(/ö/g, "oe")
39
+ .replace(/ü/g, "ue")
40
+ .replace(/Ä/g, "Ae")
41
+ .replace(/Ö/g, "Oe")
42
+ .replace(/Ü/g, "Ue")
43
+ .replace(/ß/g, "ss")
44
+
31
45
  to_hash: ->
32
- # Category, action and label must be of type string.
33
- action: "#{@action}"
34
- category: "#{@category}"
35
- label: "#{@label}"
46
+ # Category, action and label must be escaped and of type string.
47
+ action: @escape(@action)
48
+ category: @escape(@category)
49
+ label: @escape(@label)
36
50
  # Value has to be a positive integer or defaults to 1
37
51
  value: @to_positive_integer(@value)
38
52
 
@@ -46,12 +60,17 @@ class GaEvents.Event
46
60
  @may_flush = true
47
61
  @flush()
48
62
 
49
- $(document).ajaxComplete (event, xhr) =>
63
+ process_xhr = (xhr) =>
50
64
  xhr_events = xhr.getResponseHeader @header_key
51
65
  @from_string xhr_events if xhr_events?
52
66
 
53
- dom_events = $("div[data-#{@html_key}]").data @html_key
54
- @from_string dom_events if dom_events?
67
+ $(document).ajaxComplete((_, xhr) -> process_xhr(xhr))
68
+ $(document).on "turbolinks:request-end", (event) ->
69
+ xhr = event.originalEvent.data.xhr
70
+ process_xhr(xhr)
71
+
72
+ @from_dom()
73
+
55
74
 
56
75
  class GaEvents.GoogleTagManagerAdapter
57
76
  constructor: (@event = "ga_event") ->
@@ -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 +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
  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,28 +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
40
+ flash = env['rack.session'] && env['rack.session']['flash'] &&
41
+ env['rack.session']['flash']['flashes']
45
42
 
46
- GaEvents::List.init(flash && flash['ga_events'])
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
- env[ActionDispatch::Flash::KEY] = flash_hash
51
+ flash = env['rack.session'] && env['rack.session']['flash'] &&
52
+ env['rack.session']['flash']['flashes']
53
+
54
+ return unless flash
55
+
56
+ flash['ga_events'] = serialized_data
54
57
  end
55
58
 
56
- def normalize_response(r)
57
- r = r.body if r.respond_to?(:body)
58
- r = r.join if r.respond_to?(:join)
59
- 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
60
63
  end
61
64
 
62
65
  def inject_div(response, serialized_data)
@@ -71,5 +74,13 @@ module GaEvents
71
74
  headers.key?('Content-Type') &&
72
75
  headers['Content-Type'].include?('text/html')
73
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
74
85
  end
75
86
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GaEvents
2
- VERSION = '1.2.0'
4
+ VERSION = '2.0.0'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe GaEvents::Middleware do
@@ -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,33 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ga_events
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
5
- prerelease:
4
+ version: 2.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Florian Dütsch
9
8
  - Sven Winkler
10
- autorequire:
9
+ autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2014-12-18 00:00:00.000000000 Z
12
+ date: 2020-11-20 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rails
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
- - - ! '>='
18
+ - - ">="
21
19
  - !ruby/object:Gem::Version
22
- version: '3.1'
20
+ version: '4.2'
23
21
  type: :runtime
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
- - - ! '>='
25
+ - - ">="
29
26
  - !ruby/object:Gem::Version
30
- version: '3.1'
27
+ version: '4.2'
31
28
  description: Google Analytics' Event Tracking everywhere in your Rails app)
32
29
  email:
33
30
  - florian.duetsch@nix-wie-weg.de
@@ -36,7 +33,8 @@ executables: []
36
33
  extensions: []
37
34
  extra_rdoc_files: []
38
35
  files:
39
- - .gitignore
36
+ - ".gitignore"
37
+ - ".rubocop.yml"
40
38
  - CHANGELOG.md
41
39
  - Gemfile
42
40
  - LICENSE
@@ -55,27 +53,25 @@ files:
55
53
  - spec/spec_helper.rb
56
54
  homepage: https://github.com/Nix-wie-weg/ga_events
57
55
  licenses: []
58
- post_install_message:
56
+ metadata: {}
57
+ post_install_message:
59
58
  rdoc_options: []
60
59
  require_paths:
61
60
  - lib
62
61
  required_ruby_version: !ruby/object:Gem::Requirement
63
- none: false
64
62
  requirements:
65
- - - ! '>='
63
+ - - ">="
66
64
  - !ruby/object:Gem::Version
67
65
  version: '0'
68
66
  required_rubygems_version: !ruby/object:Gem::Requirement
69
- none: false
70
67
  requirements:
71
- - - ! '>='
68
+ - - ">="
72
69
  - !ruby/object:Gem::Version
73
70
  version: '0'
74
71
  requirements: []
75
- rubyforge_project:
76
- rubygems_version: 1.8.23
77
- signing_key:
78
- specification_version: 3
72
+ rubygems_version: 3.0.3
73
+ signing_key:
74
+ specification_version: 4
79
75
  summary: This gem allows you to annotate events everywhere in the code of your Rails
80
76
  app. A rack middleware is automatically inserted into the stack. It transports the
81
77
  event data to the client. Normal requests get a DIV injected, AJAX requests get
@@ -85,4 +81,3 @@ summary: This gem allows you to annotate events everywhere in the code of your R
85
81
  test_files:
86
82
  - spec/middleware_spec.rb
87
83
  - spec/spec_helper.rb
88
- has_rdoc: