Wednesday, November 28, 2007

Using Google Analytics with DotNetNuke (DNN)

I use a content management system called DotNetNuke to build a lot of my websites. It's tremendous in its flexibility and power, and {gasp} it's an open source project for the Microsoft platform. I've used other CMS tools, but nothing comes close to the power of DotNetNuke, especially when extending it using ASP.NET.

One problem I've ran into when attempting to use Google Analytics with DNN derives from the fact that each page is built dynamically when the user requests the page. A single default.aspx file is used for all page requests, and the content, skin, objects, etc. are injected as elements into this default.aspx file to create the page. On the surface it would seem simple to just copy and paste the Google Analytics code into this single default.aspx file. Some problems arise from this:

  • Sometimes multiple sites share this default.aspx file. Tracking reports from Google Analytics will then show the numbers for all these sites (or portals as they are called in DNN parlance)
  • Pushing the Google Analytics code into every DotNetNuke page using a single default.aspx file creates an issue with the DNN administrative pages. Not that it hurts anything, but those pages shouldn't be counted in the usage totals - it skews the results.
  • If you have any https pages in your site, they will fail the security test because the Google Analytics code uses plain-jane vanilla http requests. (Note - I've seen some workarounds for this, but none have been satisfactory).
  • The Google Analytics code shows extremely skewed results for some pages because of the strange way that DotNetNuke builds the URLs for each page. The friendly URL problem will hopefully have a fix in a future release.
So what to do? The answer is simpler than it seems.
  1. Login as either the DNN administrator or super user (host) account.
  2. Navigate to your home page
  3. Add a Text/HTML Module, preferable somewhere unobtrusive like the bottom of the page
  4. First, change the settings on the Text/HTML module so that the container is invisible (uncheck the 'Display Container' under the Page Settings section) and save your changes.
  5. Next, click the 'Edit Text' button on the module
  6. In the Edit Text window, click on the 'Source' button to view the actual HTML. Delete whatever content is in there (if any) and copy and paste your Google Analytics code that you received from Google. Make sure to click the Update button to save the changes.
  7. Next, go back into the Settings for the module, but this time expand the 'Advanced Settings' tab. Make sure the 'Show on every page' checkbox is checked and click 'Update'
  8. If you have https pages, navigate to those and manually delete the module from them. You won't be able to track your secure pages (maybe someone has a suggestion about this).
Make sure that Google Analytics is picking up the code, and if so, you'll have to wait until the next day to view your stats through the Analytics reports. Google updates the stats everyday at about midnight Pacific time, so statistics are never real time and always one day behind, but they are a great tool to have.

Happy stats!