Created
June 4, 2012 15:58
-
-
Save Korayem/2869209 to your computer and use it in GitHub Desktop.
A cool ActionFilter that when dropped over any controller or action, you got yourself google analytics event tracking! By default we assign controller name as category, and action name as the action. In the label we drop the user identity in case he/she i
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
using System.Web.Mvc; | |
using GoogleAnalyticsTracker; | |
using SocialFruits.Domain; | |
using SocialFruits.Domain.Entities; | |
namespace SocialFruits.Extensions.Attributes | |
{ | |
public class TrackEventsInGoogleAnalyticsAttribute : ActionFilterAttribute | |
{ | |
public override void OnActionExecuted(ActionExecutedContext filterContext) | |
{ | |
var gaTracker = new Tracker(ConfigurationManager.AppSettings["GoogleAnalytics"], GlobalSettings.Domain()); | |
if (filterContext.HttpContext.User.Identity.IsAuthenticated) | |
{ | |
gaTracker.TrackEvent(filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName, filterContext.HttpContext.User.Identity.Name, null); | |
return; | |
} | |
gaTracker.TrackEvent( | |
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, | |
filterContext.ActionDescriptor.ActionName, null, null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment