I read in a blog post that JSON service calls are recorded to the Analytics database. I have verified this and I’m also using JSON service calls in my Sitecore 7.2 MVC 5 web application. I do not want these calls to be recorded to the Analytics database as they have no relevance to marketers. To do this, I’ve updated my Sitecore.Mvc.Analytics.config in the App_Config/Include folder. I’ve replaced the processor with my own.
<mvc.requestBegin> <processor type="Site.Web.Code.Analytics.Pipelines.MvcEvents.RequestBegin.StartTracking, Site.Web"/> </mvc.requestBegin>
I’ve also added the code file StartTracking.cs
using System; using System.Web; using Sitecore; using Sitecore.Analytics; using Sitecore.Analytics.Configuration; using Sitecore.Analytics.Data.DataAccess.DataSets; using Sitecore.Diagnostics; using Sitecore.Mvc.Pipelines.Request.RequestBegin; using Sitecore.Sites; using Sitecore.Web; using System.Web.Routing; using Sitecore.Mvc.Extensions; namespace Site.Web.Code.Analytics.Pipelines.MvcEvents.RequestBegin { public class StartTracking : RequestBeginProcessor { private const string _scDisableAnalyticsKey = "scDisableAnalyticsPageViews"; public override void Process(RequestBeginArgs args) { Assert.ArgumentNotNull((object)args, "args"); if (!AnalyticsSettings.Enabled) return; SiteContext site = Context.Site; if (site == null || !site.EnableAnalytics || site.DisplayMode != DisplayMode.Normal) return; // get the current route data from the http context RouteData routeData = RouteTable.Routes.GetRouteData((HttpContextBase)new HttpContextWrapper(HttpContext.Current)); if (routeData != null) { // get the specific route values based off your current route/url RouteValueDictionary routeValueDictionary = ObjectExtensions.ValueOrDefault<Route, RouteValueDictionary>(routeData.Route as Route, (Func<Route, RouteValueDictionary>)(r => r.Defaults)); if (routeValueDictionary != null && routeValueDictionary.ContainsKey(_scDisableAnalyticsKey)) { bool disableAnalytics = false; // check if scDisableAnalyticsKey is one of those values in the route if (bool.TryParse(routeValueDictionary[_scDisableAnalyticsKey].ToString(), out disableAnalytics)) { if (disableAnalytics) { // stop Tracker.StartTracking() line below from executing in the mvc.requestBegin pipeline return; } } } } Tracker.IsActive = false; Tracker.StartTracking(); if (!Tracker.IsActive) return; VisitorDataSet.VisitsRow currentVisit = Tracker.CurrentVisit; if (!string.IsNullOrEmpty(currentVisit.AspNetSessionId)) return; currentVisit.AspNetSessionId = WebUtil.GetSessionID(); } } }
This code checks if you have a route with the scDisableAnalyticsPageViews key set to true, if you include this key in your route, the StartTracking processor will return before Tracker.StartTracking() is called and your visit won’t be recorded to the Analytics database. Here is my route:
context.MapRoute( "SiteJSON", "SiteJSON/{action}/{id}", new { scDisableAnalyticsPageViews = true, controller = "Json", action = "Index", id = UrlParameter.Optional, } );
Now if I use SQL Management Studio and go to the Pages table of my Analytics database, these entries will not longer be recorded.
