Wednesday, 2 December 2009

New Google Analytics Tracking Code for CQ 5.x

Today's announcement from Google Analytics that they are providing an alternative page tracking snippet to make tracking asynchronous looks good.  Steve Souders provides a good write up of the benefits of their revised approach.

Fortunately, its very easy to upgrade any Day CQ5.x sites that use the previous incarnation of the Google tracking code from  base page component.

There are 3 steps to updating  the existing Google analytics implementation to an asynchronous one
  1. Overlay the analytics.google.jsp in /apps/foundation/components/page/ with an async version
  2. Create a script block to queue Google Analytics commands (trackPageView)
  3. Add that script block to the section of the base template by overriding head.jsp in the overlayed component
More simply; that is the following 3 files added to the /apps/foundation/components/page/ folder on your CQ instance:
This will override the one built into CQ.  If you find that it is not working out for you, then remove these files and you will go back to the original tracking code (make sure you flush any caches here too).

You could always add as an alternative provider for analytics by setting it up as
/libs/foundation/components/page/dialog/items/items/analytics/items/provider/options/google-async
and renaming the files accordingly.  It probably wouldn't hurt to use the provider selection logic in the queue jsp also.  However, it is great to see how easy it is to override out of the box CQ functionality.