em-mixpanel 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # EM-Mixpanel
2
+
3
+ Async tracker for the Mixpanel API. It must run inside an [Eventmachine](https://github.com/eventmachine/eventmachine) reactor like the [Thin](https://github.com/macournoyer/thin) web server.
4
+
5
+ ## Usage
6
+
7
+ require 'em/mixpanel'
8
+ tracker = EM::Mixpanel.new(ENV['MIXPANEL_TOKEN'])
9
+
10
+ # This data point intentially doesn't have an IP
11
+ tracker.track 'joined game', distinct_id: 'chrislloyd', world: 'minebnb'
12
+
13
+ # For import events you need to set the time manually
14
+ tracker.import '$signup', distinct_id: 'chrislloyd', time: 1321499371
15
+
16
+ ### Rails
17
+
18
+ # app/controllers/application_controller.rb
19
+
20
+ def mixpanel
21
+ @mixpanel ||= EM::Mixpanel.new(ENV['MIXPANEL_TOKEN'], ip: request.ip)
22
+ end
23
+
24
+ # app/controllers/photos_controller.rb
25
+
26
+ def create
27
+ # ...
28
+ mixpanel.track 'photo uploaded', distinct_id: current_user.id.to_s
29
+ end
30
+
31
+
32
+ ## License
33
+
34
+ Copyright (C) 2011 David Newman & Chris Lloyd.
35
+
36
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
37
+ this software and associated documentation files (the "Software"), to deal in
38
+ the Software without restriction, including without limitation the rights to
39
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
40
+ of the Software, and to permit persons to whom the Software is furnished to do
41
+ so, subject to the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be included in all
44
+ copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
52
+ SOFTWARE.
53
+
54
+
55
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'rake/clean'
2
+
3
+ CLOBBER.add '*.gem'
Binary file
data/em-mixpanel.gemspec CHANGED
@@ -4,10 +4,10 @@ require "em/mixpanel/version"
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'em-mixpanel'
6
6
  s.version = EM::Mixpanel::VERSION.dup
7
- s.email = "dave@minefold.com"
8
- s.author = 'Dave Newman'
7
+ s.email = 'chris@minefold.com'
8
+ s.authors = ['Chris Lloyd', 'Dave Newman']
9
9
 
10
- s.summary = 'Non-blocking Mixpanel tracker'
10
+ s.summary = 'Non-blocking Mixpanel API'
11
11
 
12
12
  s.add_dependency 'em-http-request'
13
13
 
@@ -0,0 +1 @@
1
+ require 'em/mixpanel'
data/lib/em/mixpanel.rb CHANGED
@@ -1,2 +1,50 @@
1
+ require 'base64'
2
+ require 'json'
1
3
  require 'em/mixpanel/version'
2
- require 'em/mixpanel/tracker'
4
+ require 'em-http'
5
+
6
+ module EM
7
+ class Mixpanel
8
+ TRACK_URI = 'http://api.mixpanel.com/track'
9
+ IMPORT_URI = 'http://api.mixpanel.com/import'
10
+
11
+ attr_reader :token, :default_properties
12
+
13
+ def initialize(token, default_properties={})
14
+ @token, @default_properties = token, default_properties
15
+ end
16
+
17
+ def track(event, properties={})
18
+ data = self.class.encode_data(event, {
19
+ token: token,
20
+ time: Time.now.to_i
21
+ }.merge(default_properties).merge(properties))
22
+
23
+ EM::HttpRequest.new(TRACK_URI.to_s).post(
24
+ body: {data: data},
25
+ query: {ip: 0}
26
+ )
27
+ end
28
+
29
+ def import(event, properties={})
30
+ data = self.class.encode_data(event, {
31
+ token: token
32
+ }.merge(default_properties).merge(properties))
33
+
34
+ EM::HttpRequest.new(IMPORT_URI.to_s).get(
35
+ query: {
36
+ data: data,
37
+ api_key: token
38
+ }
39
+ )
40
+ end
41
+
42
+ private
43
+
44
+ def self.encode_data(event, properties)
45
+ params = {event: event, properties: properties}
46
+ Base64.strict_encode64 params.to_json
47
+ end
48
+
49
+ end
50
+ end
@@ -1,5 +1,5 @@
1
1
  module EM
2
- module Mixpanel
3
- VERSION = '0.0.1'
2
+ class Mixpanel
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,19 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-mixpanel
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:
8
+ - Chris Lloyd
8
9
  - Dave Newman
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2011-12-13 00:00:00.000000000 Z
13
+ date: 2011-12-26 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: em-http-request
16
- requirement: &70232352198300 !ruby/object:Gem::Requirement
17
+ requirement: &70194762427320 !ruby/object:Gem::Requirement
17
18
  none: false
18
19
  requirements:
19
20
  - - ! '>='
@@ -21,17 +22,20 @@ dependencies:
21
22
  version: '0'
22
23
  type: :runtime
23
24
  prerelease: false
24
- version_requirements: *70232352198300
25
+ version_requirements: *70194762427320
25
26
  description:
26
- email: dave@minefold.com
27
+ email: chris@minefold.com
27
28
  executables: []
28
29
  extensions: []
29
30
  extra_rdoc_files: []
30
31
  files:
32
+ - em-mixpanel-0.0.1.gem
31
33
  - em-mixpanel.gemspec
32
- - lib/em/mixpanel/tracker.rb
33
34
  - lib/em/mixpanel/version.rb
34
35
  - lib/em/mixpanel.rb
36
+ - lib/em-mixpanel.rb
37
+ - Rakefile
38
+ - README.md
35
39
  homepage:
36
40
  licenses: []
37
41
  post_install_message:
@@ -55,5 +59,5 @@ rubyforge_project:
55
59
  rubygems_version: 1.8.12
56
60
  signing_key:
57
61
  specification_version: 3
58
- summary: Non-blocking Mixpanel tracker
62
+ summary: Non-blocking Mixpanel API
59
63
  test_files: []
@@ -1,44 +0,0 @@
1
- require 'base64'
2
- require 'json'
3
-
4
- module EM
5
- module Mixpanel
6
- class Tracker
7
- attr_reader :env, :token
8
-
9
- def initialize(token, env)
10
- @token, @env = token, env
11
- end
12
-
13
- def track(event, properties={})
14
- if token and Rails.env.production?
15
- http = EventMachine::HttpRequest.new('http://api.mixpanel.com/track/').post body: { data: encoded_data(event, properties) }
16
- else
17
- Rails.logger.info "[Mixpanel] #{event} #{properties.to_json}"
18
- end
19
- end
20
-
21
- def ip
22
- env.has_key?("REMOTE_ADDR") ? @env["REMOTE_ADDR"] : '1'
23
- end
24
-
25
- def default_properties
26
- {
27
- ip: ip,
28
- time: Time.now.to_i,
29
- token: token
30
- }
31
- end
32
-
33
- def encoded_data(event, properties)
34
- params = {
35
- event: event,
36
- properties: default_properties.merge(properties)
37
- }
38
-
39
- ActiveSupport::Base64.encode64s(JSON.generate(params))
40
- end
41
-
42
- end
43
- end
44
- end