google_analytics liquid tag
did
committed Feb 02, 2015
commit 3bcc45ac62396057e85ee7a2b7162dd0611ffa3e
Showing 3
changed files with
41 additions
and 23 deletions
locomotive/steam/liquid/tags/google_analytics.rb b/lib/locomotive/steam/liquid/tags/google_analytics.rb
+22
-22
| @@ | @@ -1,40 +1,40 @@ |
| - | module Liquid |
| + | module Locomotive |
| module Steam | |
| - | module Locomotive |
| + | module Liquid |
| module Tags | |
| - | class GoogleAnalytics < ::Liquid::Tag |
| + | class GoogleAnalytics < ::Solid::Tag |
| - | Syntax = /(#{::Liquid::Expression}+)?/ |
| + | tag_name :google_analytics |
| - | def initialize(tag_name, markup, tokens, context) |
| - | if markup =~ Syntax |
| - | @account_id = $1.gsub('\'', '') |
| - | else |
| + | def display(account_id = nil) |
| + | if account_id.blank? |
| raise ::Liquid::SyntaxError.new("Syntax Error in 'google_analytics' - Valid syntax: google_analytics <account_id>") | |
| + | else |
| + | ga_snippet(account_id) |
| end | |
| - | |
| - | super |
| end | |
| - | def render(context) |
| + | private |
| + | |
| + | def ga_snippet(account_id) |
| %{ | |
| - | <script type="text/javascript"> |
| + | <script type="text/javascript"> |
| - | var _gaq = _gaq || []; |
| - | _gaq.push(['_setAccount', '#{@account_id}']); |
| - | _gaq.push(['_trackPageview']); |
| + | var _gaq = _gaq || []; |
| + | _gaq.push(['_setAccount', '#{account_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); |
| - | \})(); |
| + | (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>} |
| + | </script>} |
| end | |
| + | |
| end | |
| - | ::Liquid::Template.register_tag('google_analytics', GoogleAnalytics) |
| end | |
| end | |
| end | |
spec/support/liquid.rb
+3
-1
| @@ | @@ -1,3 +1,5 @@ |
| - | def render_template(template, context) |
| + | def render_template(template, context = nil) |
| + | context ||= ::Liquid::Context.new |
| + | context.exception_handler = ->(e) { true } |
| ::Liquid::Template.parse(template).render(context) | |
| end | |
spec/unit/liquid/tags/google_analytics_spec.rb
+16
-0
| @@ | @@ -0,0 +1,16 @@ |
| + | require 'spec_helper' |
| + | |
| + | describe Locomotive::Steam::Liquid::Tags::GoogleAnalytics do |
| + | |
| + | let(:template) { "{% google_analytics 42 %}" } |
| + | |
| + | subject { render_template(template) } |
| + | |
| + | it { is_expected.to include "_gaq.push(['_setAccount', '42']);" } |
| + | |
| + | describe 'raises an error if the syntax is incorrect' do |
| + | let(:template) { '{% google_analytics %}' } |
| + | it { expect { subject }.to raise_exception }#(::Liquid::SyntaxError) } |
| + | end |
| + | |
| + | end |