rack-chartbeat

Rack middleware to automatically include Chartbeat embed codes.

Example

require "rack/chartbeat"

use Rack::Chartbeat, uid: 12345, domain: "example.com"

Including this in the config.ru file of your Rack application will automatically inject the corresponding JavaScript into the <head> and <body> of your HTML, respectively:

<script type="text/javascript">var _sf_startpt=(new Date()).getTime()</script>
<script type="text/javascript">
var _sf_async_config={uid:12345,domain:"example.com"};
(function(){
  function loadChartbeat() {
    window._sf_endpt=(new Date()).getTime();
    var e = document.createElement('script');
    e.setAttribute('language', 'javascript');
    e.setAttribute('type', 'text/javascript');
    e.setAttribute('src',
       (("https:" == document.location.protocol) ? "https://a248.e.akamai.net/chartbeat.download.akamai.com/102508/" : "http://static.chartbeat.com/") +
       "js/chartbeat.js");
    document.body.appendChild(e);
  }
  var oldonload = window.onload;
  window.onload = (typeof window.onload != 'function') ?
     loadChartbeat : function() { oldonload(); loadChartbeat(); };
})();
</script>

License

rack-chartbeat is available under the MIT license. See the LICENSE file for more info.