Thoughts Electrique

Posts Tagged ‘SEO’

Better SEO for external links in OpenCms Navigation

Wednesday, November 26th, 2008

The Problem

If you put an external link into the OpenCms navigation the resulting link will target the external link file which then in turns redirects the surfer to the external page. This is bad if you really want to optimize your navigation for search engines, because search engines won’t see the real link inside the navigation.

The solution

You can use the following JSP snippet to get the real URL from a CmsJspNavElement.

// We assume that the variable "link" holds an CmsJspNavElement and
// "cmsa" holds an CmsJspActionElement.

// Prevent external links from beeing a redirect
// therefore extract the real URL out of the file
String fileUri = link.getResourceName();
CmsResource navResource = cmsa.getCmsObject().readResource(fileUri);
if (navResource.getTypeId() == CmsResourceTypePointer.getStaticTypeId()) {
    CmsFile navFile = CmsFile.upgrade(navResource,cmsa.getCmsObject());
    String externalLink = new String(navFile.getContents());
    externalLink = externalLink.trim();
}

Further reading

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.