Skip to content

Commit

Permalink
New --ga option for generating google analytics info in the layout
Browse files Browse the repository at this point in the history
  • Loading branch information
schof committed Jun 2, 2011
1 parent d3919c3 commit 43223ee
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/guides/cli.rb
Expand Up @@ -23,6 +23,8 @@ def new(name)
method_option "clean", :type => :boolean
method_option "edge", :type => :boolean, :default => false
method_option "production", :type => :boolean, :default => true, :banner => "use production mode", :aliases => "-p"
method_option "ga", :type => :string, :banner => "google analytics id (ex. \"UA-12345-6\")"

def build
if options[:clean]
FileUtils.rm_rf(File.join(Guides.root, options[:production] ? 'output' : 'staging'))
Expand Down
5 changes: 3 additions & 2 deletions lib/guides/generator.rb
Expand Up @@ -59,7 +59,7 @@

module Guides
class Generator
attr_reader :guides_dir, :source_dir, :output_dir, :edge, :warnings, :all
attr_reader :guides_dir, :source_dir, :output_dir, :edge, :ga, :warnings, :all

EXTENSIONS = %w(textile md html.erb)
GUIDES_RE = /\.(?:#{EXTENSIONS.map{|e| Regexp.escape(e)}.join('|')})$/
Expand All @@ -78,6 +78,7 @@ def initialize(options)
@warnings = options[:warnings]
@all = options[:all]
@production = options[:production]
@ga = options[:ga]

@meta = Guides.meta
end
Expand Down Expand Up @@ -139,7 +140,7 @@ def generate_guide(guide, output_file)

puts "Generating #{output_file}"
File.open(File.join(output_dir, output_file), 'w') do |f|
view = ActionView::Base.new(source_dir, :edge => edge, :production => @production)
view = ActionView::Base.new(source_dir, :edge => edge, :ga => ga, :production => @production)
view.extend(Helpers)

if guide =~ /\.html\.erb$/
Expand Down
15 changes: 15 additions & 0 deletions lib/guides/templates/source/layout.html.erb
Expand Up @@ -89,5 +89,20 @@
<script type="text/javascript">
SyntaxHighlighter.all()
</script>
<% if @ga %>
<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<%= @ga %>']);
_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 %>
</body>
</html>

0 comments on commit 43223ee

Please sign in to comment.