Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A simple DotNetNuke Google Analytics Module

0.00/5 (No votes)
26 Aug 2007 2  
Adding Google Analytics tracking to your DotNetNuke website the easy way.

Introduction

Google Analytics is great. A free, fully featured website tracking and reporting system. It's even better if you use Google Adwords and want to test out how different ads work with getting visitors. DotNetNuke is great. A free, fully featured web platform in which the sky is the limit for developing websites quickly and easily. What I found, though, was that putting the two together, while not difficult, is quite clunky. So, I set out to create a much better way.

Background

Google Analytics works by you (the webmaster or developer) inserting a piece of JavaScript into each page you want tracked by Google. As visitors view your website pages, Google records all of the page views. You then go to the Analytics website and view your reports.

Entering JavaScript into pages is tough with DotNetNuke as you don't want to modify the base code. You can put the script into a skin, but if you've downloaded a skin for your site, you mightn't want to fiddle with it. There's no easy way to control where to put some JavaScript code on a DotNetNuke page.

Enter the DotNetNuke Google Analytics module - a free and easy way to integrate the two together.

The DotNetNuke Google Analytics module

The Google Analytics module is a 'normal' DotNetNuke module, so if you have admin permissions on your DotNetNuke website to install modules, then you can install this one. It doesn't use any database tables or web.config modifications, so should be OK for anyone on shared hosting to use. It is a lightweight module, and is designed not to place extra load onto the website.

How to hook up Google Analytics to your DotNetNuke website

Step 0: Create and/or locate your login details for your Google Analytics account. If you haven't already got an account, follow the instructions on the Analytics site to create an account and website profile (http://analytics.google.com).

Step 1: Download the Module install Zip file from this page.

Step 2: Install the module by using the 'Module Install' functionality in your DotNetNuke portal. To check that the module installed correctly, it should appear in the 'Module' drop down list in your Control Panel, under the name "iFinity Google Analytics".

Step 3: Go to the home page of your DotNetNuke portal, and use the 'Add New Module' functionality to add an instance of the iFinity Google Analytics module to the page. I normally use the bottom pane to keep it out of the way of other modules. Don't be worried that you can see the module in 'Edit' mode. The module will disappear when you log out as Administrator.

Step 4: Click on the 'Settings' control for the Google Analytics module you just created. Then, scroll the Settings page until you get to the 'Advanced Settings' section, and check the 'Display Module on All Pages'. This ensures that all pages on your website are tracked by Google Analytics.

See the red highlighted area:

Screenshot - gaexample2.jpg

Advanced Settings section of the DotNetNuke module

If for any reason, you didn't want all pages on your site to be tracked, you can just copy the module from the home page onto the pages you want tracked. Scroll further down the page, expand the 'Page Settings' section until you get to the 'Basic Settings' section, and select Visibility: None, and make Display Container unchecked. This hides the Google Analytics module from visitors.

See the red highlighted area:

Screenshot - gaexample1.jpg

Basic Settings section of the DotNetNuke module

Step 5: Scroll down the page further until you reach the 'Analytics Script Generator Settings' section. Under here is the specific settings for your site. Leave this page for a minute and open up a new browser window to go to Google Analytics.

Step 6: Go to Google Analytics at http://analytics.google.com (make sure you sign in with your account), and click on 'Edit' next to the website profile you have set up. This will bring up the details of the website profile. Then, find the 'Check Status' link on the page and click that. You should see a box containing some JavaScript. Instead of copying out all the JavaScript as directed, you only need the account number, or the 'UA' number. Copy the UA number from the Google page into your DotNetNuke site, in the 'Google Analytics Tracking ID' field. You do not need the inverted commas around the number.

See the following example:

Screenshot - gaexample3.jpg

Analytics Settings section of the DotNetNuke Module

Step 7: (optional) If you'd like to restrict tracking so that it doesn't show on the reports for certain visitors, you can select a DotNetNuke security group in the next drop down list. For example, you don't want the administrative editing to show up as page hits on Google Analytics. In that case, you would select 'Administrators' as the group to exclude from tracking.

Step 8: Click on Update. The page should refresh and return to the home page of your site. If you do a "View Source" on your browser and scroll to the bottom of the HTML, just before the body tag, you should see the generated script. (Note: if you selected the Adminstrator group, you'll have to sign out first).

Step 9: Go back to your Google Analytics account. It should still be on the page showing the tracking code to be added. Click on the 'Finish' button at the bottom. When it returns to the website profile page, you should see a green tick and the words 'Receiving Data'. If you do see this, your site is providing statistics to Analytics and you are all set.

Advanced options

The security group option is there so that any editing and other administrative activity doesn't show up on the Analytics reports. However, if you wanted to separate out usage for, say, registered vs. non-registered users, you can set up two profiles in Analytics and create two Analytics modules in your DotNetNuke site. You can then set one to track unregistered users by excluding registered users. and vice versa. The other box on the settings page is the 'Do not generate tracking script for calls to this host'. This option allows you to interrupt calls to Google when the site is called using a specific host-name. I normally use this so that when running tests on localhost, I'm not bothering the Google server. However, instead of totally hiding the code, all that happens is that the urchinTracker() JavaScript call is commented out, so you can still check that it is working.

So how does it work?

If you want to see the code, it is included with the module install. For a high level intro, all that happens in the module is the collection of the module settings (UA-ID, security groups, and host name). The code determines if tracking should be done for the incoming requests and checked against those variables. If the answer is yes, some JavaScript is inserted using the 'RegisterClientScript' ASP.NET call. And, that's it.

About the only tricky bit is the use of a created BasePage property, a trick I learnt from Scott McCulloch of www.ventrian.com.au. By creating this property inside a class which is derived from PortalModuleBase, you can get access to all sorts of goodies like the meta tags, headers, and the base ClientScript object for the page.

//snip
//now register the script
this.BasePage.ClientScript.RegisterStartupScript(GetType(), 
              "analytics", script);

//end snip
//expose the base page
public DotNetNuke.Framework.CDefault BasePage
{
   get
   {
      return (DotNetNuke.Framework.CDefault)this.Page;
   }
}

History

  • First version : 27th August 2007.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here