Pages

Thursday, 13 September 2012

Google Analytics in Liferay

Liferay Google Analytics

Configuring Liferay to work with Google Analytics is very simple.
To enable Google Analytics support, go to the Manage Pages screen for the community for which you want to enable support. You can do this through the Control Panel by going to either the Organizations or Communities link in the Portal section, and then clicking Actions → Manage Pages for the community or organization you want to analyze. Click the Settings tab. Click the Monitoring Tab. Put your Google Analytics ID (which should have been provided to you when you signed up for the service) in the field and click Save. All of the pages in the community you selected will now have the Google Analytics code in them and will be tracked. 

The latest code from Google Analytics looks like this:
<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">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();atch(err) {}
</script>
Slightly older code will look like this:
<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-xxxxxx-x");
pageTracker._trackPageview();
</script>
Here's the even-older code (Note from Google):
<script type="text/javascript" src=" http://www.google-analytics.com/urchin.js "></script>
<script type="text/javascript">
_uacct = "UA-xxxxxx-x"
urchinTracker();
</script>
Where xxxxxx-x is your id, which you can find in the Google Analytics setup pages.

There are three places in Liferay where you can place the Google Analytics code.

Portal Wide (One site wide domain) #

The typical scenario is that you want to use a single Google Analytics account to track an entire portal deployment.
You can add the code to the following file:
/html/common/themes/bottom-ext.jsp
For jsp themes, make sure /html/themes/[theme_name]/templates/portal_normal.jsp contains the following line:
<liferay-util:include page="/html/common/themes/bottom-ext.jsp" />
OR if you're using a Velocity theme, make sure /html/themes/[theme_name]/templates/portal_normal.vm contains the following line:
$taglibLiferay.include("/html/common/themes/bottom-ext.jsp")
Therefore, you can add the Google Analytics code in /html/common/themes/bottom-ext.jsp to make Google Analytics configuration global for your portal.
You'll find this file in the Liferay war. If your portal is running in the root context of apache-tomcat-5.5.x, then the path is:
${CATALINA_HOME}/webapps/ROOT/html/common/themes/bottom-ext.jsp

In Manage Pages (The GUI method) #

  1. From the Communities portlet or the Enterprise Admin portlet, select Manages Pages for a community or organization.
  2. Select the Settings tab.
  3. Select the Monitoring tab.
  4. Enter your Google Analytics ID.

In Themes (Virtual Hosting?) #

"Why would I want to place the Analytics code in a theme?", you ask. Well, If you're going to use virtual hosting and want to have different Google Analytics accounts for each domain, you could design a different theme for each Community which hosts a domain. Place the appropriate Google Analytics code for a given domain in the appropriate theme. Do this for each Community hosting a domain, and have them tracked individually.
In any theme, place the above code just before the closing html or body tag. These you'll find in the following file /html/themes/[theme_name]/templates/portal_normal.[jsp|vm]
e.g.
[SNIP/]
...
    <%@ include file="bottom.jsp" %>

<liferay-util:include page="/html/common/themes/bottom-ext.jsp" />
<liferay-util:include page="/html/common/themes/session_timeout.jsp" />
<liferay-util:include page="/html/common/themes/sound_alerts.jsp" />

<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">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();atch(err) {}
</script>

</body>

</html>

PS: The liferay site has it in /html/common/themes/bottom-ext.jsp so that it applies site wide, regardless of theme.

PSS: SiteScan will scan your site and check if every page has the javascript correct. 
thanks to Himanshu Jain 

1 comment:

  1. Have any idea how to integrate webtrends analytics instead of google analytics in liferay?

    ReplyDelete