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

Commit

Permalink
Channel events through flash instead of rendering them to the page.
Browse files Browse the repository at this point in the history
All methods except for events are now ignored.
  • Loading branch information
Duarte Henriques committed Oct 8, 2012
1 parent c2e5ad1 commit 2842dfc
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 57 deletions.
9 changes: 4 additions & 5 deletions app/views/application/_analytical_javascript.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<script type="text/javascript">
var Analytical = {
track: function(page) {
<%= raw analytical.now.track('__PAGE__').gsub(/"__PAGE__"/,'page') %>
},
event: function(name, data) {
if (typeof data === 'undefined') { data = {}; }
<%= raw analytical.now.event('__EVENT__', {}).gsub(/"__EVENT__"/,'name').gsub(/"?\{\}"?/,'data') %>
if (typeof data === 'undefined') {
data = {};
}
<%= raw analytical.now.event('__EVENT__', {}).gsub(/"__EVENT__"/, 'name').gsub(/"?\{\}"?/, 'data') %>
}
};
</script>
4 changes: 4 additions & 0 deletions lib/analytical.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module Analytical
def analytical(options = {})
config = Analytical.config(options[:config])
self.analytical_options = options.reverse_merge(config)

after_filter do |controller|
flash[:analytical] = controller.analytical.flash.to_json.html_safe
end
end

def self.config(path = nil)
Expand Down
26 changes: 8 additions & 18 deletions lib/analytical/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def initialize(options={})
@dummy_module = Analytical::Modules::DummyModule.new
end

def flash
@modules.values.map{ |m| m.events_options }.flatten(1)
end

def get_mod(name)
name = name.to_s.camelize
"Analytical::Modules::#{name}".constantize
Expand Down Expand Up @@ -70,13 +74,12 @@ def now
# These methods return the javascript that should be inserted into each section of your layout
#
def head_prepend_javascript
[init_javascript(:head_prepend), tracking_javascript(:head_prepend)].delete_if{|s| s.blank?}.join("\n")
[init_javascript(:head_prepend)].delete_if{|s| s.blank?}.join("\n")
end

def head_append_javascript
js = [
init_javascript(:head_append),
tracking_javascript(:head_append),
init_javascript(:head_append)
]

if Gem::Version.new(::Rails::VERSION::STRING) >= Gem::Version.new('3.1.0') # Rails 3.1 lets us override views in engines
Expand All @@ -92,10 +95,10 @@ def head_append_javascript
alias_method :head_javascript, :head_append_javascript

def body_prepend_javascript
[init_javascript(:body_prepend), tracking_javascript(:body_prepend)].delete_if{|s| s.blank?}.join("\n")
[init_javascript(:body_prepend)].delete_if{|s| s.blank?}.join("\n")
end
def body_append_javascript
[init_javascript(:body_append), tracking_javascript(:body_append)].delete_if{|s| s.blank?}.join("\n")
[init_javascript(:body_append)].delete_if{|s| s.blank?}.join("\n")
end

private
Expand All @@ -106,19 +109,6 @@ def process_command(command, *args)
end
end

def tracking_javascript(location)
commands = []
@modules.each do |name, m|
commands += m.process_queued_commands if m.init_location?(location) || m.initialized
end
commands = commands.delete_if{|c| c.blank? || c.empty?}
unless commands.empty?
commands.unshift "<script type='text/javascript'>"
commands << "</script>"
end
commands.join("\n")
end

def init_javascript(location)
@modules.values.collect do |m|
m.init_javascript(location) if m.respond_to?(:init_javascript)
Expand Down
10 changes: 5 additions & 5 deletions lib/analytical/modules/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ def queue(*args)
@command_store << args
end
end
def process_queued_commands
command_strings = @command_store.collect do |c|
send(*c) if respond_to?(c.first)
end.compact

# we only care about events, all other commands will be removed without having effect
def events_options
result = @command_store.commands.select{ |command| command[0] == :event }.map{ |command| [command[1], command[2]] }
@command_store.flush
command_strings
result
end

def init_location?(location)
Expand Down
4 changes: 0 additions & 4 deletions spec/analytical/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@
before(:each) do
@console.stub!(:init_location?).and_return(false)
@console.stub!(:initialized).and_return(false)
@console.stub!(:process_queued_commands).and_return([])
@google.stub!(:init_location?).and_return(false)
@google.stub!(:initialized).and_return(false)
@google.stub!(:process_queued_commands).and_return([])
end

describe '#head_prepend_javascript' do
Expand Down Expand Up @@ -125,12 +123,10 @@
@console.stub!(:initialized).and_return(false)
@console.stub!(:track).and_return('console track called')
@console.stub!(:queue)
@console.stub!(:process_queued_commands).and_return(['console track called'])
@google.stub!(:init_location?).and_return(false)
@google.stub!(:initialized).and_return(true)
@google.stub!(:track).and_return('google track called')
@google.stub!(:queue)
@google.stub!(:process_queued_commands).and_return(['google track called'])
@api.track('something', {:a=>1, :b=>2})
end
describe '#body_prepend_javascript' do
Expand Down
25 changes: 0 additions & 25 deletions spec/analytical/modules/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,6 @@ class BaseApiDummy
end
end

describe '#process_queued_commands' do
before(:each) do
@api = BaseApiDummy.new(:parent=>mock('parent'))
@api.command_store.commands = [[:a, 1, 2, 3], [:b, {:some=>:args}]]
@api.stub!(:a).and_return('a')
@api.stub!(:b).and_return('b')
end
it 'should send each of the args arrays in the command list' do
@api.should_receive(:a).with(1, 2, 3).and_return('a')
@api.should_receive(:b).with({:some=>:args}).and_return('b')
@api.process_queued_commands
end
it 'should return the results as an array' do
@api.process_queued_commands.should == ['a', 'b']
end
it 'should clear the commands list' do
@api.process_queued_commands
@api.command_store.commands == []
end
it "should not store an unrecognized command" do
@api.command_store.commands << [:c, 1]
@api.process_queued_commands.should == ['a','b']
end
end

describe '#init_location?' do
before(:each) do
@api = BaseApiDummy.new(:parent=>mock('parent'))
Expand Down

0 comments on commit 2842dfc

Please sign in to comment.