Google Analytics Page Tracking in Grails Web Flow
Google Analytics makes tracking the metrics of a website easy by capturing the url of each page (gaTrackingOverview documentation). This works great for pages that have unique and meaningful urls, but when it comes to tracking pages that are included as part of a Grails web flow, tracking by the url becomes useless.
Grails uses a flow execution key to track the pages in each step of the flow, passing the key around as a parameter on the request. Within a Grails web flow, the url will look something like this http://hostName/appName/controllerName/flowName?execution=e1s2. The execution key isn’t reliable for tracking purposes, and would be meaningless to the person who will be looking at the Google Analytics report data.
One word of caution here, once you start tracking data from your website, you can never clear that data from your Google Analytics reports. So if you want to get this setup and test this without affecting up your production reports, start by reading Setting Up Google Analytics on localhost.
Here is the code for my tag…
To use the tag, here is the line of code I added to the <head> of the layout.gsp…
Quick notes about the attributes used in my tag code…
- request, grailsApplication, controllerName and actionName are all variables available in the scope of a Grails tag library.
- def appName = grailsApplication.metadata[‘app.name’] //This line of code grabs the metadata assigned to the app.name property in the application.properties file; this value is customizable.
- ProgressData is a class defined in our application where we store the names of the steps in our flow. We use this data to track the users progress through the flow in order to display a progress bar on each page. Since this was already used in our flow, it was easy for me to use this same data to track the pages in Goole Analytics.
Now the Google Analytics report looks much cleaner with the revised urls and it’s easier to analyze the data.
One thought on “Google Analytics Page Tracking in Grails Web Flow”