event_tracker 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +30 -0
- data/Rakefile +5 -0
- data/event_tracker.gemspec +20 -0
- data/lib/event_tracker.rb +48 -0
- data/lib/event_tracker/mixpanel.rb +36 -0
- data/lib/event_tracker/version.rb +3 -0
- data/spec/app/views/layouts/application.html.erb +9 -0
- data/spec/event_tracker_spec.rb +126 -0
- data/spec/spec_helper.rb +28 -0
- metadata +98 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Paul McMahon
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# EventTracker
|
2
|
+
|
3
|
+
Easy tracking using mixpanel or kissmetrics
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'event_tracker'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install event_tracker
|
18
|
+
|
19
|
+
## Todos
|
20
|
+
|
21
|
+
* AJAX handling
|
22
|
+
* External redirects
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/event_tracker/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Paul McMahon"]
|
6
|
+
gem.email = ["paul@mobalean.com"]
|
7
|
+
gem.description = %q{Easy integration with Mixpanel and Kissmetrics for Rails}
|
8
|
+
gem.summary = %q{Track using javascript from your controllers, even when redirecting}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "event_tracker"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = EventTracker::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'rails', '~> 3.0'
|
19
|
+
gem.add_development_dependency 'steak'
|
20
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "event_tracker/version"
|
2
|
+
require "event_tracker/mixpanel"
|
3
|
+
|
4
|
+
module EventTracker
|
5
|
+
module ActionControllerExtension
|
6
|
+
def track_event(event_name, args = {})
|
7
|
+
(session[:event_tracker_queue] ||= []) << [event_name, args]
|
8
|
+
end
|
9
|
+
|
10
|
+
def register_properties(args)
|
11
|
+
(session[:registered_properties] ||= {}).merge!(args)
|
12
|
+
end
|
13
|
+
|
14
|
+
def append_event_tracking_tags
|
15
|
+
mixpanel_key = Rails.application.config.event_tracker.mixpanel_key
|
16
|
+
return unless mixpanel_key
|
17
|
+
|
18
|
+
body = response.body
|
19
|
+
insert_at = body.index('</head')
|
20
|
+
if insert_at
|
21
|
+
registered_properties = session.delete(:registered_properties)
|
22
|
+
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
|
+
|
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?
|
32
|
+
a << %q{</script>}
|
33
|
+
body.insert insert_at, a.join("\n")
|
34
|
+
response.body = body
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
class Railtie < Rails::Railtie
|
41
|
+
config.event_tracker = ActiveSupport::OrderedOptions.new
|
42
|
+
initializer "event_tracker" do |app|
|
43
|
+
ActiveSupport.on_load :action_controller do
|
44
|
+
include ActionControllerExtension
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module EventTracker::Mixpanel
|
2
|
+
def self.init(mixpanel_key)
|
3
|
+
s = <<-EOD
|
4
|
+
(function(c,a){window.mixpanel=a;var b,d,h,e;b=c.createElement("script");
|
5
|
+
b.type="text/javascript";b.async=!0;b.src=("https:"===c.location.protocol?"https:":"http:")+
|
6
|
+
'//cdn.mxpnl.com/libs/mixpanel-2.1.min.js';d=c.getElementsByTagName("script")[0];
|
7
|
+
d.parentNode.insertBefore(b,d);a._i=[];a.init=function(b,c,f){function d(a,b){
|
8
|
+
var c=b.split(".");2==c.length&&(a=a[c[0]],b=c[1]);a[b]=function(){a.push([b].concat(
|
9
|
+
Array.prototype.slice.call(arguments,0)))}}var g=a;"undefined"!==typeof f?g=a[f]=[]:
|
10
|
+
f="mixpanel";g.people=g.people||[];h=['disable','track','track_pageview','track_links',
|
11
|
+
'track_forms','register','register_once','unregister','identify','name_tag',
|
12
|
+
'set_config','people.identify','people.set','people.increment'];for(e=0;e<h.length;e++)d(g,h[e]);
|
13
|
+
a._i.push([b,c,f])};a.__SV=1.1;})(document,window.mixpanel||[]);
|
14
|
+
mixpanel.init("#{mixpanel_key}");
|
15
|
+
EOD
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.register(registered_properties)
|
19
|
+
%Q{mixpanel.register(#{registered_properties.to_json})}
|
20
|
+
end
|
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")
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.name_tag(name_tag)
|
30
|
+
%Q{mixpanel.name_tag("#{name_tag}");}
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.identify(distinct_id)
|
34
|
+
%Q{mixpanel.identify("#{distinct_id}");}
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
shared_examples_for "mixpanel init" do
|
4
|
+
it { should include('mixpanel.init("YOUR_TOKEN")') }
|
5
|
+
end
|
6
|
+
|
7
|
+
shared_examples_for "without distinct id" do
|
8
|
+
it { should_not include('mixpanel.identify("distinct_id")') }
|
9
|
+
end
|
10
|
+
|
11
|
+
shared_examples_for "with distinct id" do
|
12
|
+
it { should include('mixpanel.identify("distinct_id")') }
|
13
|
+
end
|
14
|
+
|
15
|
+
shared_examples_for "without event" do
|
16
|
+
it { should_not include('mixpanel.track("Register for site")') }
|
17
|
+
end
|
18
|
+
|
19
|
+
shared_examples_for "with event" do
|
20
|
+
it { should include('mixpanel.track("Register for site")') }
|
21
|
+
end
|
22
|
+
|
23
|
+
feature 'basic integration' do
|
24
|
+
subject { page.find("script").native.content }
|
25
|
+
|
26
|
+
class BasicController < ApplicationController
|
27
|
+
after_filter :append_event_tracking_tags
|
28
|
+
def no_tracking
|
29
|
+
render inline: "OK", layout: true
|
30
|
+
end
|
31
|
+
|
32
|
+
def with_tracking
|
33
|
+
track_event "Register for site"
|
34
|
+
render inline: "OK", layout: true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'visit page without tracking' do
|
39
|
+
background { visit '/basic/no_tracking' }
|
40
|
+
it_should_behave_like "mixpanel init"
|
41
|
+
it_should_behave_like "without distinct id"
|
42
|
+
it_should_behave_like "without event"
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'visit page with tracking' do
|
46
|
+
background { visit '/basic/with_tracking' }
|
47
|
+
it_should_behave_like "mixpanel init"
|
48
|
+
it_should_behave_like "without distinct id"
|
49
|
+
it_should_behave_like "with event"
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'visit page with tracking then without tracking' do
|
53
|
+
background do
|
54
|
+
visit '/basic/with_tracking'
|
55
|
+
visit '/basic/no_tracking'
|
56
|
+
end
|
57
|
+
it_should_behave_like "without event"
|
58
|
+
end
|
59
|
+
|
60
|
+
class RedirectsController < ApplicationController
|
61
|
+
after_filter :append_event_tracking_tags
|
62
|
+
|
63
|
+
def index
|
64
|
+
track_event "Register for site"
|
65
|
+
redirect_to action: :redirected
|
66
|
+
end
|
67
|
+
|
68
|
+
def redirected
|
69
|
+
render inline: "OK", layout: true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'track event then redirect' do
|
74
|
+
background { visit '/redirects' }
|
75
|
+
it_should_behave_like "with event"
|
76
|
+
end
|
77
|
+
|
78
|
+
class WithPropertiesController < ApplicationController
|
79
|
+
after_filter :append_event_tracking_tags
|
80
|
+
|
81
|
+
def index
|
82
|
+
register_properties age: 19
|
83
|
+
register_properties gender: "female"
|
84
|
+
track_event "Take an action", property1: "a", property2: 1
|
85
|
+
render inline: "OK", layout: true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "track event with properties" do
|
90
|
+
background { visit "/with_properties" }
|
91
|
+
it { should include %Q{mixpanel.track("Take an action", {"property1":"a","property2":1})} }
|
92
|
+
it { should include %Q{mixpanel.register({"age":19,"gender":"female"})} }
|
93
|
+
end
|
94
|
+
|
95
|
+
class IdentityController < ApplicationController
|
96
|
+
after_filter :append_event_tracking_tags
|
97
|
+
def mixpanel_distinct_id
|
98
|
+
"distinct_id"
|
99
|
+
end
|
100
|
+
|
101
|
+
def index
|
102
|
+
render inline: "OK", layout: true
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "with identity" do
|
107
|
+
background { visit "/identity" }
|
108
|
+
it_should_behave_like "with distinct id"
|
109
|
+
end
|
110
|
+
|
111
|
+
class NameTagController < ApplicationController
|
112
|
+
after_filter :append_event_tracking_tags
|
113
|
+
def mixpanel_name_tag
|
114
|
+
"foo@example.org"
|
115
|
+
end
|
116
|
+
|
117
|
+
def index
|
118
|
+
render inline: "OK", layout: true
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "with name tag" do
|
123
|
+
background { visit "/name_tag" }
|
124
|
+
it { should include(%q{mixpanel.name_tag("foo@example.org")}) }
|
125
|
+
end
|
126
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require "bundler"
|
4
|
+
require "rails"
|
5
|
+
Rails.env = "test"
|
6
|
+
Bundler.require(:default, Rails.env)
|
7
|
+
require 'action_controller/railtie'
|
8
|
+
require 'action_view/railtie'
|
9
|
+
|
10
|
+
app = Class.new(Rails::Application)
|
11
|
+
app.config.root = File.dirname(__FILE__)
|
12
|
+
app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
|
13
|
+
app.config.active_support.deprecation = :log
|
14
|
+
app.config.action_dispatch.show_exceptions = false
|
15
|
+
|
16
|
+
app.config.event_tracker.mixpanel_key = "YOUR_TOKEN"
|
17
|
+
|
18
|
+
app.initialize!
|
19
|
+
|
20
|
+
app.routes.draw do
|
21
|
+
match ':controller(/:action(/:id))'
|
22
|
+
end
|
23
|
+
|
24
|
+
class ApplicationController < ActionController::Base; end
|
25
|
+
|
26
|
+
require 'rspec/rails'
|
27
|
+
require "steak"
|
28
|
+
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: event_tracker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paul McMahon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: steak
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Easy integration with Mixpanel and Kissmetrics for Rails
|
47
|
+
email:
|
48
|
+
- paul@mobalean.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- event_tracker.gemspec
|
59
|
+
- lib/event_tracker.rb
|
60
|
+
- lib/event_tracker/mixpanel.rb
|
61
|
+
- lib/event_tracker/version.rb
|
62
|
+
- spec/app/views/layouts/application.html.erb
|
63
|
+
- spec/event_tracker_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
homepage: ''
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
hash: 4227757316548368302
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
hash: 4227757316548368302
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.24
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Track using javascript from your controllers, even when redirecting
|
95
|
+
test_files:
|
96
|
+
- spec/app/views/layouts/application.html.erb
|
97
|
+
- spec/event_tracker_spec.rb
|
98
|
+
- spec/spec_helper.rb
|