How I added a visitor tracker to each static HTML page on my site

require 'dynarex'

httpbase = 'http://www.jamesrobertson.eu/'
localbase = '/home/james/jamesrobertson.eu/'
dx = Dynarex.new httpbase + 'dynarex/main-directory.xml'
a = dx.all.map(&:title)

tracker = '
<script type="text/javascript" src="http://a1.jamesrobertson.eu/do/visitor/tracker" id="visitortracker1446915683"></script>
<noscript><img src="http://a1.jamesrobertson.eu/do/visitor/count-png"/></noscript>'

err_report = []
a.each do |title|

  puts 'title: ' + title.inspect

  dxfile = File.join(localbase, title, '/dynarex.xml')
  next if not File.exists? dxfile
  dx2 = Dynarex.new dxfile

  dx2.all.map(&:url).select{|x| x =~ /\.html$/}.each do |url|

    puts 'url: ' + url.inspect
    relative_url = url[/^https?:\/\/\w+.jamesrobertson.eu\/(.*)/,1]
    next unless relative_url

    page_filepath = localbase + relative_url

    begin
      s = File.read(page_filepath)

      if not s =~ /visitortracker1446915683/ then

        File.write page_filepath, s.sub(/<\/body>.*$/, tracker + "\n" + '\0')
      end
    rescue
      err_report << [url, $!]
    end

  end
end

The above code was executed within an IRB session to add a visitor tracker to most of the static HTML pages on jamesrobertson.eu.