Blogs

Blogs

Tip of the Day - Enhance Your Analytics With Hooks

Liferay allows using site specific Google Analytics tracking codes to analyze the traffic inside your portal. Adding the code is simply a matter of going to the site settings and saving the code you need.

Insert your google analytics id in site settings

However, the default version of Liferay's Google Analytics code might not be enough in some cases, as it offers only the basic analytics features. While they are enough for some cases, Google is also offering you their own official plugins and also a chance to build plugins of your own. More information about plugins in Google Analytics can be found here.

Original analytics code in Liferay 6.2 EE SP13 as seen in HTML

Now that you've realised that the default Liferay way of adding the analytics code to your site is not enough, you want to enable a ga-plugin. What can you do? You could add the script itself directly to your theme, but it might not be the best option if you want your portal to be maintainable. Creating a custom portlet to do the job is a possibility as well, you could embedd it to your theme or whatever. The way we've done this at Ambientia is that we've created a google analytics hook, which overrides the default way how Liferay creates the tracking code.

Now depending on your portal version, you need to do a bit of searching to find the place where the analytics script is added to your portal. In Liferay 6.2 the code resides in a file called top_js.jspf which can be found from "html/common/themes" folder. So to enable the custom analytics plugins needed, you'll just override the file with a hook to add the plugins you want to include. Also if you have a very specific need to enable ga plugins on a site or two, you might be able to create the hook as an application adapter and enable it only on the sites you need.

Analytics plugin "displayfeatures" added via hook

There you have it, enhancing your analytics with a hook is a simple task.