Articles

HACKING HT4TEX TO ADD GOOGLE ANALYTICS TRACKING CODE

In latex on June 29, 2012 by hoxide

Define myconfig.cfg


\Preamble{html}
\begin{document}
\ScriptEnv{vvvv}{}{}
\begin{vvvv}
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'your tracking code']);
_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{vvvv}
% redefine Link such that google can track any links you clicked
\LinkCommand\Link{a,\noexpand\jsref,name}
\def\jsref="#1"{href="#1" onClick="javascript: _gaq.push(['_trackPageview', '#1']);" target="_blank"}
% redefine title\Configure{TITLE}{\HCode{<title>} the titile }{\HCode{</title>}}% jpg with link
\Configure{graphics*}
{jpg}
{\Link[\csname Gin@base\endcsname .jpg]{}{}
\Picture[pict]{\csname Gin@base\endcsname .jpg}
\EndLink
}
\EndPreamble

Now use following command to compile

htlatex file.tex "myconfig,html,word"

Leave a comment