mixpanel 3.5.1 → 3.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +64 -0
- data/lib/mixpanel/middleware.rb +13 -2
- data/lib/mixpanel/person.rb +14 -1
- data/mixpanel.gemspec +1 -1
- data/spec/mixpanel/middleware_spec.rb +12 -0
- data/spec/mixpanel/tracker_spec.rb +4 -0
- metadata +18 -18
data/README.md
CHANGED
@@ -346,6 +346,12 @@ Occasionally you may need to send a request for HTML that you don't want the mid
|
|
346
346
|
});
|
347
347
|
```
|
348
348
|
|
349
|
+
Alternatively, you can add this line of code to your controller to temporarily disable the middleware:
|
350
|
+
|
351
|
+
```ruby
|
352
|
+
Mixpanel::Middleware.skip_this_request
|
353
|
+
```
|
354
|
+
|
349
355
|
## Examples
|
350
356
|
|
351
357
|
### How to use it from Rails controllers?
|
@@ -415,6 +421,61 @@ end
|
|
415
421
|
end
|
416
422
|
```
|
417
423
|
|
424
|
+
## How to track events using Delayed Job and Rails
|
425
|
+
Below is an example of implementing async even tracking with Delayed Job
|
426
|
+
|
427
|
+
**Create a new worker**
|
428
|
+
```ruby
|
429
|
+
class MixpanelWorker < Struct.new(:name, :properties, :request_env)
|
430
|
+
def perform
|
431
|
+
if defined?(MIXPANEL_TOKEN)
|
432
|
+
@mixpanel = Mixpanel::Tracker.new(MIXPANEL_TOKEN, { :env => request_env })
|
433
|
+
else
|
434
|
+
@mixpanel = DummyMixpanel.new
|
435
|
+
end
|
436
|
+
|
437
|
+
@mixpanel.track(name, properties)
|
438
|
+
end
|
439
|
+
end
|
440
|
+
```
|
441
|
+
|
442
|
+
**Add the following to your Application controller**
|
443
|
+
```ruby
|
444
|
+
class ApplicationController < ActionController::Base
|
445
|
+
before_filter :initialize_env
|
446
|
+
|
447
|
+
private
|
448
|
+
##
|
449
|
+
# Initialize env for mixpanel
|
450
|
+
def initialize_env
|
451
|
+
# Similar to the Resque problem above, we need to help DJ serialize the
|
452
|
+
# request object.
|
453
|
+
@request_env = {
|
454
|
+
'REMOTE_ADDR' => request.env['REMOTE_ADDR'],
|
455
|
+
'HTTP_X_FORWARDED_FOR' => request.env['HTTP_X_FORWARDED_FOR'],
|
456
|
+
'rack.session' => request.env['rack.session'].to_hash,
|
457
|
+
'mixpanel_events' => request.env['mixpanel_events']
|
458
|
+
}
|
459
|
+
end
|
460
|
+
```
|
461
|
+
**You can optionally create a nice model wrapper to tidy things up**
|
462
|
+
```ruby
|
463
|
+
#app/models/mix_panel.rb
|
464
|
+
class MixPanel
|
465
|
+
def self.track(name, properties, env)
|
466
|
+
# Notice we are using the 'mixpanel' queue
|
467
|
+
Delayed::Job.enqueue MixpanelWorker.new(name, properties, env), queue: 'mixpanel'
|
468
|
+
end
|
469
|
+
end
|
470
|
+
```
|
471
|
+
**Sample Usage**
|
472
|
+
```ruby
|
473
|
+
MixPanel.track("Front Page Load", {
|
474
|
+
url_type: short_url.uid_type,
|
475
|
+
page_name: short_url.page.name,
|
476
|
+
distinct_id: @client_uid }, @request_env)
|
477
|
+
```
|
478
|
+
|
418
479
|
## Supported Ruby Platforms
|
419
480
|
|
420
481
|
- 1.8.7
|
@@ -444,3 +505,6 @@ end
|
|
444
505
|
* [Jon Pospischil] (https://github.com/pospischil)
|
445
506
|
* [Tom Brown] (https://github.com/nottombrown)
|
446
507
|
* [Murilo Pereira] (https://github.com/mpereira)
|
508
|
+
* [Marko Vasiljevic] (https://github.com/marmarko)
|
509
|
+
* [Joel] (https://github.com/jclay)
|
510
|
+
* [adimichele] (https://github.com/adimichele)
|
data/lib/mixpanel/middleware.rb
CHANGED
@@ -3,6 +3,15 @@ require 'json'
|
|
3
3
|
|
4
4
|
module Mixpanel
|
5
5
|
class Middleware
|
6
|
+
class << self
|
7
|
+
attr_accessor :skip_request
|
8
|
+
def skip_this_request
|
9
|
+
@skip_request = true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
@skip_request = false
|
14
|
+
|
6
15
|
def initialize(app, mixpanel_token, options={})
|
7
16
|
@app = app
|
8
17
|
@token = mixpanel_token
|
@@ -18,14 +27,16 @@ module Mixpanel
|
|
18
27
|
@env = env
|
19
28
|
|
20
29
|
@status, @headers, @response = @app.call(env)
|
21
|
-
|
22
|
-
if is_trackable_response?
|
30
|
+
|
31
|
+
if is_trackable_response? && !Mixpanel::Middleware.skip_request
|
23
32
|
merge_queue! if @options[:persist]
|
24
33
|
update_response!
|
25
34
|
update_content_length!
|
26
35
|
delete_event_queue!
|
27
36
|
end
|
28
37
|
|
38
|
+
Mixpanel::Middleware.skip_request = false
|
39
|
+
|
29
40
|
[@status, @headers, @response]
|
30
41
|
end
|
31
42
|
|
data/lib/mixpanel/person.rb
CHANGED
@@ -7,6 +7,10 @@ module Mixpanel::Person
|
|
7
7
|
engage :set, distinct_id, properties, options
|
8
8
|
end
|
9
9
|
|
10
|
+
def unset(distinct_id, property, options={})
|
11
|
+
engage :unset, distinct_id, property, options
|
12
|
+
end
|
13
|
+
|
10
14
|
def increment(distinct_id, properties={}, options={})
|
11
15
|
engage :add, distinct_id, properties, options
|
12
16
|
end
|
@@ -53,7 +57,12 @@ module Mixpanel::Person
|
|
53
57
|
|
54
58
|
request_properties = person_request_properties(request_properties_or_distinct_id)
|
55
59
|
|
56
|
-
|
60
|
+
if action == :unset
|
61
|
+
data = build_person_unset request_properties, properties
|
62
|
+
else
|
63
|
+
data = build_person action, request_properties, properties
|
64
|
+
end
|
65
|
+
|
57
66
|
url = "#{options[:url]}?data=#{encoded_data(data)}"
|
58
67
|
parse_response request(url, options[:async])
|
59
68
|
end
|
@@ -70,4 +79,8 @@ module Mixpanel::Person
|
|
70
79
|
def build_person(action, request_properties, person_properties)
|
71
80
|
properties_hash(request_properties, PERSON_REQUEST_PROPERTIES).merge({ "$#{action}".to_sym => properties_hash(person_properties, PERSON_PROPERTIES) })
|
72
81
|
end
|
82
|
+
|
83
|
+
def build_person_unset(request_properties, property)
|
84
|
+
properties_hash(request_properties, PERSON_REQUEST_PROPERTIES).merge({ "$unset".to_sym => [property] })
|
85
|
+
end
|
73
86
|
end
|
data/mixpanel.gemspec
CHANGED
@@ -2,7 +2,7 @@ files = ['README.md', 'LICENSE', 'Rakefile', 'mixpanel.gemspec', '{spec,lib}/**/
|
|
2
2
|
|
3
3
|
spec = Gem::Specification.new do |s|
|
4
4
|
s.name = "mixpanel"
|
5
|
-
s.version = "3.5.
|
5
|
+
s.version = "3.5.2"
|
6
6
|
s.rubyforge_project = "mixpanel"
|
7
7
|
s.description = "Simple lib to track events in Mixpanel service. It can be used in any rack based framework."
|
8
8
|
s.author = "Alvaro Gil"
|
@@ -47,6 +47,18 @@ describe Mixpanel::Middleware do
|
|
47
47
|
get "/"
|
48
48
|
Nokogiri::HTML(last_response.body).search('script').size.should == 1
|
49
49
|
end
|
50
|
+
|
51
|
+
context "when disabling with #skip_this_request" do
|
52
|
+
before{ Mixpanel::Middleware.skip_this_request }
|
53
|
+
|
54
|
+
it "should skip this request but not the next request" do
|
55
|
+
get "/"
|
56
|
+
Nokogiri::HTML(last_response.body).search('script').should be_empty
|
57
|
+
get "/"
|
58
|
+
Nokogiri::HTML(last_response.body).search('script').size.should == 1
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
50
62
|
end
|
51
63
|
|
52
64
|
describe "Appending async mixpanel scripts" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mixpanel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.5.
|
4
|
+
version: 3.5.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-24 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153291720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2153291720
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rack
|
27
|
-
requirement: &
|
27
|
+
requirement: &2153291280 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2153291280
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: escape
|
38
|
-
requirement: &
|
38
|
+
requirement: &2153290860 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2153290860
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &2153290440 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2153290440
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rack-test
|
60
|
-
requirement: &
|
60
|
+
requirement: &2153290020 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2153290020
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: fakeweb
|
71
|
-
requirement: &
|
71
|
+
requirement: &2153289600 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *2153289600
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: nokogiri
|
82
|
-
requirement: &
|
82
|
+
requirement: &2153289180 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *2153289180
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rake
|
93
|
-
requirement: &
|
93
|
+
requirement: &2153288760 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,7 +98,7 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *2153288760
|
102
102
|
description: Simple lib to track events in Mixpanel service. It can be used in any
|
103
103
|
rack based framework.
|
104
104
|
email: zevarito@gmail.com
|