lascivious 0.1.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.2)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.4)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Cloudability Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,218 @@
1
+ = Lascivious
2
+
3
+ This plugin simplifies the use of Kiss Metrics with Rails.
4
+
5
+ Kiss Metrics really works best with Javascript. The problem is that in Rails the best place to decide whether to fire off an event is inside a Controller.
6
+
7
+ Using a flash mechanism this plugin provides a series of helper functions to allow your Controller to inject the correct Javascript into any page.
8
+
9
+ It's a work-in-progress right now and is pretty dumb: you have to RTFM on when to insert what. We will probably extend functionality to handle aliasing and strong typing of users.
10
+
11
+
12
+ == Instructions
13
+
14
+ 1. Install the gem by adding it to your Gemfile
15
+ 2. Add an initializer in `config/initializers/kiss_metrics.rb` like:
16
+
17
+ KissMetrics.setup do |config|
18
+ config.api_key="0000000000000000000000000000000000000000"
19
+ end
20
+
21
+ 3. Replace the zeroes with your API key from https://www.kissmetrics.com/settings
22
+ 4. Add the kiss metrics tag to whichever layouts you want to use Kiss Metrics with, usually all of them, e.g. here's what our header partial looks like:
23
+
24
+ <title><%= title %></title>
25
+ <%= csrf_meta_tag %>
26
+ <link rel="image_src" href="/images/facebook-icon.png"/>
27
+ <%= kiss_metrics_tag %>
28
+
29
+ 5. Now all you have to do is add Kiss Metrics tags in controllers or views wherever you need something. For instance:
30
+
31
+ class SomeController < ApplicationController
32
+ def index
33
+ kiss_record "SomeController loaded"
34
+ end
35
+ end
36
+
37
+ Currently the following commands are provided:
38
+
39
+ - `kiss_record <message>` - adds a 'record' event with a message of 'message'
40
+ - `kiss_metric <event_type> <message>` - adds an event of type 'event_type' with a message of 'message'
41
+
42
+ We will soon add helpers for things like `kiss_alias`, etc.
43
+
44
+
45
+ == How to Integrate with your app
46
+
47
+ Our service is built on Rails 3 with Devise and Inherited Resources. This is how we integrated Kiss Metrics into our app.
48
+
49
+ 0. Everywhere
50
+
51
+ In all cases add this to the HEAD of your layouts:
52
+
53
+ <%= kiss_metrics_tag %>
54
+
55
+ And define your keys via an initializer in `app/config/initializers/kiss_metrics.rb` like this:
56
+
57
+ KissMetrics.setup do |config|
58
+ if Rails.env == 'production'
59
+ config.api_key="2222222222222222222222222222222222222222"
60
+ else
61
+ config.api_key="1111111111111111111111111111111111111111"
62
+ end
63
+ end
64
+
65
+
66
+ 1. Sign In
67
+
68
+ We use a Controller override in Devise as we want to handle a failed login very specifically. So we have in `app/controllers/users/sessions_controller.rb`:
69
+
70
+ class Users::SessionsController < Devise::SessionsController
71
+ def create
72
+ warden_opts = { :scope => resource_name, :recall => "#{controller_path}#new" }
73
+ resource = warden.authenticate(warden_opts)
74
+ if(resource.nil?)
75
+ kind = :invalid
76
+ resource = build_resource
77
+ resource.errors[:base] = I18n.t("#{resource_name}.#{kind}", {
78
+ scope: "devise.failure",
79
+ default: [kind],
80
+ resource_name: resource_name
81
+ })
82
+ else
83
+ kiss_identify resource.email
84
+ kiss_record "Signed In"
85
+ set_flash_message(:notice, :signed_in) if is_navigational_format?
86
+ sign_in(resource_name, resource)
87
+ end
88
+ respond_with resource, :location => redirect_location(resource_name, resource)
89
+ end
90
+ end
91
+
92
+ A much simpler version would be an `after_sign_in_path_for` override in `app/controllers/application_controller.rb`:
93
+
94
+ private
95
+
96
+ def after_sign_in_path_for(resource_or_scope)
97
+ kiss_identify resource_or_scope.email unless resource_or_scope.email.nil?
98
+ kiss_record "Signed In"
99
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
100
+ home_path = "#{scope}_root_path"
101
+ respond_to?(home_path, true) ? send(home_path) : root_path
102
+ end
103
+
104
+ 2. Sign Out
105
+
106
+ We added this to `app/controllers/application_controller.rb`:
107
+
108
+ private
109
+
110
+ # Record a sign out
111
+ def after_sign_out_path_for(resource_or_scope)
112
+ kiss_record "Signed Out"
113
+ new_user_session_path
114
+ end
115
+
116
+ The `new_user_session_path` is important: if you redirect to `root_path` you will be redirected to the login page (as your user will now fail authorization) and in the process your flash will be wiped clear.
117
+
118
+ 3. Email Open
119
+
120
+ Our Mailers typically look like this:
121
+
122
+ def send_mail(user, recipient, bill_period, subject)
123
+ @recipient ||= user
124
+ @user = user
125
+ @bill_period = bill_period
126
+ @data = collate(@user, @bill_period)
127
+
128
+ mail({
129
+ to: formatted_address(@user, @recipient),
130
+ subject: subject
131
+ })
132
+ end
133
+
134
+ The @recipient variable is important to us, it allows us to send an email even if we don't have a user setup.
135
+
136
+ Now inside the email partial we do:
137
+
138
+ ... our email ERB or HAML template ...
139
+ <%= kiss_metrics_email_beacon @recipient.email, "Summary" %>
140
+ </body>
141
+ </html>
142
+
143
+ Points to note here:
144
+ - This only works inside HTML emails and even then not all the time. If this doesn't make sense to you go Google 'email pixels' or 'email beacons'
145
+ - Change "Summary" to be whichever email variant you have, it could be 'Welcome Email' or 'Bill Reminder', etc.
146
+ - We've put the kiss_metrics_email_beacon at the bottom of the email, right before the closing BODY tag. This reduces the chance the pixel kills your layout and means the open is only triggered if the email is properly downloaded and parsed.
147
+
148
+ 4. General Activity
149
+
150
+ You get this for free on every page where you have included the `kiss_metrics_tag` included in your layout.
151
+
152
+ 5. Identity
153
+
154
+ See the kiss_identify tag in section 1 above. We use the email address but you could use a hash of this or the user record ID if you don't want to put the email address inside a page. We prefer the email address (it's easier to understand what's happening on a user by user basis) but some folks don't like to put an email address inside a web page.
155
+
156
+ 6. Activation & Sign Up
157
+
158
+ This gets a bit awkward. We have an 'invite' model that is a bit unusual. Without going into the details this is what the controller looks like:
159
+
160
+ class InvitesController < InheritedResources::Base
161
+ respond_to :html
162
+ actions :new, :create
163
+
164
+ def new
165
+ new! do
166
+ kiss_record "Activated"
167
+ end
168
+ end
169
+
170
+ def create
171
+ create! do |success, failure|
172
+ success.html do
173
+ kiss_record "Signed Up"
174
+ sign_in(@invite.user)
175
+ kiss_identify current_user.email
176
+ redirect_to first_page_in_your_post_sign_up_path
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ Beyond this you're on your own here, sorry.
183
+
184
+ 7. Dev vs Prod
185
+
186
+ If you don't setup two sites - one for prod and the other for dev - your Prod site will get polluted with your dev work. Or you can simply disable it. See the example in step 0 above for a template.
187
+
188
+ 8. Other Stuff
189
+
190
+ You can add other events to your app by simply stating:
191
+
192
+ kiss_record "Some Other Event"
193
+
194
+ You might want to record for instance the first time someone returns to your site after they have purchased your product. You can work these events into Kiss Metrics really easily, with no setup required on the KM side.
195
+
196
+ Lascivious also supports:
197
+
198
+ - kiss_set(value)
199
+ - kiss_identify(value)
200
+ - kiss_alias(value)
201
+ - kiss_metric(event_type, value)
202
+
203
+
204
+ == Contributing to lascivious
205
+
206
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
207
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
208
+ * Fork the project
209
+ * Start a feature/bugfix branch
210
+ * Commit and push until you are happy with your contribution
211
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
212
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
213
+
214
+ == Copyright
215
+
216
+ Copyright (c) 2011 Cloudability Inc. See LICENSE.txt for
217
+ further details.
218
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ require './lib/version.rb'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "lascivious"
19
+ gem.homepage = "https://github.com/cloudability/lascivious"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Easy Kiss Metrics integration for Rails}
22
+ gem.description = %Q{Easy interface between Rails & Javascript for Kiss Metrics}
23
+ gem.email = "support@cloudability.com"
24
+ gem.authors = ["Mat Ellis"]
25
+ gem.version = Lascivious::Version::STRING
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ test.rcov_opts << '--exclude "gems/*"'
43
+ end
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "lascivious #{Lascivious::Version::STRING}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
@@ -0,0 +1 @@
1
+ <img src="https://trk.kissmetrics.com/e?_k=<%= kiss_metrics_api_key %>&_n=<%= event_type %><% unless email.blank? -%>&_p=<%= email %><% end -%><% unless variation.blank? -%>&email_variation=<%= variation %><% end -%>"/>
@@ -0,0 +1,16 @@
1
+ <% if kiss_metrics_api_key.blank? -%>
2
+ <!-- ERR: Kiss Metrics API key from https://www.kissmetrics.com/settings must be defined in kiss_metrics_key -->
3
+ <% Rails.logger.warn "Missing Kiss Metrics API Key, set api key from https://www.kissmetrics.com/settings via KissMetrics.api_key" -%>
4
+ <% else -%>
5
+ <script type="text/javascript">
6
+ var _kmq = _kmq || [];
7
+ function _kms(u){
8
+ setTimeout(function(){
9
+ var s = document.createElement('script'); var f = document.getElementsByTagName('script')[0]; s.type = 'text/javascript'; s.async = true;
10
+ s.src = u; f.parentNode.insertBefore(s, f);
11
+ }, 1);
12
+ }
13
+ _kms('//i.kissmetrics.com/i.js');_kms('//doug1izaerwt3.cloudfront.net/<%= kiss_metrics_api_key %>.1.js');
14
+ <%= kiss_metrics_flash -%>
15
+ </script>
16
+ <% end -%>
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'lascivious'
2
+ ::ActionView::Base.send(:include, Lascivious)
3
+ ::ActionController::Base.send(:include, Lascivious)
data/lib/lascivious.rb ADDED
@@ -0,0 +1,70 @@
1
+ module Lascivious
2
+
3
+ # API key for Kiss Metrics. Available via https://www.kissmetrics.com/settings
4
+ mattr_accessor :api_key
5
+ @@api_key = ""
6
+
7
+ # For use in config so we can do KissMetrics.setup
8
+ def self.setup
9
+ yield self
10
+ end
11
+
12
+ # The main kiss metrics javascript & stuff
13
+ def kiss_metrics_tag
14
+ render :partial => "kiss_metrics/header"
15
+ end
16
+
17
+ # The email beacon
18
+ def kiss_metrics_email_beacon(email_address, variation, event_type = "Opened Email")
19
+ render :partial => "kiss_metrics/email_beacon", :locals => {
20
+ :event_type => event_type,
21
+ :api_key => kiss_metrics_api_key,
22
+ :email => email_address,
23
+ :variation => variation
24
+ }
25
+ end
26
+
27
+ # Flash for all kiss metrics
28
+ def kiss_metrics_flash
29
+ messages = flash[:kiss_metrics]
30
+ unless messages.blank? || messages.empty?
31
+ return messages.map do |type_hash|
32
+ type_hash.map do |e|
33
+ %Q{_kmq.push(['#{e.first.to_s}', '#{e.last.to_s}']);}
34
+ end
35
+ end.flatten.join("\n")
36
+ end
37
+ return nil
38
+ end
39
+
40
+ # Trigger an event at Kiss (specific: message of event_type 'record', e.g. User Signed Up)
41
+ def kiss_record(value)
42
+ kiss_metric :record, value
43
+ end
44
+
45
+ # Set values (e.g. country: uk)
46
+ def kiss_set(value)
47
+ kiss_metric :set, value
48
+ end
49
+
50
+ # Strong identifier (e.g. user ID)
51
+ def kiss_identify(value)
52
+ kiss_metric :identify, value
53
+ end
54
+
55
+ # Weak identifier (e.g. cookie)
56
+ def kiss_alias(value)
57
+ kiss_metric :alias, value
58
+ end
59
+
60
+ #
61
+ def kiss_metric(event_type, value)
62
+ flash[:kiss_metrics] ||= []
63
+ flash[:kiss_metrics] << { event_type => value }
64
+ end
65
+
66
+ # Get kiss metrics key
67
+ def kiss_metrics_api_key
68
+ return KissMetrics.api_key
69
+ end
70
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,10 @@
1
+ class Lascivious
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ BUILD = 'pre2'
7
+
8
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
9
+ end
10
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'lascivious'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestLascivious < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lascivious
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre2
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Mat Ellis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-14 00:00:00.000000000 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ requirement: &70101459603320 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *70101459603320
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &70101459601560 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70101459601560
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &70101459600260 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.6.4
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70101459600260
48
+ - !ruby/object:Gem::Dependency
49
+ name: rcov
50
+ requirement: &70101459598960 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70101459598960
59
+ description: Easy interface between Rails & Javascript for Kiss Metrics
60
+ email: support@cloudability.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - LICENSE.txt
65
+ - README.rdoc
66
+ files:
67
+ - .document
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.rdoc
72
+ - Rakefile
73
+ - app/views/lascivious/_email_beacon.html.erb
74
+ - app/views/lascivious/_header.html.erb
75
+ - init.rb
76
+ - lib/lascivious.rb
77
+ - lib/version.rb
78
+ - test/helper.rb
79
+ - test/test_lascivious.rb
80
+ has_rdoc: true
81
+ homepage: https://github.com/cloudability/lascivious
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
95
+ - 0
96
+ hash: 191209867734086694
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>'
101
+ - !ruby/object:Gem::Version
102
+ version: 1.3.1
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.6.2
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Easy Kiss Metrics integration for Rails
109
+ test_files: []