Permalink
Browse files

Add analytics support

  • Loading branch information...
1 parent 74c2927 commit a055d2507151a5f9c1d018d34993b1f08032c2fa Sebastian von Conrad committed Jan 8, 2012
Showing with 31 additions and 0 deletions.
  1. +4 −0 lib/in_bed_with.rb
  2. +13 −0 lib/in_bed_with/analytics.rb
  3. +14 −0 spec/in_bed_with/analytics_spec.rb
View
@@ -12,4 +12,8 @@ module InBedWith
def self.config(&block)
yield self
end
+
+ def self.method_missing(method, *args, &block)
+ "InBedWith::#{method.to_s.camelize}".constantize.new(*args).code
+ end
end
@@ -0,0 +1,13 @@
+module InBedWith
+ class Analytics
+ attr_accessor :property_id
+
+ def initialize(options={})
+ self.property_id = options[:property_id] || InBedWith.analytics_property_id
+ end
+
+ def code
+ "<script type=\"text/javascript\">var _gaq = _gaq || []; _gaq.push(['_setAccount', '#{property_id}']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>"
+ end
+ end
+end
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe InBedWith::Analytics do
+ it 'prints a javascript tag based on argument' do
+ js = InBedWith.analytics(:property_id => 'UA-1234567-1')
+ js.should == "<script type=\"text/javascript\">var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-1234567-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>"
+ end
+
+ it 'prints a javascript tag based on configuration option' do
+ InBedWith.config { |config| config.analytics_property_id = 'UA-7654321-1' }
+ js = InBedWith.analytics
+ js.should == "<script type=\"text/javascript\">var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-7654321-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>"
+ end
+end

0 comments on commit a055d25

Please sign in to comment.