Using the Rules Engine to Trigger Goals
In a Sitecore 8.x installation there is no out of the box way to trigger a goal from the rules engine. This can cause some trouble for marketers as there is no way to create a goal that is triggered on a specific visit. A typical "top of the funnel" sales approach would typically identify any visitors to the site that have returned multiple times. This blog post will cover creating a goal, writing a rule, and creating a custom action that can be used to trigger the goal.
Create the Goal
Let's create a goal called "2nd Visit" in the marketing center.
- Navigate to
/sitecore/system/Marketing Control Panel/Goals
in the Content Editor. - Create a goal named "2nd Visit"
- Under the Rules section, add a rule
where the visit no. is equal to 2
.
You will notice that there isn't an action available for triggering the goal. Let's create it now.
Create the Action
Start by creating a new Action under /sitecore/system/Settings/Rules/Definitions/Elements/Visitor
. Let's name in TriggerGoalAction
.
There are two important fields. In the Data
section fill the Text
field with the value trigger the goal
. This is the text that appears in the rules editor.
Under the Scripts
section set the Type
field to the namespace reference to you class and project. In this example, you can use AgileReaction.Feature.Actions.Models.TriggerGoalAction, AgileReaction.Feature.Actions
.
Create the Code Behind the Action
Add the following code to your Visual Studio project. Change the namespaces as appropriate.
namespace AgileReaction.Feature.Actions.Models
{
using Sitecore.Analytics.Data;
using Sitecore.Analytics.Data.Items;
using Sitecore.Analytics.Rules.PageEvents;
using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.Rules.Actions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class TriggerGoalAction<T> : RuleAction<T>
where T : RuleContext
{
public override void Apply(T ruleContext)
{
var rc = ruleContext as PageEventRuleContext;
if (rc == null)
return;
if (rc.PageEventDefinitionItem.IsGoal)
{
if (!Sitecore.Analytics.Tracker.IsActive)
Sitecore.Analytics.Tracker.StartTracking();
if (!Sitecore.Analytics.Tracker.IsActive
|| Sitecore.Analytics.Tracker.Current.CurrentPage == null)
return;
var goal = rc.PageEventDefinitionItem;
var triggeredGoals = Sitecore.Analytics.Tracker.Current.CurrentPage.PageEvents
.Where(x => x.IsGoal == true)
.Where(x => x.PageEventDefinitionId == goal.ID.Guid)
.ToList();
if (triggeredGoals.Any())
return;
var pageEventData = new PageEventData(goal.Name, goal.ID.ToGuid())
{
Data = "TriggerGoalAction : " + DateTime.Now.ToString("F"),
ItemId = Sitecore.Context.Item.ID.Guid,
DataKey = Sitecore.Context.Item.Paths.Path
};
Sitecore.Analytics.Tracker.Current.CurrentPage.Register(pageEventData);
Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications();
}
}
}
}
Pay attention to the following lines:
var triggeredGoals = Sitecore.Analytics.Tracker.Current.CurrentPage.PageEvents
.Where(x => x.IsGoal == true)
.Where(x => x.PageEventDefinitionId == goal.ID.Guid)
.ToList();
if (triggeredGoals.Any())
return;
In Sitecore, triggering a goal causes the rules on the page to be run again. Without this code there will be a stack overflow exception as the rule / action keep running.
Update the Goal
Now that we have a custom action, let's go back and update the goal. Edit the 2nd Visit
goal and open the rules editor. On the right side of the editor you will now see a trigger the goal
action. Go ahead and add it and save the item.
Add the goal to a page and you are done!