Skip to content

Commit

Permalink
Add Google Analytics javascript to Mocha docs.
Browse files Browse the repository at this point in the history
Customising YARD templates is a bit obscure and this was the best I
could come up with. The javascript appears as the last section within
the content div, but before the footer div. Not ideal, but I think it
should be ok.

The tracking javascript is only displayed if the
`GOOGLE_ANALYTICS_WEB_PROPERTY_ID` environment variable is set.
  • Loading branch information
floehopper committed Jan 8, 2013
1 parent b16ed30 commit e683d2a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions .yardopts
@@ -1,3 +1,4 @@
--template-path yard-templates
--no-private
lib/mocha/api.rb
lib/mocha/hooks.rb
Expand Down
8 changes: 7 additions & 1 deletion Rakefile
Expand Up @@ -113,8 +113,14 @@ unless ENV["MOCHA_NO_DOCS"]
`rm -rf ./doc`
end

task 'docs_environment' do
unless ENV['GOOGLE_ANALYTICS_WEB_PROPERTY_ID']
puts "\nWarning: GOOGLE_ANALYTICS_WEB_PROPERTY_ID was not defined\n\n"
end
end

desc 'Generate documentation'
YARD::Rake::YardocTask.new('yardoc') do |task|
YARD::Rake::YardocTask.new('yardoc' => 'docs_environment') do |task|
task.options = ["--title", "Mocha #{Mocha::VERSION}"]
end

Expand Down
11 changes: 11 additions & 0 deletions yard-templates/default/layout/html/google_analytics.erb
@@ -0,0 +1,11 @@
<script src="/javascripts/app.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("<%= ENV['GOOGLE_ANALYTICS_WEB_PROPERTY_ID'] %>");
pageTracker._trackPageview();
} catch(err) {}</script>
6 changes: 6 additions & 0 deletions yard-templates/default/layout/html/setup.rb
@@ -0,0 +1,6 @@
def init
super
if ENV['GOOGLE_ANALYTICS_WEB_PROPERTY_ID']
sections[:layout] << :google_analytics
end
end

0 comments on commit e683d2a

Please sign in to comment.