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-########-#']);
    _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);
    })();
  &lt;/script&gt;&lt;/body&gt;</replacevalue>
</replace>

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.

Advertisements

2 Comments

Filed under Docs, Tools

2 responses to “Docbkx-tools, DocBook 5 XML, and Google Analytics

  1. Thanks to Peter Desjardins and David Cramer on the docbook-apps list. I should have read Inserting external HTML code more carefully from the start, and am now using the dbhtml-include processing instruction.

  2. The more I look at it, the more I like the idea of inserting analytics per HTML file, rather than per document. If legalnotice.html is getting more traffic than the release notes, that would actually be something worth knowing.

    Perhaps it would be a nice feature independently of the existing features.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s