Sitecore DMS – Trigger Goals Programatically

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.

[sourcecode language=”csharp”]

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();
}
}

[/sourcecode]

Method 2 : Trigger an Goal using Goal Item Name

This method uses the Name of the goal item to trigger it.You will have to express caution when you trigger the goal using name.

[sourcecode language=”csharp”]

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();
}

[/sourcecode]

Method 3 : Trigger an Goal using Query Parameter

This method uses the “sc_trk” query parameter to trigger the goal

[sourcecode language=”csharp”]

/*append the query parameter "sc_trk" with the value as name of the goal item */

http://www.yourwebsite.com/?sc_trk={Item name of the Goal}

[/sourcecode]

Should have any questions or concerns Please do not hesitate to comment below.