Docbkx-tools, DocBook 5 XML, and Google Analytics

Docbkx-tools logo Today I feel dirty for two reasons. I feel dirty on the outside because I rode hard on the bike at lunchtime and we have no showers at the office, yet. I feel dirty on the inside because I found a solution to add Google Analytics code into HTML generated from DocBook 5 XML with docbkx-tools, but the solution is sort of an ugly hack.

Using the maven-antrun-plugin, I added another task for Ant.

<replace dir='${basedir}/target/docbkx/html/' token='&lt;/body&gt;'>
  <include name='**/**/*.html' />
  <replacevalue>&lt;script type="text/javascript"&gt;
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-########-#']);
    (function() {
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

I would put that in a separate .xsl for HTML customizations, but I cannot find an appropriate parameter. I’m not chunking documents, so user.footer.content does not get used.

I tried going the external code file route, but either I get the <script> in the HTML and the FO (PDF, RTF) generation breaks, or I get &lt;script&gt; in the HTML (with parse="text").

Leave me a comment if you have solved this more elegantly.