Click here to Skip to main content
6,305,776 members and growing! (15,781 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Beginner License: The Code Project Open License (CPOL)

A simple DotNetNuke Google Analytics Module

By brucerchapman

Adding Google Analytics tracking to your DotNetNuke website the easy way.
C# 2.0, Windows, .NET 2.0, ASP.NET, WebForms, VS2005, Dev
Version:2 (See All)
Posted:26 Aug 2007
Views:26,910
Bookmarked:27 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
12 votes for this article.
Popularity: 4.99 Rating: 4.63 out of 5
1 vote, 8.3%
1

2

3
2 votes, 16.7%
4
9 votes, 75.0%
5

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

brucerchapman


Member
Bruce Chapman is a developer from back when screens blinked black and green and internet browsing only came in text form. He now has a website and software development company but still likes to get his fingers dirty in code.

The latest specialty is developing websites and internet applications in C# and SQL Server 2005.

Bruce lives on the Sunshine Coast in Queensland, Australia, and is rarely seen outside without his sunglasses.
Occupation: Web Developer
Location: Australia Australia

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralError when creating GA module in DNN Pinmembermvangils10:57 27 Jan '09  
GeneralRe: Error when creating GA module in DNN Pinmemberjavierobcn5:38 10 Jun '09  
GeneralNEW VERSION 1.01.00 PinmemberGibros20:58 16 Sep '08  
GeneralRe: NEW VERSION 1.01.00 PinmemberIntelligent Netware19:54 20 Nov '08  
GeneralRe: NEW VERSION 1.01.00 Pinmemberbrucerchapman20:01 20 Nov '08  
QuestionScript is not rendered immediate before tab Pinmembermaxcom4:49 1 Sep '08  
AnswerRe: Script is not rendered immediate before tab Pinmemberbrucerchapman15:39 1 Sep '08  
GeneralDotNetNuke 4.8.4 PinmemberSteve Karpik6:18 13 Jun '08  
GeneralRe: DotNetNuke 4.8.4 Pinmemberbrucerchapman17:02 13 Jun '08  
GeneralScript appears too far up ? PinmemberBeefcake 500023:28 25 Nov '07  
GeneralRe: Script appears too far up ? Pinmemberbrucerchapman2:34 26 Nov '07  
GeneralRe: Script appears too far up ? PinmemberBeefcake 500021:46 26 Nov '07  
GeneralThanks for the module PinmemberDaniel Vaughan17:36 29 Sep '07  
GeneralSupport DotNetNuke 4.5? PinmemberMackz4:55 12 Sep '07  
GeneralRe: Support DotNetNuke 4.5? Pinmemberbrucerchapman13:35 12 Sep '07  
GeneralGroup exclude from tracking. PinmemberChris Richner22:35 27 Aug '07  
GeneralRe: Group exclude from tracking. Pinmemberbrucerchapman23:00 27 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Aug 2007
Editor: Smitha Vijayan
Copyright 2007 by brucerchapman
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project