Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Korayem/2869209 to your computer and use it in GitHub Desktop.
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
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