mixpal 0.2.1 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29a0e62e6775b0e59acac47db4e0ef9c0af4b708
4
- data.tar.gz: bf499158ffa8e0e15dfff70c5f5c67286826eea4
3
+ metadata.gz: fb233388194e4bb65920f2397f440b69c0e84d3e
4
+ data.tar.gz: 83af8a3d7aca41e8cbbb049175847f3e74d0be81
5
5
  SHA512:
6
- metadata.gz: 6c41217c5011aa6a47f46203be1304a33d8421c8aa34614b593945fd37e53ecf531e64a5a30dd6c396f388075f12810cb866de5d47c24d5cbd41802bdc01409b
7
- data.tar.gz: 845c43bf95de75e6d679a2ee2b1f03f4ebea45398cd6c9c48a5cb222a74fa8c9d3595d70786994d7cbe2794b2343880e5b1d2b72f829973358e0fcac3112f6d8
6
+ metadata.gz: 576d25b01395d3f4e2dda4dfa00df0b93660bb358d914117d596dd472aea60d7caa90828daed689e752716caf0532574f2c78e41b9253e11444eb3c3c9759b51
7
+ data.tar.gz: fb5c7d9aacc19f111d8a82fdfdd5b36677542cd9d86a2ff79ccd5fc08415d05306f5d6d861a2ac44be53677718f33a81d88c2f42ff58de127a7ee5901a2b9930
@@ -16,9 +16,6 @@ MethodLength:
16
16
  DoubleNegation:
17
17
  Enabled: false
18
18
 
19
- AbcSize:
20
- Max: 20
21
-
22
19
  AllCops:
23
20
  Include:
24
21
  - "Rakefile"
data/README.md CHANGED
@@ -78,6 +78,38 @@ mixpanel.update_user email: "mynewemail@example.com"
78
78
 
79
79
  As with `register_user`, this method will also identify "special properties".
80
80
 
81
+ ### Custom Events
82
+
83
+ Mixpal allows you to define custom mixpal methods to use in your controllers/views
84
+
85
+ 1. create a custom module and define your mixpal events
86
+ ```ruby
87
+ module YourCustomEventsModule
88
+ def sign_up(user)
89
+ register_user user.attributes.slice('name', 'email')
90
+ track 'User signed up'
91
+ end
92
+ end
93
+ ```
94
+
95
+ 2. create a mixpal.rb initializer and configure mixpal to use your module
96
+ ```ruby
97
+ Mixpal.configure do |config|
98
+ config.helper_module = YourCustomEventsModule
99
+ end
100
+ ```
101
+
102
+ 3. use in controllers/views
103
+ ```ruby
104
+ class UserController < ActionController::Base
105
+ def create
106
+ # ... do cool stuff ...
107
+ mixpal.sign_up(user)
108
+ redirect_to root_path
109
+ end
110
+ end
111
+ ```
112
+
81
113
  ### Persistance Across Redirects
82
114
 
83
115
  Mixpal stores any tracked events or user data in the session when
@@ -7,6 +7,7 @@ module Mixpal
7
7
  autoload :Tracker, 'mixpal/tracker'
8
8
  autoload :Event, 'mixpal/event'
9
9
  autoload :User, 'mixpal/user'
10
+ autoload :Revenue, 'mixpal/revenue'
10
11
  autoload :Integration, 'mixpal/integration'
11
12
 
12
13
  class << self
@@ -0,0 +1,32 @@
1
+ module Mixpal
2
+ class Revenue
3
+ attr_reader :amount, :properties
4
+
5
+ def initialize(amount, properties)
6
+ @amount = amount
7
+ @properties = properties
8
+ end
9
+
10
+ def render
11
+ args = "#{amount}, #{properties_as_js_object_for_mixpanel}"
12
+ "mixpanel.people.track_charge(#{args});".html_safe
13
+ end
14
+
15
+ def to_store
16
+ {
17
+ 'amount' => amount,
18
+ 'properties' => properties
19
+ }
20
+ end
21
+
22
+ def self.from_store(data)
23
+ new(data['amount'], data['properties'])
24
+ end
25
+
26
+ private
27
+
28
+ def properties_as_js_object_for_mixpanel
29
+ Mixpal::Util.hash_to_js_object_string(properties)
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,6 @@
1
1
  module Mixpal
2
2
  class Tracker
3
- attr_reader :events, :user_updates, :identity, :alias_user
3
+ attr_reader :events, :user_updates, :revenue_updates, :identity, :alias_user
4
4
 
5
5
  STORAGE_KEY = 'mixpal'
6
6
 
@@ -9,6 +9,7 @@ module Mixpal
9
9
 
10
10
  @events = []
11
11
  @user_updates = []
12
+ @revenue_updates = []
12
13
 
13
14
  @identity = args[:identity]
14
15
  end
@@ -26,6 +27,10 @@ module Mixpal
26
27
  events << Mixpal::Event.new(name, properties)
27
28
  end
28
29
 
30
+ def track_charge(amount, properties = {})
31
+ revenue_updates << Mixpal::Revenue.new(amount, properties)
32
+ end
33
+
29
34
  def render
30
35
  ''.tap do |html|
31
36
  html << '<script type="text/javascript">'
@@ -33,6 +38,7 @@ module Mixpal
33
38
  html << "mixpanel.identify(\"#{identity}\");" if identity
34
39
  html << events.map(&:render).join('')
35
40
  html << user_updates.map(&:render).join('')
41
+ html << revenue_updates.map(&:render).join('')
36
42
  html << '</script>'
37
43
  end.html_safe
38
44
  end
@@ -56,6 +62,11 @@ module Mixpal
56
62
  data['user_updates'].map { |u| Mixpal::User.from_store(u) }
57
63
  end
58
64
 
65
+ if data['revenue_updates']
66
+ @revenue_updates =
67
+ data['revenue_updates'].map { |u| Mixpal::Revenue.from_store(u) }
68
+ end
69
+
59
70
  session.delete(STORAGE_KEY)
60
71
  end
61
72
 
@@ -66,7 +77,8 @@ module Mixpal
66
77
  'alias_user' => alias_user,
67
78
  'identity' => identity,
68
79
  'events' => events.map(&:to_store),
69
- 'user_updates' => user_updates.map(&:to_store)
80
+ 'user_updates' => user_updates.map(&:to_store),
81
+ 'revenue_updates' => revenue_updates.map(&:to_store)
70
82
  }
71
83
  end
72
84
  end
@@ -1,3 +1,3 @@
1
1
  module Mixpal
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mixpal::Revenue do
4
+ let(:properties) do
5
+ {
6
+ sku: 'SKU-1010'
7
+ }
8
+ end
9
+
10
+ let(:amount) { 50 }
11
+
12
+ let(:subject) { described_class.new(amount, properties) }
13
+
14
+ describe '#render' do
15
+ it 'delegates to Util for js_object composition' do
16
+ Mixpal::Util.should_receive(:hash_to_js_object_string).with(properties)
17
+ subject.render
18
+ end
19
+
20
+ it 'outputs a call to people.track_charge' do
21
+ js_object = Mixpal::Util.hash_to_js_object_string(properties)
22
+ expect(subject.render)
23
+ .to eq "mixpanel.people.track_charge(#{amount}, #{js_object});"
24
+ end
25
+
26
+ it 'outputs an html safe string' do
27
+ expect(subject.render).to be_html_safe
28
+ end
29
+ end
30
+
31
+ describe '#to_store' do
32
+ it 'returns a hash with its data' do
33
+ expect(subject.to_store).to eq(
34
+ 'amount' => amount,
35
+ 'properties' => properties
36
+ )
37
+ end
38
+ end
39
+
40
+ describe '#from_store' do
41
+ let(:result) do
42
+ described_class.from_store(
43
+ 'amount' => amount,
44
+ 'properties' => properties
45
+ )
46
+ end
47
+
48
+ it 'instantiates a new instance' do
49
+ expect(result).to be_an_instance_of(described_class)
50
+ end
51
+
52
+ it 'sets its amount from the data' do
53
+ expect(result.amount).to eq amount
54
+ end
55
+
56
+ it 'sets its properties from the data' do
57
+ expect(result.properties).to eq properties
58
+ end
59
+ end
60
+ end
@@ -26,6 +26,10 @@ describe Mixpal::Tracker do
26
26
  expect(subject.user_updates).to eq []
27
27
  end
28
28
 
29
+ it 'creates an empty set of revenue_updates' do
30
+ expect(subject.revenue_updates).to eq []
31
+ end
32
+
29
33
  context 'with an :identity arg' do
30
34
  subject { subject_with_identity }
31
35
 
@@ -82,6 +86,22 @@ describe Mixpal::Tracker do
82
86
  end
83
87
  end
84
88
 
89
+ describe '#track_charge' do
90
+ it 'instantiates a new Revenue object with properties' do
91
+ properties = { sku: 'SKU-1010' }
92
+ Mixpal::Revenue.should_receive(:new).with(50, properties)
93
+ subject.track_charge(50, properties)
94
+ end
95
+
96
+ it 'adds the Revenue to revenue_updates for rendering later' do
97
+ expect do
98
+ subject.track_charge(50, sku: 'SKU-1010')
99
+ end.to change(subject.revenue_updates, :size).by(1)
100
+
101
+ subject.revenue_updates.first.should be_an_instance_of(Mixpal::Revenue)
102
+ end
103
+ end
104
+
85
105
  describe '#render' do
86
106
  it 'outputs script tag' do
87
107
  expect(subject.render).to have_tag('script')
@@ -152,6 +172,24 @@ describe Mixpal::Tracker do
152
172
  expect(subject.render).to include joined
153
173
  end
154
174
  end
175
+
176
+ context 'with revenue' do
177
+ before do
178
+ subject.track_charge(50)
179
+ subject.track_charge(100, sku: 'SKU-1010')
180
+ end
181
+
182
+ it 'delegates render to the revenues' do
183
+ subject.revenue_updates.each { |r| r.should_receive :render }
184
+ subject.render
185
+ end
186
+
187
+ it 'joins each rendered revenue' do
188
+ joined =
189
+ subject.revenue_updates[0].render + subject.revenue_updates[1].render
190
+ expect(subject.render).to include joined
191
+ end
192
+ end
155
193
  end
156
194
 
157
195
  describe '#store!' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixpal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - patbenatar
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-13 00:00:00.000000000 Z
12
+ date: 2015-10-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -184,12 +184,14 @@ files:
184
184
  - lib/mixpal.rb
185
185
  - lib/mixpal/event.rb
186
186
  - lib/mixpal/integration.rb
187
+ - lib/mixpal/revenue.rb
187
188
  - lib/mixpal/tracker.rb
188
189
  - lib/mixpal/user.rb
189
190
  - lib/mixpal/util.rb
190
191
  - lib/mixpal/version.rb
191
192
  - mixpanel_assistant.gemspec
192
193
  - spec/lib/mixpal/event_spec.rb
194
+ - spec/lib/mixpal/revenue_spec.rb
193
195
  - spec/lib/mixpal/tracker_spec.rb
194
196
  - spec/lib/mixpal/user_spec.rb
195
197
  - spec/lib/mixpal/util_spec.rb
@@ -275,6 +277,7 @@ summary: As the JavaScript library is Mixpanel's preferred method of usage, Mixp
275
277
  form submissions.
276
278
  test_files:
277
279
  - spec/lib/mixpal/event_spec.rb
280
+ - spec/lib/mixpal/revenue_spec.rb
278
281
  - spec/lib/mixpal/tracker_spec.rb
279
282
  - spec/lib/mixpal/user_spec.rb
280
283
  - spec/lib/mixpal/util_spec.rb