Flushing Sitecore Analytics data to storage

Under normal circumstances, in order for analytics data to make its way into your Sitecore Experience Profile Dashboard the user’s session needs to end. Usually you’d achieve this by “logging out” of the site or waiting for the timeout period specified by the “sessionState” element within your web.config.

However, many sites don’t have a consumer log in/out functionality to manually test with… Therefore, in development / debugging / testing it can be handy to have a manual mechanism for flushing analytics data down to disk (e.g. the MongoDB). The crux of the code required to do this without logging out was hidden in the bowels of the “Getting to Know Sitecore” blog post series.

To achieve this within our site we created a controller action that we could inject in debug builds:

  1. Create an “AncillaryController” class

    public class AncillaryController : Controller
    {
            public ActionResult FlushAnalytics()
        {
            if (AnalyticsHelper.IsTracking())
            {
                var analytics = new AnalyticsHelper();
                analytics.FlushTrackerContact();
            }
            return Redirect("~/");
        }
    }
    
  2. Define the “AnalyticsHelper”

    public class AnalyticsHelper
    {
        public static bool IsTracking()
        {
            if (!Tracker.IsActive)
            {
                Tracker.StartTracking();
            }
     
            if (Tracker.Current != null && Tracker.Current.Interaction != null && Tracker.Current.Interaction.CurrentPage != null)
            {
                return true;
            }
     
            Log.Warn("Analytics is not enabled"typeof(AnalyticsHelper));
            return false;
        }
     
        public void FlushTrackerContact()
        {
            if (IsTracking())
            {
                // http://www.sitecore.net/learn/blogs/technical-blogs/getting-to-know-sitecore/posts/2014/10/introducing-the-sitecore-analytics-index.aspx
                var tracker = Tracker.Current;
                var manager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager"trueas ContactManager;
                manager.FlushContactToXdb(tracker.Contact);
                manager.SaveAndReleaseContact(tracker.Contact);
                var ctxManager = Sitecore.Configuration.Factory.CreateObject("tracking/sessionContextManager"trueas Sitecore.Analytics.Data.SessionContextManagerBase;
                ctxManager.Submit(tracker.Session);
            }
        }
    }
  3. Add a route for debug builds only

    #if DEBUG
                routes.MapRoute("Flush-Analytics""flush-analytics"new { controller = "Ancillary", action = "FlushAnalytics" });
    #endif
    

Good luck and happy Sitecore-ing!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s