Loading...

March 22, 2009

Track Grails form remote submit in Google Analytics

Tracking AJAX requests in Google Analytics is easy as described in the Google help center. When we use the Grails formRemote taglib we can track a form submit in Google Analytics. We must execute the Javascript code pageTracker._trackPageview('/formsubmitted') and the "page" formsubmitted will show up in the Google Analytics reports.

The following snippet shows how we can use the onSuccess attribute to execute this code:

<g:formRemote name="formname" method="GET"
    url="[action:'handleSubmit']"
    onSuccess="if (pageTracker) pageTracker._trackPageView('/reservation');">
...
form fields here
...
</g:formRemote>