I built a little open source side project: Google Analytics Handler, which reports tracking information to Google Analytics on the server side.
It's for my new podcast, All Things Git, so that we can track the RSS and audio downloads from the podcast's website.
If this was 1997 and I was hosting the website on my own server, I could just crunch my Apache logs for the data. But it's not; it's 2017 so of course I'm hosting the podcast in the cloud. The website is an Azure Web App and the audio downloads are hosted in Azure CDN.
And it's nearly perfect! Performance? Amazing! Cost? Low! But log files? Not so much.
Martin suggested a hosted analytics platform to track RSS requests, and another to track audio downloads. But that's two new bits of analytics to go along with Google Analytics, which we use to track the page visits. Requests being tracked in three distinct places? Ugh.
Thankfully, Google Analytics offers the Measurement Protocol API - which lets you report events like page views manually. So I built an ASP handler to report requests on RSS and audio downloads to Google Analytics.
For RSS requests, the handler simply opens the RSS file that exists on disk
and returns it to the client before reporting to Google Analytics.
This is nice because it's a transparent change - the handler is loaded
by the web configuration only in production. It's just a few lines in
<configuration> <system.webServer> <handlers> <add name="RssHandler" verb="*" path="rss.xml" type="GoogleAnalyticsHandler.GoogleAnalyticsHandler, GoogleAnalyticsHandler" resourceType="Unspecified" /> </handlers> </system.webServer> </configuration>
For audio, we don't host the audio directly on the web site, so for the
actual podcast itself, the handler redirects to the audio files in Azure
CDN. I set it up so that any request in the
/episodes/audio folder is
redirected straight to the CDN:
<configuration> <system.webServer> <handlers> <add name="AudioHandler" verb="*" path="/episodes/audio/*" type="GoogleAnalyticsHandler.GoogleAnalyticsHandler, GoogleAnalyticsHandler" resourceType="Unspecified" /> </handlers> </system.webServer> <location path="episodes/audio"> <appSettings> <add key="redirect-root" value="https://mycdn.azureedge.net/episodes/" /> </appSettings> </location> </configuration>
As soon as I deployed the handler and configuration to production, I was seeing results in the real-time tab of Google Analytics:
So the Google Analytics Handler makes it very straightforward to add Google Analytics tracking to your media assets like audio and video, and to your non-HTML pages like text and XML.