event_tracker 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/event_tracker.rb +47 -34
- data/lib/event_tracker/kissmetrics.rb +10 -6
- data/lib/event_tracker/mixpanel.rb +11 -7
- data/lib/event_tracker/version.rb +1 -1
- data/spec/event_tracker_spec.rb +2 -1
- metadata +4 -4
data/lib/event_tracker.rb
CHANGED
@@ -14,49 +14,62 @@ module EventTracker
|
|
14
14
|
end
|
15
15
|
|
16
16
|
module ActionControllerExtension
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
def mixpanel_tracker
|
18
|
+
@mixpanel_tracker ||= begin
|
19
|
+
mixpanel_key = Rails.application.config.event_tracker.mixpanel_key
|
20
|
+
EventTracker::Mixpanel.new(mixpanel_key) if mixpanel_key
|
21
|
+
end
|
22
|
+
end
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
def kissmetrics_tracker
|
25
|
+
@kissmetrics_tracker ||= begin
|
26
|
+
kissmetrics_key = Rails.application.config.event_tracker.kissmetrics_key
|
27
|
+
EventTracker::Kissmetrics.new(kissmetrics_key) if kissmetrics_key
|
28
|
+
end
|
29
|
+
end
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
def event_trackers
|
32
|
+
@event_trackers ||= begin
|
33
|
+
trackers = []
|
34
|
+
trackers << mixpanel_tracker if mixpanel_tracker
|
35
|
+
trackers << kissmetrics_tracker if kissmetrics_tracker
|
36
|
+
trackers
|
37
|
+
end
|
38
|
+
end
|
36
39
|
|
37
|
-
|
38
|
-
|
39
|
-
identity = respond_to?(:kissmetrics_identity) && kissmetrics_identity
|
40
|
-
a << EventTracker::Kissmetrics.init(kissmetrics_key)
|
41
|
-
a << EventTracker::Kissmetrics.identify(identity) if identity
|
42
|
-
end
|
40
|
+
def append_event_tracking_tags
|
41
|
+
return if event_trackers.empty?
|
43
42
|
|
44
|
-
|
45
|
-
|
43
|
+
body = response.body
|
44
|
+
head_insert_at = body.index('</head')
|
45
|
+
return unless head_insert_at
|
46
|
+
body.insert head_insert_at, view_context.javascript_tag(event_trackers.map {|t| t.init }.join("\n"))
|
47
|
+
body_insert_at = body.index('</body')
|
48
|
+
return unless body_insert_at
|
49
|
+
a = []
|
50
|
+
if distinct_id = respond_to?(:mixpanel_distinct_id) && mixpanel_distinct_id
|
51
|
+
a << mixpanel_tracker.identify(distinct_id)
|
52
|
+
end
|
53
|
+
if name_tag = respond_to?(:mixpanel_name_tag) && mixpanel_name_tag
|
54
|
+
a << mixpanel_tracker.name_tag(name_tag)
|
55
|
+
end
|
56
|
+
if identity = respond_to?(:kissmetrics_identity) && kissmetrics_identity
|
57
|
+
a << kissmetrics_tracker.identify(identity)
|
58
|
+
end
|
59
|
+
registered_properties = session.delete(:registered_properties)
|
60
|
+
event_tracker_queue = session.delete(:event_tracker_queue)
|
46
61
|
|
47
|
-
|
48
|
-
|
62
|
+
event_trackers.each do |tracker|
|
63
|
+
a << tracker.register(registered_properties) if registered_properties.present?
|
49
64
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
65
|
+
if event_tracker_queue.present?
|
66
|
+
event_tracker_queue.each do |event_name, properties|
|
67
|
+
a << tracker.track(event_name, properties)
|
54
68
|
end
|
55
69
|
end
|
56
|
-
a << %q{</script>}
|
57
|
-
body.insert insert_at, a.join("\n")
|
58
|
-
response.body = body
|
59
70
|
end
|
71
|
+
body.insert body_insert_at, view_context.javascript_tag(a.join("\n"))
|
72
|
+
response.body = body
|
60
73
|
end
|
61
74
|
|
62
75
|
end
|
@@ -1,8 +1,12 @@
|
|
1
|
-
|
2
|
-
def
|
1
|
+
class EventTracker::Kissmetrics
|
2
|
+
def initialize(key)
|
3
|
+
@key = key
|
4
|
+
end
|
5
|
+
|
6
|
+
def init
|
3
7
|
<<-EOD
|
4
8
|
var _kmq = _kmq || [];
|
5
|
-
var _kmk = _kmk || '#{key}';
|
9
|
+
var _kmk = _kmk || '#{@key}';
|
6
10
|
function _kms(u){
|
7
11
|
setTimeout(function(){
|
8
12
|
var d = document, f = d.getElementsByTagName('script')[0],
|
@@ -16,16 +20,16 @@ module EventTracker::Kissmetrics
|
|
16
20
|
EOD
|
17
21
|
end
|
18
22
|
|
19
|
-
def
|
23
|
+
def register(registered_properties)
|
20
24
|
%Q{_kmq.push(['set', #{registered_properties.to_json}]);}
|
21
25
|
end
|
22
26
|
|
23
|
-
def
|
27
|
+
def track(event_name, properties)
|
24
28
|
p = properties.empty? ? "" : ", #{properties.to_json}"
|
25
29
|
%Q{_kmq.push(['record', '#{event_name}'#{p}]);}
|
26
30
|
end
|
27
31
|
|
28
|
-
def
|
32
|
+
def identify(identity)
|
29
33
|
%Q{_kmq.push(['identify', '#{identity}']);}
|
30
34
|
end
|
31
35
|
end
|
@@ -1,5 +1,9 @@
|
|
1
|
-
|
2
|
-
def
|
1
|
+
class EventTracker::Mixpanel
|
2
|
+
def initialize(key)
|
3
|
+
@key = key
|
4
|
+
end
|
5
|
+
|
6
|
+
def init
|
3
7
|
s = <<-EOD
|
4
8
|
(function(c,a){window.mixpanel=a;var b,d,h,e;b=c.createElement("script");
|
5
9
|
b.type="text/javascript";b.async=!0;b.src=("https:"===c.location.protocol?"https:":"http:")+
|
@@ -11,24 +15,24 @@ module EventTracker::Mixpanel
|
|
11
15
|
'track_forms','register','register_once','unregister','identify','name_tag',
|
12
16
|
'set_config','people.identify','people.set','people.increment'];for(e=0;e<h.length;e++)d(g,h[e]);
|
13
17
|
a._i.push([b,c,f])};a.__SV=1.1;})(document,window.mixpanel||[]);
|
14
|
-
mixpanel.init("#{
|
18
|
+
mixpanel.init("#{@key}");
|
15
19
|
EOD
|
16
20
|
end
|
17
21
|
|
18
|
-
def
|
22
|
+
def register(registered_properties)
|
19
23
|
%Q{mixpanel.register(#{registered_properties.to_json});}
|
20
24
|
end
|
21
25
|
|
22
|
-
def
|
26
|
+
def track(event_name, properties)
|
23
27
|
p = properties.empty? ? "" : ", #{properties.to_json}"
|
24
28
|
%Q{mixpanel.track("#{event_name}"#{p});}
|
25
29
|
end
|
26
30
|
|
27
|
-
def
|
31
|
+
def name_tag(name_tag)
|
28
32
|
%Q{mixpanel.name_tag("#{name_tag}");}
|
29
33
|
end
|
30
34
|
|
31
|
-
def
|
35
|
+
def identify(distinct_id)
|
32
36
|
%Q{mixpanel.identify("#{distinct_id}");}
|
33
37
|
end
|
34
38
|
end
|
data/spec/event_tracker_spec.rb
CHANGED
@@ -1,6 +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
5
|
it { should include('mixpanel.init("YOUR_TOKEN")') }
|
5
6
|
it { should include(%q{var _kmk = _kmk || 'KISSMETRICS_KEY'}) }
|
6
7
|
end
|
@@ -25,7 +26,7 @@ shared_examples_for "with event" do
|
|
25
26
|
end
|
26
27
|
|
27
28
|
feature 'basic integration' do
|
28
|
-
subject { page.find("script").native.content }
|
29
|
+
subject { page.find("body script").native.content }
|
29
30
|
|
30
31
|
class BasicController < ApplicationController
|
31
32
|
after_filter :append_event_tracking_tags
|
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
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -78,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
78
78
|
version: '0'
|
79
79
|
segments:
|
80
80
|
- 0
|
81
|
-
hash:
|
81
|
+
hash: 2100786682895310824
|
82
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
87
|
version: '0'
|
88
88
|
segments:
|
89
89
|
- 0
|
90
|
-
hash:
|
90
|
+
hash: 2100786682895310824
|
91
91
|
requirements: []
|
92
92
|
rubyforge_project:
|
93
93
|
rubygems_version: 1.8.24
|