event_tracker 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/event_tracker.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "event_tracker/version"
2
2
  require "event_tracker/mixpanel"
3
+ require "event_tracker/kissmetrics"
3
4
 
4
5
  module EventTracker
5
6
  module ActionControllerExtension
@@ -13,22 +14,43 @@ module EventTracker
13
14
 
14
15
  def append_event_tracking_tags
15
16
  mixpanel_key = Rails.application.config.event_tracker.mixpanel_key
16
- return unless mixpanel_key
17
+ kissmetrics_key = Rails.application.config.event_tracker.kissmetrics_key
18
+ return unless mixpanel_key || kissmetrics_key
17
19
 
18
20
  body = response.body
19
21
  insert_at = body.index('</head')
20
22
  if insert_at
23
+ trackers = []
24
+ a = [ %q{<script type="text/javascript">} ]
25
+
26
+ if mixpanel_key
27
+ trackers << EventTracker::Mixpanel
28
+ distinct_id = respond_to?(:mixpanel_distinct_id) && mixpanel_distinct_id
29
+ name_tag = respond_to?(:mixpanel_name_tag) && mixpanel_name_tag
30
+ a << EventTracker::Mixpanel.init(mixpanel_key)
31
+ a << EventTracker::Mixpanel.identify(distinct_id) if distinct_id
32
+ a << EventTracker::Mixpanel.name_tag(name_tag) if name_tag
33
+ end
34
+
35
+ if kissmetrics_key
36
+ trackers << EventTracker::Kissmetrics
37
+ identity = respond_to?(:kissmetrics_identity) && kissmetrics_identity
38
+ a << EventTracker::Kissmetrics.init(kissmetrics_key)
39
+ a << EventTracker::Kissmetrics.identify(identity) if identity
40
+ end
41
+
21
42
  registered_properties = session.delete(:registered_properties)
22
43
  event_tracker_queue = session.delete(:event_tracker_queue)
23
- distinct_id = respond_to?(:mixpanel_distinct_id) && mixpanel_distinct_id
24
- name_tag = respond_to?(:mixpanel_name_tag) && mixpanel_name_tag
25
44
 
26
- a = [ %q{<script type="text/javascript">} ]
27
- a << EventTracker::Mixpanel.init(mixpanel_key)
28
- a << EventTracker::Mixpanel.identify(distinct_id) if distinct_id
29
- a << EventTracker::Mixpanel.name_tag(name_tag) if name_tag
30
- a << EventTracker::Mixpanel.register(registered_properties) if registered_properties.present?
31
- a << EventTracker::Mixpanel.track(event_tracker_queue) if event_tracker_queue.present?
45
+ trackers.each do |tracker|
46
+ a << tracker.register(registered_properties) if registered_properties.present?
47
+
48
+ if event_tracker_queue.present?
49
+ event_tracker_queue.each do |event_name, properties|
50
+ a << tracker.track(event_name, properties)
51
+ end
52
+ end
53
+ end
32
54
  a << %q{</script>}
33
55
  body.insert insert_at, a.join("\n")
34
56
  response.body = body
@@ -0,0 +1,31 @@
1
+ module EventTracker::Kissmetrics
2
+ def self.init(key)
3
+ <<-EOD
4
+ var _kmq = _kmq || [];
5
+ var _kmk = _kmk || '#{key}';
6
+ function _kms(u){
7
+ setTimeout(function(){
8
+ var d = document, f = d.getElementsByTagName('script')[0],
9
+ s = d.createElement('script');
10
+ s.type = 'text/javascript'; s.async = true; s.src = u;
11
+ f.parentNode.insertBefore(s, f);
12
+ }, 1);
13
+ }
14
+ _kms('//i.kissmetrics.com/i.js');
15
+ _kms('//doug1izaerwt3.cloudfront.net/' + _kmk + '.1.js');
16
+ EOD
17
+ end
18
+
19
+ def self.register(registered_properties)
20
+ %Q{_kmq.push(['set', #{registered_properties.to_json}]);}
21
+ end
22
+
23
+ def self.track(event_name, properties)
24
+ p = properties.empty? ? "" : ", #{properties.to_json}"
25
+ %Q{_kmq.push(['record', '#{event_name}'#{p}]);}
26
+ end
27
+
28
+ def self.identify(identity)
29
+ %Q{_kmq.push(['identify', '#{identity}']);}
30
+ end
31
+ end
@@ -16,14 +16,12 @@ module EventTracker::Mixpanel
16
16
  end
17
17
 
18
18
  def self.register(registered_properties)
19
- %Q{mixpanel.register(#{registered_properties.to_json})}
19
+ %Q{mixpanel.register(#{registered_properties.to_json});}
20
20
  end
21
21
 
22
- def self.track(event_tracker_queue)
23
- event_tracker_queue.map do |event_name, properties|
24
- p = properties.empty? ? "" : ", #{properties.to_json}"
25
- %Q{mixpanel.track("#{event_name}"#{p});}
26
- end.join("\n")
22
+ def self.track(event_name, properties)
23
+ p = properties.empty? ? "" : ", #{properties.to_json}"
24
+ %Q{mixpanel.track("#{event_name}"#{p});}
27
25
  end
28
26
 
29
27
  def self.name_tag(name_tag)
@@ -1,3 +1,3 @@
1
1
  module EventTracker
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,14 +1,17 @@
1
1
  require "spec_helper"
2
2
 
3
- shared_examples_for "mixpanel init" do
3
+ shared_examples_for "init" do
4
4
  it { should include('mixpanel.init("YOUR_TOKEN")') }
5
+ it { should include(%q{var _kmk = _kmk || 'KISSMETRICS_KEY'}) }
5
6
  end
6
7
 
7
8
  shared_examples_for "without distinct id" do
9
+ it { should_not include(%q{_kmq.push(['identify', 'name@email.com']);}) }
8
10
  it { should_not include('mixpanel.identify("distinct_id")') }
9
11
  end
10
12
 
11
13
  shared_examples_for "with distinct id" do
14
+ it { should include(%q{_kmq.push(['identify', 'name@email.com']);}) }
12
15
  it { should include('mixpanel.identify("distinct_id")') }
13
16
  end
14
17
 
@@ -18,6 +21,7 @@ end
18
21
 
19
22
  shared_examples_for "with event" do
20
23
  it { should include('mixpanel.track("Register for site")') }
24
+ it { should include(%q{_kmq.push(['record', 'Register for site']);}) }
21
25
  end
22
26
 
23
27
  feature 'basic integration' do
@@ -37,14 +41,14 @@ feature 'basic integration' do
37
41
 
38
42
  context 'visit page without tracking' do
39
43
  background { visit '/basic/no_tracking' }
40
- it_should_behave_like "mixpanel init"
44
+ it_should_behave_like "init"
41
45
  it_should_behave_like "without distinct id"
42
46
  it_should_behave_like "without event"
43
47
  end
44
48
 
45
49
  context 'visit page with tracking' do
46
50
  background { visit '/basic/with_tracking' }
47
- it_should_behave_like "mixpanel init"
51
+ it_should_behave_like "init"
48
52
  it_should_behave_like "without distinct id"
49
53
  it_should_behave_like "with event"
50
54
  end
@@ -90,6 +94,8 @@ feature 'basic integration' do
90
94
  background { visit "/with_properties" }
91
95
  it { should include %Q{mixpanel.track("Take an action", {"property1":"a","property2":1})} }
92
96
  it { should include %Q{mixpanel.register({"age":19,"gender":"female"})} }
97
+ it { should include %Q{_kmq.push(['record', 'Take an action', {"property1":"a","property2":1}])} }
98
+ it { should include %Q{_kmq.push(['set', {"age":19,"gender":"female"}])} }
93
99
  end
94
100
 
95
101
  class IdentityController < ApplicationController
@@ -98,6 +104,10 @@ feature 'basic integration' do
98
104
  "distinct_id"
99
105
  end
100
106
 
107
+ def kissmetrics_identity
108
+ "name@email.com"
109
+ end
110
+
101
111
  def index
102
112
  render inline: "OK", layout: true
103
113
  end
data/spec/spec_helper.rb CHANGED
@@ -14,6 +14,7 @@ app.config.active_support.deprecation = :log
14
14
  app.config.action_dispatch.show_exceptions = false
15
15
 
16
16
  app.config.event_tracker.mixpanel_key = "YOUR_TOKEN"
17
+ app.config.event_tracker.kissmetrics_key = "KISSMETRICS_KEY"
17
18
 
18
19
  app.initialize!
19
20
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_tracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -57,6 +57,7 @@ files:
57
57
  - Rakefile
58
58
  - event_tracker.gemspec
59
59
  - lib/event_tracker.rb
60
+ - lib/event_tracker/kissmetrics.rb
60
61
  - lib/event_tracker/mixpanel.rb
61
62
  - lib/event_tracker/version.rb
62
63
  - spec/app/views/layouts/application.html.erb
@@ -76,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
77
  version: '0'
77
78
  segments:
78
79
  - 0
79
- hash: 4227757316548368302
80
+ hash: -2338464354941489983
80
81
  required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  none: false
82
83
  requirements:
@@ -85,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
86
  version: '0'
86
87
  segments:
87
88
  - 0
88
- hash: 4227757316548368302
89
+ hash: -2338464354941489983
89
90
  requirements: []
90
91
  rubyforge_project:
91
92
  rubygems_version: 1.8.24