Permalink
Browse files

Add usertime

  • Loading branch information...
1 parent 742e819 commit 752f8832267401d6a52c01199e54ae9540c4c1ea @wfarr wfarr committed Jul 16, 2010
View
@@ -8,5 +8,6 @@ vendor/rails
config/deploy/production.rb
config/deploy/staging.rb
config/initializers/custom.rb
+config/initializers/usertime_config.rb
config/moonshine/production.yml
config/moonshine/staging.yml
View
@@ -30,6 +30,12 @@ To deploy, you need to create the following files (we provide also the default t
set :rails_env, :staging
server 'staging.example.com', :app, :web, :db, :primary => true
+### config/initializers/usertime_config.rb
+
+ Stop::UserTime.configure do |config|
+ config.key = "sekrit"
+ end
+
### config/initializers/custom.rb
TT_HOST = 'example.com'
@@ -2,24 +2,26 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title>XTT<%= @project ? " - #{h(@project.name)}" : '' %></title>
- <%= javascript_include_tag 'prototype', 'cookie', 'time', :cache => 'main' %>
- <%= javascript_include_tag 'showcase', 'control.tabs.js', 'ipselect.js', :cache => 'components' %>
- <%= javascript_include_tag 'application' %>
+ <%- usertime do %>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+ <title>XTT<%= @project ? " - #{h(@project.name)}" : '' %></title>
+ <%= javascript_include_tag 'prototype', 'cookie', 'time', :cache => 'main' %>
+ <%= javascript_include_tag 'showcase', 'control.tabs.js', 'ipselect.js', :cache => 'components' %>
+ <%= javascript_include_tag 'application' %>
- <%= yield :head %>
- <script type="text/javascript" charset="utf-8">
- document.observe('dom:loaded', function() {
- <%= yield :dom_loaded %>
- })
- </script>
- <%= stylesheet_link_tag 'screen' %>
- <%= stylesheet_link_tag 'showcase', 'ipselect' %>
- <!--[if lt IE 7]>
- <%= stylesheet_link_tag 'fail' %>
- <%= javascript_include_tag 'morefail' %>
- <![endif]-->
+ <%= yield :head %>
+ <script type="text/javascript" charset="utf-8">
+ document.observe('dom:loaded', function() {
+ <%= yield :dom_loaded %>
+ })
+ </script>
+ <%= stylesheet_link_tag 'screen' %>
+ <%= stylesheet_link_tag 'showcase', 'ipselect' %>
+ <!--[if lt IE 7]>
+ <%= stylesheet_link_tag 'fail' %>
+ <%= javascript_include_tag 'morefail' %>
+ <![endif]-->
+ <%- end %>
</head>
<body>
<!-- header -->
@@ -0,0 +1,20 @@
+Copyright (c) 2010 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
+# Stop::UserTime
+
+[Stop... UserTime!](http://dancejam.com/videos/1054584095-mc-hammer-cant-touch-this-asian-sty)
+
+## Usage
+
+There are three steps to using Stop::UserTime.
+
+ * install the plugin
+ * configure with your key
+ * update layout with the usertime helper
+
+
+### Install
+
+ script/plugin install git@github.com:railsmachine/stop-usertime.git
+
+
+### Configuration
+
+Next, you need to add an initialize for your app's settings, perhaps in config/initializers/usertime_config.rb
+
+ Stop::UserTime.configure do |config|
+ config.key = "redacted"
+ end
+
+Stop::UserTime only will enable UserTime reporting for production by default. If you need others, it's easy to add:
+
+ Stop::UserTime.configure do |config|
+ config.key = "redacted"
+ config.environments = %w(staging production)
+ end
+
+
+Stop::UserTime will send along information about the current controller and action, defaulting to ControllerClass#action_name. You can override this with a lamda:
+
+
+ Stop::UserTime.configure do |config|
+ config.key = "redacted"
+ config.label_generator do |view|
+ "Oh oh oh... oh oh... stop... #{view.controller.class}##{view.controller.action_name}"
+ end
+ end
+
+If you need even finer control of this, you can just override the `usertime_label` method to pull to do whatever logic you might need to do.
+
+ module ApplicationHelper
+ def usertime_label
+ # this code hidden to protect the innocent
+ end
+ end
+
+### Layout
+
+UserTime works by adding extra JavaScrip at the beginning and end of your &lt;head&gt;. The `usertime` can handle inserting this, and making sure it has the correct key and label.
+
+
+ <html>
+ <head>
+ <%- usertime do %>
+ <title>My awesome site</title>
+ <%= javascript_include_tag :defaults %>
+ <% end %>
+ </head>
+ <body>
+ <%= yield
+ </body>
+ </html>
+
+## Credit
+
+Copyright (c) 2010 Josh Nichols, released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the usertime_rails plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the usertime_rails plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Usertime-rails'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,5 @@
+require 'stop-user_time'
+
+ActionView::Base.class_eval do
+ include Stop::UserTime::ViewHelpers
+end
@@ -0,0 +1,78 @@
+module Stop
+ module UserTime
+ module ViewHelpers
+ # To properly work, UserTime requires you to have some extra JavaScript bits in your <head>.
+ #
+ # +usertime+ is a helper to help make sure everything is in place. Call it in your view, and pass it everything that'd normally have in your head. It will handle putting the start and end javascript pieces in place.
+ #
+ # Here's an example:
+ #
+ # <html>
+ # <head>
+ # <%- usertime do %>
+ # <title>My awesome site</title>
+ # <%= javascript_include_tag :defaults %>
+ # <% end %>
+ # </head>
+ # <body>
+ # <%= yield
+ # </body>
+ # </html>
+ #
+ # To properly work, you'll also need to properly +configure+ Stop::UserTime with your API +key+.
+ def usertime(&block)
+ key = Configuration.instance.key # TODO raise or something
+ if Configuration.instance.enabled?
+ concat %Q{
+ <script type="text/javascript">UT={"firstbyte":Number(new Date()), "key":"#{key}", "label":"#{usertime_label}"}</script>
+ <script type="text/javascript" src="http://cdn.usertimeapp.com/ut.js"></script>
+ }
+ end
+
+ concat capture(&block)
+
+ if Configuration.instance.enabled?
+ concat %Q{
+ <script type="text/javascript">UT.headDone()</script>
+ }
+ end
+ end
+
+ # Helper for calculate the label of this page reported to UserTime. This defaults to ControllerClass#action_name. You can +configure+ +label_generator+ with a lambda that takes an ActionView::Base, or just override this outright.
+ def usertime_label
+ Configuration.instance.label_generator.call(self)
+ end
+ end
+
+ # Configure Stop::UserTime as is your want.
+ def self.configure
+ yield Configuration.instance if block_given?
+ Configuration.instance
+ end
+
+ class Configuration
+ include Singleton
+
+ # UserTime access key. See login to http://usertimeapp.com/home then click 'setup' for your application to find the value to enter here.
+ attr_accessor :key
+
+ # Environments which UserTime will be tracking. Defaults to just production.
+ attr_accessor :environments
+
+ # A lambda used to generate the 'label' UserTime will report for each page. This will be passed the ActionView::Base instance currently being rendered
+ attr_accessor :label_generator
+
+ def environments
+ @environments ||= ['production']
+ end
+
+ def label_generator
+ @label_generator ||= lambda {|view| "#{view.controller.class}##{view.controller.action_name}"}
+ end
+
+ def enabled?
+ environments.include?(Rails.env)
+ end
+ end
+ end
+end

0 comments on commit 752f883

Please sign in to comment.