This is it. This is why we built the Key-Value SiteOptions Component that we’ve been blogging about to set Application Properties from Administration web forms. Google Analytics Tracking Script!
If you go to the Nixmash Spring Demo Site you’ll see it in the page source.
Of course we’ll be using the SiteOptions Component for more than just the Google Analytics Script, but it was the impetus. We could have easily entered the Tracking Id from a .properties file, but now that we’ve got a sweet Administration Area we might as well use it!
Here’s the new General Settings page with the option to enable Analytics display and enter the Tracking Id.
Now that we’ve got a siteOptions Component with all of our custom settings in it we can make it available by adding the model in our @ControllerAdvice GlobalController. (The appSettings component holds values from an external properties file. I blogged about that here.)
Our Thymeleaf HTML completes the system and demonstrates some interesting things.
- The first line with @thymesVar is an IntelliJ IDEA-specific declaration to add intellisense for our siteOptions component. I have a Live Template that spits these lines out in seconds.
- We display the tracking script only if enabled in getAddGoogleAnalytics().
- We enter the Analytics TrackingId with Thymeleaf Inline Scripting.