views:

658

answers:

3

I'm working on a site that uses Google Analytics. On most of the pages the default call to _trackPageView() that records the page view in GA using the current URL works just fine. I can add that code to a master page so it is included on each page.

However, under certain circumstances I want to override the name of the page that gets recorded in GA. For instance I may have a single page that has multiple steps and want to record them as separate pages in GA. The _trackPageView method works fine for this but I'm not sure of the best way to incorporate it into ASP.NET. I don't want to handle the logic of rendering the call to _trackPageView on each page.

I'm thinking that I either expose a property on the master page (something like OverrideGAURL) that if present is inserted into the call to _trackPageView or wrap the GA script rendering into a user control that has a method or property exposed to override the default rendering.

Has any done something similar or have suggestions on the best way to approach this? My main goal is to encapsulate the GA script rendering and be able to override the page name with just one line of code in the code behind or by setting an attribute.

+2  A: 

I'm not an ASP.NET guy, but using _trackPageView to generate an additional virtual page view can be done only one way: calling the method with the appropriate URL path. On pages with multiple steps you have to call _trackPageView for each step, like this:

pageTracker._trackPageView('/Page_name/Step2');

Beyond this, you'll still need the original _trackPageView call in the standard tracking code.

See How do I track AJAX applications? in Google Analytics Help for further details.

Török Gábor
A: 

You're probably better off just letting Google track the standard page name with multiviews, and just filtering the page name out with Google Analytics if you don't want it showing up in your reports. Then, in each view, just throw this script in: pageTracker._pageView('/UniqueViewName'); to track each step however you'd like then to show up in your content.

To learn about filtering pages, see: https://www.google.com/support/googleanalytics/bin/answer.py?answer=55593&hl=en_US&utm_id=ad

A: 

Ended up creating a simple user control with a public PageName property. If the PageName property is set then _trackPageView is called and the PageName value is passed otherwise _trackPageView is called without any parameters.

This approach is working pretty well and made it really easy to manage the GA tracking code between environments by reading it from the web.config within the user control.

joshb