Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

GoogleAnalytics

Summary: Add Google Analytics tracking code to wiki pages
Version: 2007-03-21
Prerequisites: PmWiki 2.1.0
Status: Stable
Maintainer: Pm
Categories: Administration

Questions answered by this recipe

  • How can I add Google Analytics tracking code to my site?

Description

Adding the tracking code for Google Analytics is easy -- simply add the following into any local or per-group customization file, replacing the UA-CODE-1 with your Google Analytics tracking code:

$HTMLHeaderFmt['analytics'] = "
  <script src='http://www.google-analytics.com/urchin.js'
    type='text/javascript'></script>
  <script type='text/javascript'>
    _uacct = 'UA-CODE-1';

  </script>";

Other Method

You could also just copy the code into the .tmpl file of your standard skin.

Notes

It's also possible to add the above <script> lines directly into a skin template file, but a site should only do one or the other.

Comments

Thanks for the recipe. I guess this would be helpful to solve 00767. But sometimes the script needs to be inserted at the end, preferably.
To have the analytics code at the end of the page instead of the beginning, use $HTMLFooterFmt instead of $HTMLHeaderFmt. -- Pm
Trouble is you need a way to automatically make external links (and links to attachments) include a short javascript function call, with a meaningful name. If such a link is present, then the code above goes in the body (anywhere before the link)... if not then it must go in the header. The individual link code can probably go into $UrlLinkFmt or $IMapLinkFmt's, but I have no idea how to turn the link title/url into a useful name or how to move the code from header to body automatically if such a link is present. Here's an example of an outbound link, from Googles help system:
<a href="http://www.example.com" onClick="javascript:urchinTracker('/outgoing/example_com');">
-- pjv
See this post on the mailing list. --Petko February 18, 2008, at 05:59 PM
Since Décember 2007, the code has changed, some quotes must be escaped
$HTMLFooterFmt['google'] = '<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">
var pageTracker = _gat._getTracker("UA-CODE-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>';
-- Thomas

See Also

Contributors

Edit - History - Print - Recent Changes - Search
Page last modified on February 18, 2008, at 06:01 PM