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.


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

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.


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

Method 3 : Trigger an Goal using Query Parameter

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


/*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}

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

3 thoughts on “Sitecore DMS – Trigger Goals Programatically

Add yours

  1. Hi Sheetal Jain,

    I’ve a requirement that when user clicks on a link. we need to trigger the goal. So have a written below logic one action method which used to call through AJAX request while user clicks the link.

    if (Sitecore.Analytics.Tracker.IsActive && Sitecore.Analytics.Tracker.CurrentPage != null)
    {
    var goalItem = Sitecore.Context.Database.GetItem(goal);
    var goalPageEvent = new PageEventItem(goalItem);
    var pageEventsRow = Sitecore.Analytics.Tracker.CurrentPage.Register(goalPageEvent);

    pageEventsRow.Data = goalItem.Name + ” has been fired at ”
    + DateTime.Now.ToString(“F”);
    // pageEventsRow.ItemId = Sitecore.Context.Item.ID.Guid;
    // pageEventsRow.DataKey = Sitecore.Context.Item.Paths.Path;
    Sitecore.Analytics.Tracker.Submit();
    }

    Here “goal” variable represents the ID of the goal. It’s executing successfully without any exception. but i’m not view the changes in Goal analytic report in “Experience analytics” in sitecore. I’m using sitecore 8.1. Could you let me know Is there any issue in the code, If not how can we view that changes in analytics report.

    I gone through some articles that were saying that , you can able to see the records in PageEvents table in the analytics DB. I’m not able to see that table in my analytics DB

    Thanks in advance..

    Regards,
    Kali Krishna

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: