Programmatically Trigger a Goal in Sitecore xDB



While the out of the box Sitecore xDB goal triggering is OK, a lot of the time you don’t want to trigger a goal based on a user hitting a page as it can be slightly inaccurate. Instead, it makes more sense to trigger a goal when an action is completed in the code. 

Example: Register for the website
You can trigger the goal based on the Thank You page, but what if someone triggers that thank you page a second time or someone sends a friend the thank you page link. I realize these scenarios are unlikely, but I felt the fool proof way to capture this would be to programmatically trigger the goal immediately following the user creation. This will always give precise numbers. 

Example: Comment on a blog post
This scenario there is no landing page so the out of the box method wouldn’t work. So I decided to add the programmatic goal trigger immediately following the creation of the comment. 

Simply call TriggerGoal passing in the ID of the Sitecore Goal! 

Here is the code for triggering a goal (modified from Ryan Bailey's original code):

 public static bool TriggerGoal(Sitecore.Data.ID goalId)
        {
            bool triggeredGoal = false;
            //Sitecore.Diagnostics.Log.Info(string.Format("Triggering Goal: {0}",goalId.ToString()), "xDBHelper");
            try
            {
                //start tracker if not active
                if (!Sitecore.Analytics.Tracker.IsActive)
                {
                    Sitecore.Analytics.Tracker.StartTracking();
                }

                //additional check to ensure that Tracker is active and has a CurrentPage 
                if (Sitecore.Analytics.Tracker.IsActive && Sitecore.Analytics.Tracker.Current.CurrentPage != null)
                {
                    // Look up the goal item
                    var goalItem = Sitecore.Context.Database.GetItem(goalId); 
                    if (goalItem != null)
                    {
                        //Sitecore.Diagnostics.Log.Info(string.Format("Triggering Goal Item: {0}", goalItem["Name"]), "xDBHelper");
                        var goal = new PageEventItem(goalItem); // Wrap goal in a PageEventItem
                        var pageEventsRow = Tracker.Current.CurrentPage.Register(goal); // Create PageEventData for the goal to be stored
                        pageEventsRow.Data = goalItem["Name"]; //Add name of goal to Data field --- not sure this is needed
                        Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications(); //updates current interaction's end date time
                        triggeredGoal = true;
                    }
                }
            }
            catch (Exception ex)
            {
                //if you can't track log a warning but do impact user for analytics issues
                Sitecore.Diagnostics.Log.Warn(string.Format("Failed to trigger goal: {0}", ex.ToString()), "xDBHelper");
            }
            return triggeredGoal;
        }


References: http://blog.ryanbailey.co.nz/2015/08/sitecore-triggering-goal-programatically.html

Comments