Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

Commit

Permalink
Added Chartbeat module
Browse files Browse the repository at this point in the history
  • Loading branch information
nwp authored and jkrall committed Aug 23, 2010
1 parent c5e543a commit c331cfd
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Service implementations include:
* KISSMetrics[http://kissmetrics.com]
* Hubspot[http://hubspot.com]
* CrazyEgg[http://www.crazyegg.com]
* Chartbeat[http://chartbeat.com]

== Usage

Expand All @@ -28,12 +29,13 @@ Then, in your template files, you'll want to add the analytical helper methods f
<!DOCTYPE html>
<html>
<head>
<%= raw analytical.head_prepend_javascript %>
<title>Example</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
<% analytical.identify '5', :email=>'josh@transfs.com' %>
<%= raw analytical.head_javascript %>
<%= raw analytical.head_append_javascript %>
</head>
<body>
<%= raw analytical.body_prepend_javascript %>
Expand Down
4 changes: 3 additions & 1 deletion example/config/analytical.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ adwords:
color: ffffff
label: yyyyyyyyyyyyyyyy
value: 0

chartbeat:
key: chartbeat_12345
domain: your.domain.com
49 changes: 49 additions & 0 deletions lib/analytical/chartbeat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Analytical
module Chartbeat
class Api
include Analytical::Base::Api

def initialize(parent, options={})
super
@tracking_command_location = [:head_prepend, :body_append]
end

def init_javascript(location)
init_location(location) do
case location.to_sym
when :head_prepend
js = <<-HTML
<!-- Analytical Head Init: Chartbeat -->
<script type="text/javascript">var _sf_startpt=(new Date()).getTime();</script>
HTML
js
when :body_append
js = <<-HTML
<!-- Analytical Body Init: Chartbeat -->
<script type="text/javascript">
var _sf_async_config={uid:#{options[:key]}, domain:"#{options[:domain]}"};
(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://s3.amazonaws.com/" : "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>
HTML
js
end
end
end

end
end
end
4 changes: 3 additions & 1 deletion spec/analytical/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Analytical::Console::Api.stub!(:new).and_return(@console = mock('console'))
Analytical::Google::Api.stub!(:new).and_return(@google = mock('google'))
Analytical::Clicky::Api.stub!(:new).and_return(@clicky = mock('clicky'))
Analytical::Chartbeat::Api.stub!(:new).and_return(@chartbeat = mock('chartbeat'))

@api = Analytical::Api.new :modules=>[:console, :google]
end
Expand Down Expand Up @@ -61,10 +62,11 @@

describe 'when accessing a module by name' do
it 'should return the module api object' do
@api = Analytical::Api.new :modules=>[:console, :google, :clicky]
@api = Analytical::Api.new :modules=>[:console, :google, :clicky, :chartbeat]
@api.console.should == @console
@api.clicky.should == @clicky
@api.google.should == @google
@api.chartbeat.should == @chartbeat
end
end

Expand Down
38 changes: 38 additions & 0 deletions spec/analytical/chartbeat_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "Analytical::Chartbeat::Api" do
before(:each) do
@parent = mock('api', :options=>{:chartbeat=>{:key=>1234, :domain =>'test.com'}})
end
describe 'on initialize' do
it 'should set the command_location' do
a = Analytical::Chartbeat::Api.new @parent, {:key=>1234}
a.tracking_command_location.should == [:head_prepend, :body_append]
end
it 'should set the options' do
a = Analytical::Chartbeat::Api.new @parent, {:key=>12345, :domain =>'abcdef.com'}
a.options.should == {:key=>12345, :domain => 'abcdef.com'}
end
end
describe '#track' do
it 'should return the tracking javascript' do
@api = Analytical::Chartbeat::Api.new @parent, {:key=>12345}
@api.track.should == ''
end
end
describe '#identify' do
it 'should return an empty string' do
@api = Analytical::Chartbeat::Api.new @parent, {:key=>12345}
@api.identify('nothing', {:matters=>'at all'}).should == ''
end
end
describe '#init_javascript' do
it 'should return the init javascript' do
@api = Analytical::Chartbeat::Api.new @parent, {:key=>12345, :domain =>'abcdef.com'}
@api.init_javascript(:head_prepend).should =~ /_sf_startpt=\(new.Date\(\)\)\.getTime\(\);/
@api.init_javascript(:head_append).should == ''
@api.init_javascript(:body_prepend).should == ''
@api.init_javascript(:body_append).should =~ /_sf_async_config=\{uid:12345,.*domain:"abcdef\.com"\};/
end
end
end
3 changes: 2 additions & 1 deletion spec/analytical_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def request
DummyForInit.analytical
DummyForInit.analytical_options[:google].should == {:key=>'google_12345'}
DummyForInit.analytical_options[:kiss_metrics].should == {:key=>'kiss_metrics_12345'}
DummyForInit.analytical_options[:clicky].should == {:key=>'clicky_12345'}
DummyForInit.analytical_options[:clicky].should == {:key=>'clicky_12345'}
DummyForInit.analytical_options[:chartbeat].should == {:key=>'chartbeat_12345', :domain => 'your.domain.com'}
end

describe 'in production mode' do
Expand Down
3 changes: 3 additions & 0 deletions spec/config/analytical.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ clicky:
key: clicky_12345
kiss_metrics:
key: kiss_metrics_12345
chartbeat:
key: chartbeat_12345
domain: your.domain.com

0 comments on commit c331cfd

Please sign in to comment.