Thoughts Electrique

Posts Tagged ‘Websites’

Google Analytics with OpenCms

Wednesday, November 19th, 2008

Avoid the wrong track

For a while I have been using Google Analytics for my website statistics (additionally to AWStats) and I really like the reports (Note: Yahoo! now has a competing product out there called “Yahoo! Web Analytics” which I might check out in the future). I’ve also installed various tracking software (like etracker - which is a German service) on different CMS solutions at customer sites.

Nearly all services offer some copy&paste code to throw into your HTML/JSP/PHP/Whatever, but if you’re using a CMS system things might be a bit more complicated. In most CMS systems you have two different kinds of traffic: Visitor generated and editor generated. Editors browse the site while checking their changes or proofreading text in the offline area (unpublished content). You most likely don’t want to have this traffic screw up your statistics, so it makes sense to filter out the traffic. Mostly a simple if(…)-clause is all that’s needed to achieve this. Here is my solution for OpenCms:

<%@ page session="false" %>
<%@ page import="org.opencms.jsp.*,org.opencms.file.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  CmsJspActionElement cmsa = new CmsJspActionElement(pageContext,request,response);
  CmsUser currentUser = cmsa.getRequestContext().currentUser();
  boolean displayAnalytics = currentUser.isGuestUser() || currentUser.isWebuser();
  pageContext.setAttribute("displayAnalytics",new Boolean(displayAnalytics));
%>
<c:if test="${displayAnalytics}">
  <!-- Your Google Analytics JavaScript goes here -->
</c:if>

Of course you can easily create a JSP from this and include it in your template.