rack-mixpanel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-mixpanel.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Derek Barnes
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.
@@ -0,0 +1,61 @@
1
+ # Rack::Mixpanel
2
+
3
+ Rack::Mixpanel provides a piece of Rack middleware for tracking events with
4
+ the same identity as set in the client-side JavaScript API. This is
5
+ achieved through parsing the cookie that Mixpanel sets on your site's
6
+ domain.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'rack-mixpanel', :require => 'rack/mixpanel'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rack-mixpanel
21
+
22
+ ### Rails
23
+
24
+ In your config/application.rb:
25
+
26
+ ```
27
+ config.middleware.use Rack::Mixpanel
28
+ ```
29
+
30
+ This will use the "MIXPANEL_API_TOKEN" environment variable as the
31
+ Mixpanel API token.
32
+
33
+ Alternatively, you may set this explicitly:
34
+
35
+ ```
36
+ config.middleware.use Rack::Mixpanel, '54c0b5d418d4215a1061a894736eeed9'
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### Track events
42
+
43
+ ```ruby
44
+ env['mixpanel.tracker'].track event_name, properties
45
+ ```
46
+
47
+ Example:
48
+
49
+ ```ruby
50
+ env['mixpanel.tracker'].track 'Sent message', {:age => 25,
51
+ :recipient_age => 23}
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Pick out an [emoji](http://www.emoji-cheat-sheet.com).
59
+ 4. Commit your changes (`git commit -am ':new: Add some feature'`)
60
+ 5. Push to the branch (`git push origin my-new-feature`)
61
+ 6. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,49 @@
1
+ require 'base64'
2
+ require 'json'
3
+ require 'open-uri'
4
+
5
+ require 'rack'
6
+
7
+ module Rack
8
+ class Mixpanel
9
+ def initialize(app, token=nil)
10
+ @app = app
11
+ @token = token || ENV['MIXPANEL_API_TOKEN']
12
+ end
13
+
14
+ def call(env)
15
+ request = Rack::Request.new(env)
16
+ mp_cookie = request.cookies["mp_#{@token}_mixpanel"]
17
+ if mp_cookie
18
+ mp_env = JSON.parse(mp_cookie)
19
+ distinct_id = mp_env['distinct_id']
20
+ env['mixpanel.distinct_id'] = distinct_id
21
+ end
22
+ env['mixpanel.tracker'] = Tracker.new(@token, request.ip, env['mixpanel.distinct_id'])
23
+ @app.call(env)
24
+ end
25
+
26
+ class Tracker
27
+ attr_reader :thread
28
+ def initialize(token, ip, distinct_id)
29
+ @token = token
30
+ @ip = ip
31
+ @distinct_id = distinct_id
32
+ end
33
+
34
+ def track(event, properties)
35
+ base_props = {
36
+ 'token' => @token,
37
+ 'ip' => @ip,
38
+ 'distinct_id' => @distinct_id
39
+ }
40
+ enc_props = Base64.encode64 JSON.generate({'event' => event, 'properties' => base_props.merge(properties)})
41
+ uri = URI('https://api.mixpanel.com/track')
42
+ uri.query = URI.encode_www_form(:data => enc_props)
43
+ @thread = Thread.new { uri.read }
44
+ nil
45
+ end
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "rack-mixpanel"
7
+ gem.version = "0.1.0"
8
+ gem.authors = ["Derek Barnes"]
9
+ gem.email = ["barnes@okcupidlabs.com"]
10
+ gem.description = %q{Rack middleware for tracking Mixpanel events}
11
+ gem.summary = %q{Rack middleware for tracking Mixpanel events}
12
+ gem.homepage = ""
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency 'rack'
20
+ gem.add_development_dependency 'fakeweb'
21
+ gem.add_development_dependency 'rspec'
22
+ end
@@ -0,0 +1,70 @@
1
+ require 'rack/mixpanel'
2
+ require 'fakeweb'
3
+
4
+ describe Rack::Mixpanel do
5
+ let(:distinct_id) { '405e1d32-8e1e-4d04-b1ee-d060134c5dcf' }
6
+ let(:token) { '660f899ecec3091074cc828dde2a2bcd' }
7
+ let(:cookie) { "mp_#{token}_mixpanel=%7B%22distinct_id%22%3A%22#{distinct_id}%22%2C%22%24initial_referrer%22%3A%22%24direct%22%2C%22%24initial_referring_domain%22%3A%22%24direct%22%7D" }
8
+
9
+ subject { Rack::MockRequest.new(app).get('/', 'HTTP_COOKIE' => cookie) }
10
+
11
+ describe 'env.distinct_id' do
12
+ # An app that returns env['mixpanel.distinct_id'] in the body
13
+ let(:distinct_id_app) do
14
+ lambda { |env| [200, {'Content-Type' => 'text/plain'}, [env['mixpanel.distinct_id']]] }
15
+ end
16
+ context 'when a token is set in the app' do
17
+ let(:app) { Rack::Mixpanel.new(distinct_id_app, token) }
18
+ context 'and a cookie is set' do
19
+ its(:body) { should == distinct_id }
20
+ end
21
+ context 'and a cookie is not set' do
22
+ let(:cookie) { '' }
23
+ its(:body) { should == '' }
24
+ end
25
+ end
26
+
27
+ context 'when a token is set in the environment' do
28
+ let(:app) { Rack::Mixpanel.new(distinct_id_app) }
29
+ before { ENV['MIXPANEL_API_TOKEN'] = token }
30
+ context 'and a cookie is set' do
31
+ its(:body) { should == distinct_id }
32
+ end
33
+ context 'and a cookie is not set' do
34
+ let(:cookie) { '' }
35
+ its(:body) { should == '' }
36
+ end
37
+ end
38
+ end
39
+
40
+ describe 'env.tracker' do
41
+ # An app that returns the response from the env['mixpanel.track'].track call
42
+ # and the track call's request thread
43
+ let(:tracker_app) do
44
+ lambda do |env|
45
+ res = env['mixpanel.tracker'].track(event, properties)
46
+ val = env['mixpanel.tracker'].thread.value
47
+ [200, {'Content-Type' => 'text/plain'}, [res.nil?, "\n", val]]
48
+ end
49
+ end
50
+ let(:event) { 'signup' }
51
+ let(:properties) { {'gender' => 'male', 'age' => 45} }
52
+ let(:app) { Rack::Mixpanel.new(tracker_app, token) }
53
+ it 'sends a request to Mixpanel' do
54
+ props = {
55
+ 'event' => event,
56
+ 'properties' => {
57
+ 'token' => token,
58
+ 'ip' => nil,
59
+ 'distinct_id' => distinct_id
60
+ }.merge(properties)
61
+ }
62
+ enc_params = Base64.encode64 JSON.generate(props)
63
+ query = URI.encode_www_form(:data => enc_params)
64
+ uri = URI('https://api.mixpanel.com/track')
65
+ uri.query = query
66
+ FakeWeb.register_uri(:get, uri, :body => '1')
67
+ subject.body.split("\n").should == ['true', '1']
68
+ end
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-mixpanel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Derek Barnes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: fakeweb
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
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Rack middleware for tracking Mixpanel events
63
+ email:
64
+ - barnes@okcupidlabs.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/rack/mixpanel.rb
75
+ - rack-mixpanel.gemspec
76
+ - spec/rack/mixpanel_spec.rb
77
+ homepage: ''
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.24
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Rack middleware for tracking Mixpanel events
101
+ test_files:
102
+ - spec/rack/mixpanel_spec.rb