Hello Community!, I would like to focus my next series of blogs on how to achieve various tasks in Sitecore DMS Programatically.
In this Blog Post I would like to start with ”How to trigger a DMS goal Programatically”.
Pre-Requisites to Trigger Sitecore DMS Goal Programatically
Before we can trigger an DMS Goal programmatically, I would like to outline some pre-requisites
- Create a Goal in the Marketing Center ( /sitecore/system/Marketing Center/Goals)
- Deploy and Publish the Goal
How to Trigger a Goal Programatically
There are two ways to trigger a goal programmatically
Method 1 : Trigger an Goal using Goal Item ID (Preferable Way)
This method uses the item ID of the goal to trigger and is a better way since item ids in sitecore do not change if the location of the goal is changed or if the name of the goal is changed.
if (Sitecore.Analytics.Tracker.IsActive && Sitecore.Analytics.Tracker.CurrentPage != null)
{
Sitecore.Data.Items.Item GoaltoTrigger = Sitecore.Context.Database.GetItem("{Item ID of the Goal}");
if(GoaltoTrigger!=null)
{
Sitecore.Analytics.Data.Items.PageEventItem registerthegoal = new Sitecore.Analytics.Data.Items.PageEventItem(GoaltoTrigger);
Sitecore.Analytics.Data.DataAccess.DataSets.VisitorDataSet.PageEventsRow eventData = Sitecore.Analytics.Tracker.CurrentPage.Register(registerthegoal);
eventData.Data = GoaltoTrigger["Description"];
Sitecore.Analytics.Tracker.Submit();
}
}
Method 2 : Trigger an Goal using Goal Item Name
if (Sitecore.Analytics.Tracker.IsActive && Sitecore.Analytics.Tracker.CurrentPage != null)
{
Sitecore.Analytics.Data.PageEventData GoaltoTrigger = new Sitecore.Analytics.Data.PageEventData("Item Name of the Goal");
Sitecore.Analytics.Data.DataAccess.DataSets.VisitorDataSet.PageEventsRow eventData = Sitecore.Analytics.Tracker.CurrentPage.Register(GoaltoTrigger);
eventData.Data = "Short Description of the Goal";
Sitecore.Analytics.Tracker.Submit();
}
Should have any questions or concerns Please do not hesitate to comment below.