allBlogsList

Sitecore Code Based Page Event Tracking

Sometimes as developers we like to dig deep and blog about more complex ideas.  But tonight I want to explore the concept of programmatically inserting a page event.  A fairly simple concept but one of the first that a Sitecore developer will want to explore when they start to tackle Sitecore DMS. The first thing you are going to need is to create your own custom page event, unless you are planning to use one of the default out of box events.   These are located here:  /sitecore/system/Settings/Analytics/Page Events/ .  Upon creating the event, pay specific attention to the "Name" field.  The value you use there is what will be used in your event tracking. Assuming I called my new event "Test Event", using Sitecore.Analytics and Sitecore.Analytics.Data I can register a page event simply with the following code:

PageEventData pageData = new PageEventData("Test Event")
            {
                Text = "This is my test event message",
                Data = "test data",
                DataKey = "test key",
            };

            if (Tracker.CurrentVisit != null)
            {
                Tracker.CurrentVisit.CurrentPage.Register(pageData);
            }

So to break this down.  I create my page data setting it to the name of page event and adding some extra data.  Note that there are other data options as well so feel free to explore that.  Finally I want to make sure that there is actually a current visit and if so I register my page event to the current page.  This will work quickly for you if you are looking just a for a one page / one scenario to cover. Let's look how we can make this a bit more flexible via a class.  In a class I have created two methods:  

public static void RegisterPageEvent(string pageEventName, string pageEventText, string pageEventData, string pageEventDataKey)
        {
            // if no page set, set to current
            if (Settings.Analytics.Enabled && Sitecore.Context.Site.EnableAnalytics)
            {
                VisitorDataSet.PagesRow currentPage = Tracker.Visitor.CurrentVisit.CurrentPage;
                RegisterPageEvent(pageEventName, pageEventText, pageEventData, pageEventDataKey, currentPage);
            }
        }

        public static void RegisterPageEvent(string pageEventName, string pageEventText, string pageEventData, string pageEventDataKey, VisitorDataSet.PagesRow page)
        {
            if (Settings.Analytics.Enabled && Sitecore.Context.Site.EnableAnalytics)
            {

                PageEventData pageData = new PageEventData(pageEventName)
                {
                    Text = pageEventText,
                    Data = pageEventData,
                    DataKey = pageEventDataKey
                };

                if (Tracker.CurrentVisit != null)
                {
                    page.Register(pageData);
                }
            }
        }

Our first method allows us to assume that the current page is our target.  We get the current page, and that pass through to our other method. The second method does a couple extra things from our simple call.  First we are looking to confirm that Analytics is actually enabled and that it is enabled within the context that we are calling it in.  This second part is important if you try to attach a process to the httpModules as Sitecore admin will also pick up on it. Finally instead of using Tracker.CurrentVisit.CurrentPage.Register to register our event we are using the PageRow page object that was passed through to our method.  This can be real handy when you might be running some clean up, remove a page, but want to maintain the events on a different page.  Also if you are looking at running this through a web service call, the current page would always be that web service call, so you would want to pass the previous page object. Further expansion of this class could involve returning true or false instead of a void to check for creation of the event.  Also other data in our PageEventData could be researched to provide other data options. In summary creating a page event is a very simple task.  However if you have a little extra time, try and think ahead and create something a little more robust.  Think about scenarios where event tracking could occur as well as the methods to trigger them.