| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This plugin simplifies the use of KISSmetrics with Rails.
KISSmetrics 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.
Using a flash mechanism this plugin provides a series of helper functions to allow your Controller to inject the correct Javascript into any page.
Lascivious.setup do |config|
config.api_key = "0000000000000000000000000000000000000000"
end<title><%= title %></title>
<%= csrf_meta_tag %>
<link rel="image_src" href="/images/facebook-icon.png"/>
<%= kiss_metrics_tag %>class SomeController < ApplicationController
def index
kiss_record "SomeController loaded"
end
endCurrently the following commands are provided:
Our service is built on Rails 3 with Devise and Inherited Resources. This is how we integrated KISSmetricss into our app.
In all cases add this to the HEAD of your layouts:
<%= kiss_metrics_tag %>And define your keys via an initializer in app/config/initializers/kiss_metrics.rb like this:
Lascivious.setup do |config|
if Rails.env == 'production'
config.api_key = ENV['KISS_METRICS_API_KEY']
else
# Development/testing/staging account key...
config.api_key = "1111111111111111111111111111111111111111"
end
endWe 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:
class Users::SessionsController < Devise::SessionsController
def create
warden_opts = { :scope => resource_name, :recall => "#{controller_path}#new" }
resource = warden.authenticate(warden_opts)
if(resource.nil?)
kind = :invalid
resource = build_resource
resource.errors[:base] = I18n.t("#{resource_name}.#{kind}", {
scope: "devise.failure",
default: [kind],
resource_name: resource_name
})
else
kiss_identify resource.email
kiss_record "Signed In"
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
end
respond_with resource, :location => redirect_location(resource_name, resource)
end
endA much simpler version would be an after_sign_in_path_for override in app/controllers/application_controller.rb:
private
def after_sign_in_path_for(resource_or_scope)
kiss_identify resource_or_scope.email unless resource_or_scope.email.nil?
kiss_record "Signed In"
scope = Devise::Mapping.find_scope!(resource_or_scope)
home_path = "#{scope}_root_path"
respond_to?(home_path, true) ? send(home_path) : root_path
endWe added this to app/controllers/application_controller.rb:
private
### Record a sign out
def after_sign_out_path_for(resource_or_scope)
kiss_record "Signed Out"
new_user_session_path
endThe 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.
Our Mailers typically look like this:
def send_mail(user, recipient, bill_period, subject)
@recipient ||= user
@user = user
@bill_period = bill_period
@data = collate(@user, @bill_period)
mail({
to: formatted_address(@user, @recipient),
subject: subject
})
endThe @recipient variable is important to us, it allows us to send an email even if we don't have a user setup.
Now inside the email partial we do:
... our email ERB template ...
<%= kiss_metrics_email_beacon @recipient.email, "Summary" %>
</body>
</html>Points to note here:
You get this for free on every page where you have included the kiss_metrics_tag included in your layout.
See the kiss_identify tag 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.
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:
class InvitesController < InheritedResources::Base
respond_to :html
actions :new, :create
def new
new! do
kiss_record "Activated"
end
end
def create
create! do |success, failure|
success.html do
kiss_record "Signed Up"
sign_in(@invite.user)
kiss_identify current_user.email
redirect_to first_page_in_your_post_sign_up_path
end
end
end
endBeyond this you're on your own here, sorry.
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 above for a template.
You can add other events to your app by simply stating:
kiss_record "Some Other Event"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 KISSmetricss really easily, with no setup required on the KM side.
See the API section for more details on what tools you have for doing this.
Copyright (c) 2011-2012 Cloudability Inc.
See LICENSE for further details.
| Back | FazBrowse Home | New Git URL |