event_tracker 0.3.0 → 0.4.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
2
  SHA1:
3
- metadata.gz: 52bc0fa90b12fce477d49af6289838fed0bf5d53
4
- data.tar.gz: a7c4949bf4442d2ac03d6f1806cb8ca4dbf0b2e9
3
+ metadata.gz: 14869ca74719eb114deaf73ce4da6edd9685a7d8
4
+ data.tar.gz: 81d3f06adccf5a96fa3860382169c2cbd92db7d6
5
5
  SHA512:
6
- metadata.gz: b9162174fa778c890e4ae9990c5f6bfce1f48f0d988674c2ebb75db9e6248f12ad8640ee9fbaa695d7efc21d3fc83c78ed77f01074b7c3695a4e933409ecc5bc
7
- data.tar.gz: 55fc69bcaf73d92ebce53c9e10f61bac30845b84ebb3c5bbd8646b7e96286e918904a9701d327dabef96727df0eed4d620b565cac48a17779c6cd162e9d7883a
6
+ metadata.gz: 5882e8505b278c934871e46e55516a36d3fc5b05bc45248b7462c6911f2f61b6c35d3644af7db114014f59563c013f31dc28ec52b7f3886e9fd3d86e0b60e784
7
+ data.tar.gz: 9c496b64dc3aae6c73bdbd8e034f57ac1fdd5ba95a0161f3cd62ac042ef209e43ac02a73bf51ecc574cf9a4ee5c49d10810a4207d19c1d8bcaf8b76d243ed24f
@@ -0,0 +1,6 @@
1
+ # CHANGELOG
2
+
3
+ ## v0.4.0
4
+
5
+ - Prevent XSS when tracking user supplied properties
6
+ - Minimize methods added to ActionController::Base
@@ -16,6 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.version = EventTracker::VERSION
17
17
 
18
18
  gem.add_dependency 'rails', '>= 3.0'
19
- gem.add_development_dependency 'steak'
20
- gem.add_development_dependency 'capybara', '~> 2.0.3'
19
+ gem.add_development_dependency 'capybara', '>= 2.1'
20
+ gem.add_development_dependency 'rspec-rails'
21
21
  end
@@ -1,7 +1,9 @@
1
1
  require "event_tracker/version"
2
- require "event_tracker/mixpanel"
3
- require "event_tracker/kissmetrics"
4
- require "event_tracker/google_analytics"
2
+ require "event_tracker/integration"
3
+ require "event_tracker/integration/base"
4
+ require "event_tracker/integration/mixpanel"
5
+ require "event_tracker/integration/kissmetrics"
6
+ require "event_tracker/integration/google_analytics"
5
7
 
6
8
  module EventTracker
7
9
  module HelperMethods
@@ -35,38 +37,8 @@ module EventTracker
35
37
  end
36
38
 
37
39
  module ActionControllerExtension
38
- def mixpanel_tracker
39
- @mixpanel_tracker ||= begin
40
- mixpanel_key = Rails.application.config.event_tracker.mixpanel_key
41
- EventTracker::Mixpanel.new(mixpanel_key) if mixpanel_key
42
- end
43
- end
44
-
45
- def kissmetrics_tracker
46
- @kissmetrics_tracker ||= begin
47
- kissmetrics_key = Rails.application.config.event_tracker.kissmetrics_key
48
- EventTracker::Kissmetrics.new(kissmetrics_key) if kissmetrics_key
49
- end
50
- end
51
-
52
- def google_analytics_tracker
53
- @google_analytics_tracker ||= begin
54
- google_analytics_key = Rails.application.config.event_tracker.google_analytics_key
55
- EventTracker::GoogleAnalytics.new(google_analytics_key) if google_analytics_key
56
- end
57
- end
58
-
59
- def event_trackers
60
- @event_trackers ||= begin
61
- trackers = []
62
- trackers << mixpanel_tracker if mixpanel_tracker
63
- trackers << kissmetrics_tracker if kissmetrics_tracker
64
- trackers << google_analytics_tracker if google_analytics_tracker
65
- trackers
66
- end
67
- end
68
-
69
40
  def append_event_tracking_tags
41
+ event_trackers = EventTracker::Integration.configured
70
42
  yield
71
43
  return if event_trackers.empty?
72
44
 
@@ -79,40 +51,42 @@ module EventTracker
79
51
  return unless body_insert_at
80
52
 
81
53
  a = []
82
- if mixpanel_alias = session.delete(:mixpanel_alias)
83
- a << mixpanel_tracker.alias(mixpanel_alias)
84
- elsif distinct_id = respond_to?(:mixpanel_distinct_id, true) && mixpanel_distinct_id
85
- a << mixpanel_tracker.identify(distinct_id)
86
- end
87
-
88
- if name_tag = respond_to?(:mixpanel_name_tag, true) && mixpanel_name_tag
89
- a << mixpanel_tracker.name_tag(name_tag)
90
- end
54
+ registered_properties = session.delete(:registered_properties)
55
+ event_tracker_queue = session.delete(:event_tracker_queue)
91
56
 
92
- if (config = session.delete(:mixpanel_set_config)).present?
93
- a << mixpanel_tracker.set_config(config)
94
- end
57
+ event_trackers.each do |tracker|
58
+ if tracker.is_a?(EventTracker::Integration::Mixpanel)
59
+ if mixpanel_alias = session.delete(:mixpanel_alias)
60
+ a << tracker.alias(mixpanel_alias)
61
+ elsif distinct_id = respond_to?(:mixpanel_distinct_id, true) && mixpanel_distinct_id
62
+ a << tracker.identify(distinct_id)
63
+ end
95
64
 
96
- if (people = session.delete(:mixpanel_people_set)).present?
97
- a << mixpanel_tracker.people_set(people)
98
- end
65
+ if name_tag = respond_to?(:mixpanel_name_tag, true) && mixpanel_name_tag
66
+ a << tracker.name_tag(name_tag)
67
+ end
99
68
 
100
- if (people = session.delete(:mixpanel_people_set_once)).present?
101
- a << mixpanel_tracker.people_set_once(people)
102
- end
69
+ if (config = session.delete(:mixpanel_set_config)).present?
70
+ a << tracker.set_config(config)
71
+ end
103
72
 
104
- if (people = session.delete(:mixpanel_people_increment)).present?
105
- a << mixpanel_tracker.people_increment(people)
106
- end
73
+ if (people = session.delete(:mixpanel_people_set)).present?
74
+ a << tracker.people_set(people)
75
+ end
107
76
 
108
- if identity = respond_to?(:kissmetrics_identity, true) && kissmetrics_identity
109
- a << kissmetrics_tracker.identify(identity)
110
- end
77
+ if (people = session.delete(:mixpanel_people_set_once)).present?
78
+ a << tracker.people_set_once(people)
79
+ end
111
80
 
112
- registered_properties = session.delete(:registered_properties)
113
- event_tracker_queue = session.delete(:event_tracker_queue)
81
+ if (people = session.delete(:mixpanel_people_increment)).present?
82
+ a << tracker.people_increment(people)
83
+ end
84
+ elsif tracker.is_a?(EventTracker::Integration::Kissmetrics)
85
+ if identity = respond_to?(:kissmetrics_identity, true) && kissmetrics_identity
86
+ a << tracker.identify(identity)
87
+ end
88
+ end
114
89
 
115
- event_trackers.each do |tracker|
116
90
  a << tracker.register(registered_properties) if registered_properties.present? && tracker.respond_to?(:register)
117
91
 
118
92
  if event_tracker_queue.present?
@@ -0,0 +1,16 @@
1
+ module EventTracker::Integration
2
+ def self.configured
3
+ @configured ||= begin
4
+ trackers = []
5
+ integrations = [Mixpanel, Kissmetrics, GoogleAnalytics ]
6
+ integrations.each do |integration|
7
+ key_string = "#{integration.to_s.demodulize.underscore}_key"
8
+ key = Rails.application.config.event_tracker[key_string]
9
+ if key
10
+ trackers << integration.new(key)
11
+ end
12
+ end
13
+ trackers
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ class EventTracker::Integration::Base
2
+ include ERB::Util
3
+
4
+ def initialize(key)
5
+ @key = key
6
+ end
7
+
8
+ private
9
+
10
+ def embeddable_json(properties)
11
+ json_escape(properties.to_json).html_safe
12
+ end
13
+ end
@@ -1,8 +1,4 @@
1
- class EventTracker::GoogleAnalytics
2
- def initialize(key)
3
- @key = key
4
- end
5
-
1
+ class EventTracker::Integration::GoogleAnalytics < EventTracker::Integration::Base
6
2
  def init
7
3
  <<-EOD
8
4
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
@@ -1,8 +1,4 @@
1
- class EventTracker::Kissmetrics
2
- def initialize(key)
3
- @key = key
4
- end
5
-
1
+ class EventTracker::Integration::Kissmetrics < EventTracker::Integration::Base
6
2
  def init
7
3
  <<-EOD
8
4
  var _kmq = _kmq || [];
@@ -21,11 +17,11 @@ class EventTracker::Kissmetrics
21
17
  end
22
18
 
23
19
  def register(registered_properties)
24
- %Q{_kmq.push(['set', #{registered_properties.to_json}]);}
20
+ %Q{_kmq.push(['set', #{embeddable_json(registered_properties)}]);}
25
21
  end
26
22
 
27
23
  def track(event_name, properties)
28
- p = properties.empty? ? "" : ", #{properties.to_json}"
24
+ p = properties.empty? ? "" : ", #{embeddable_json(properties)}"
29
25
  %Q{_kmq.push(['record', '#{event_name}'#{p}]);}
30
26
  end
31
27
 
@@ -1,8 +1,4 @@
1
- class EventTracker::Mixpanel
2
- def initialize(key)
3
- @key = key
4
- end
5
-
1
+ class EventTracker::Integration::Mixpanel < EventTracker::Integration::Base
6
2
  def init
7
3
  s = <<-EOD
8
4
  (function(e,b){if(!b.__SV){var a,f,i,g;window.mixpanel=b;a=e.createElement("script");
@@ -20,11 +16,11 @@ class EventTracker::Mixpanel
20
16
  end
21
17
 
22
18
  def register(registered_properties)
23
- %Q{mixpanel.register(#{registered_properties.to_json});}
19
+ %Q{mixpanel.register(#{embeddable_json(registered_properties)});}
24
20
  end
25
21
 
26
22
  def track(event_name, properties)
27
- p = properties.empty? ? "" : ", #{properties.to_json}"
23
+ p = properties.empty? ? "" : ", #{embeddable_json(properties)}"
28
24
  %Q{mixpanel.track("#{event_name}"#{p});}
29
25
  end
30
26
 
@@ -37,22 +33,22 @@ class EventTracker::Mixpanel
37
33
  end
38
34
 
39
35
  def people_set(properties)
40
- %Q{mixpanel.people.set(#{properties.to_json});}
36
+ %Q{mixpanel.people.set(#{embeddable_json(properties)});}
41
37
  end
42
38
 
43
39
  def people_set_once(properties)
44
- %Q{mixpanel.people.set_once(#{properties.to_json});}
40
+ %Q{mixpanel.people.set_once(#{embeddable_json(properties)});}
45
41
  end
46
42
 
47
43
  def people_increment(properties)
48
- %Q{mixpanel.people.increment(#{properties.to_json});}
44
+ %Q{mixpanel.people.increment(#{embeddable_json(properties)});}
49
45
  end
50
46
 
51
47
  def set_config(properties)
52
- %Q{mixpanel.set_config(#{properties.to_json});}
48
+ %Q{mixpanel.set_config(#{embeddable_json(properties)});}
53
49
  end
54
50
 
55
51
  def alias(identity)
56
- %Q{mixpanel.alias(#{identity.to_json});}
52
+ %Q{mixpanel.alias(#{embeddable_json(identity)});}
57
53
  end
58
54
  end
@@ -1,3 +1,3 @@
1
1
  module EventTracker
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  shared_examples_for "init" do
4
- subject { page.find("head script").native.content }
4
+ subject { page.find("head script", visible: false).native.content }
5
5
  it { should include('mixpanel.init("YOUR_TOKEN")') }
6
6
  it { should include(%q{var _kmk = _kmk || 'KISSMETRICS_KEY'}) }
7
7
  it { should include(%q{ga('create', 'GOOGLE_ANALYTICS_KEY', 'auto', {'name': 'event_tracker'});}) }
@@ -29,7 +29,7 @@ shared_examples_for "with event" do
29
29
  end
30
30
 
31
31
  feature 'basic integration' do
32
- subject { page.find("body script").native.content }
32
+ subject { page.find("body script", visible: false).native.content }
33
33
 
34
34
  class BasicController < ApplicationController
35
35
  around_filter :append_event_tracking_tags
@@ -94,16 +94,16 @@ feature 'basic integration' do
94
94
  def index
95
95
  register_properties age: 19
96
96
  register_properties gender: "female"
97
- track_event "Take an action", property1: "a", property2: 1
97
+ track_event "Take an action", property1: "a", property2: 1, xss: "</script>"
98
98
  render inline: "OK", layout: true
99
99
  end
100
100
  end
101
101
 
102
102
  context "track event with properties" do
103
103
  background { visit "/with_properties" }
104
- it { should include %Q{mixpanel.track("Take an action", {"property1":"a","property2":1})} }
104
+ it { should include %q{mixpanel.track("Take an action", {"property1":"a","property2":1,"xss":"\u003c/script\u003e"})} }
105
105
  it { should include %Q{mixpanel.register({"age":19,"gender":"female"})} }
106
- it { should include %Q{_kmq.push(['record', 'Take an action', {"property1":"a","property2":1}])} }
106
+ it { should include %q{_kmq.push(['record', 'Take an action', {"property1":"a","property2":1,"xss":"\u003c/script\u003e"}])} }
107
107
  it { should include %Q{_kmq.push(['set', {"age":19,"gender":"female"}])} }
108
108
  end
109
109
 
@@ -26,5 +26,4 @@ end
26
26
  class ApplicationController < ActionController::Base; end
27
27
 
28
28
  require 'rspec/rails'
29
- require "steak"
30
29
 
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_tracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul McMahon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-08 00:00:00.000000000 Z
11
+ date: 2016-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: steak
28
+ name: capybara
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '2.1'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '2.1'
41
41
  - !ruby/object:Gem::Dependency
42
- name: capybara
42
+ name: rspec-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 2.0.3
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 2.0.3
54
+ version: '0'
55
55
  description: Easy integration with Mixpanel, Kissmetrics, and Google Analytics for
56
56
  Rails
57
57
  email:
@@ -60,16 +60,19 @@ executables: []
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
- - .gitignore
63
+ - ".gitignore"
64
+ - CHANGELOG.md
64
65
  - Gemfile
65
66
  - LICENSE
66
67
  - README.md
67
68
  - Rakefile
68
69
  - event_tracker.gemspec
69
70
  - lib/event_tracker.rb
70
- - lib/event_tracker/google_analytics.rb
71
- - lib/event_tracker/kissmetrics.rb
72
- - lib/event_tracker/mixpanel.rb
71
+ - lib/event_tracker/integration.rb
72
+ - lib/event_tracker/integration/base.rb
73
+ - lib/event_tracker/integration/google_analytics.rb
74
+ - lib/event_tracker/integration/kissmetrics.rb
75
+ - lib/event_tracker/integration/mixpanel.rb
73
76
  - lib/event_tracker/version.rb
74
77
  - spec/app/views/basic/in_views.html.erb
75
78
  - spec/app/views/layouts/application.html.erb
@@ -84,17 +87,17 @@ require_paths:
84
87
  - lib
85
88
  required_ruby_version: !ruby/object:Gem::Requirement
86
89
  requirements:
87
- - - '>='
90
+ - - ">="
88
91
  - !ruby/object:Gem::Version
89
92
  version: '0'
90
93
  required_rubygems_version: !ruby/object:Gem::Requirement
91
94
  requirements:
92
- - - '>='
95
+ - - ">="
93
96
  - !ruby/object:Gem::Version
94
97
  version: '0'
95
98
  requirements: []
96
99
  rubyforge_project:
97
- rubygems_version: 2.1.11
100
+ rubygems_version: 2.2.3
98
101
  signing_key:
99
102
  specification_version: 4
100
103
  summary: Track using javascript from your controllers, even when redirecting
@@ -103,3 +106,4 @@ test_files:
103
106
  - spec/app/views/layouts/application.html.erb
104
107
  - spec/event_tracker_spec.rb
105
108
  - spec/spec_helper.rb
109
+ has_rdoc: