Tuesday, August 11, 2015

Sitecore triggering a goal programatically

Goals are simple to setup to be trigger on Sitecore items, however there are times where goals may need to be triggered in the code. This is generally the case where the goal is triggered based on some business logic, and in these cases the following code can be used. Please note: This code is for version 8 onward, there are some older methods which are no longer used.
if (Sitecore.Analytics.Tracker.IsActive && Sitecore.Analytics.Tracker.Current.CurrentPage != null)
    // Trigger a goal
    var goalItem = Sitecore.Context.Database.GetItem("{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"); // Goal item

    if (goalItem != null)
        var goal = new PageEventItem(goalItem); // Wrapper for goal
        var pageEventsRow = Tracker.Current.CurrentPage.Register(goal); // Goal rtecord to be stored
        pageEventsRow.Data = goalItem["Description"];
The code above accesses the goal item in Sitecore via GUID (don't forget to publish!) and then registers it to the current page - also using the goals description as extra information.

On the goal item in Sitecore, you are also able to set if the goal is shown in events/latest events in the Experience Profile (a custom image can also be defined here).

This then means that the goal will show inside of the Experience Profile, which is great for a complete overview on site users.


  1. Hi Ryan,

    I've implemented the code like you mentioned in the post. I've one doubt here. I'm not able to view the activity,Goals in experience profile(like screen shot 2). Do we have to enable any option in settings to view as i'm using sitecore 8.

    Looking forward hear your inputs on here.

    Thanks in advance.. :)

    Kali Krishna

  2. Hi Kali,

    The data may be being stored in the experience database (MongoDB), which may take time to aggregate into the analytics/reporting database in Sitecore (SQL Server). For dev/testing purposes you can flush your data through (for the current Sitecore session) using the C# code: Session.Abandon()