WorldTurner Blog
Google Analytics, Wicket and Panels
As I mentioned in a previous post, it is possible to use Google Analytics in a page even if you switch components (or panels) instead of bookmarkable pages.
Whether this provides what you need, depends on your use-case. Temporary wicket URLs don't go well with the indexer algorithm of Google Search, I've been told. I can't solve that (yet). But Google Analytics is something else.
If you have a page with one main panel that you switch around (using the methods replace, replaceWith, remove/add, etc.), you can tell Google Analytics the class of that panel instead of the URL of the http request (or anything else about that panel that is interesting to you).
First a bit of code:
<?xml version="1.0" encoding="UTF-8"?> _gaq.push(['_setAccount', '<wicket:container wicket:id="accountName"></wicket:container>']); })(); |
File: GoogleAnalyticsScriptPanel.html |
This is the basic script for the (new) asynchronous Google Analytics interface. This is the backing HTML for the wicket panel GoogleAnalyticsScriptPanel.java. There are two placeholders that the panel will fill in: the account name and the tracking url.
Google Analytics doesn't really care what you put in the tracking url. If you want some kind of hierarchy, you need to use slashes in the url, but apart from that, you can put pretty much anything in it. It certainly doesn't have to look like a URL. So a java class name is fine. And in this case, I'm going to put the wicket panel class name in it.
You can download the source code of GoogleAnalyticsScriptPanel.java here. I won't put it in the blog entry as it isn't very interesting by itself, it just works.
Now the interesting part comes from an implementation of the interface IModel: ComponentClassNameModel. I've created a model that looks up the class name of component within a container.
So say that you use a page with several components: a component with the id "menu" on the left, one with "links" on the right, "central" in the middle and "messages" at the bottom. These components are switched from time to time depending on what the user is doing; the central component might at one time be a "MainArticlePanel" and a "SignupForOfferPanel" at another time. Likewise, you have different kinds of link panels, menus, etc.
When you have multiple components, you should decide what you want to log in Google Analytics. What is going to tell you most about the behavior of the user. I actually haven't tried to see what happens when you log more than one page view to Google Analytics for a single actual page-view - I think this will screw up your statistics.
Besides logging page views to Google Analytics, you can also log events. I haven't delved too deeply into that, but that could be useful for logging user-clicks that trigger panel changes, at least if that tells you something about user behavior for your website.
In this case, you probably are most interested in the "central" panel, as the rest is more about navigation and informing the user. To do this, take the markup HTML of your page and add this as the first thing after the <body> element:
<script type="text/javascript" wicket:id="googleAnalyticsScript">
</script>
Then,
in the Java code of your page, add this:
private String googleAnalyticsAccountName = "UA-XXXXX-X";
// ... inside the code that initializes your GUI:
IModel<String> trackingUrlModel = new ComponentClassNameModel(this, "central");
add(new GoogleAnalyticsScriptPanel("googleAnalyticsScript", trackingUrlModel, new Model<String>(
googleAnalyticsAccountName)));
That's all, from now on Google Analytics will see the class name of the "central" component instead of the URL of the http request.
If you want to do some formatting on the class name, there are two options:
- change my source code for ComponentClassNameModel. Perhaps you want to remove a portion of the package name before logging to Google Analytics.
- Wrap the ComponentClassNameModel inside another model that does formatting. For that you can use the standard wicket model StringResourceModel. You can pass the ComponentClassNameModel as a parameter to the resource key, and embed the class name inside a larger string.
I hope that this is an inspiration for people who need to work with wicket and Google Analytics.
Download
a .tar.gz file with all three files mentioned in this blog post.
Posted at 03:16PM Aug 20, 2010 by Erwin Bolwidt in Wicket | Comments[1]
Posted by CmaJ on December 27, 2010 at 11:01 AM CET #