Programmatically trigger Sitecore DMS goals

Sitecore provides you with a robust way of configuring when a DMS goal is triggered using the rules editor.
Its usage is quite intuitive and there are a vast number of options available for the configuration.

But at times depending on the site specific custom business logic we have in place, we might want to be able to trigger a goal from our code.

Here is how we would do that.
Following is the Demo Goal I have created and am going to work with:
2014-06-28_155804

To trigger the goal from code:

            if (!Sitecore.Analytics.Tracker.IsActive)
                Sitecore.Analytics.Tracker.StartTracking();

            if (!Sitecore.Analytics.Tracker.IsActive 
                || Sitecore.Analytics.Tracker.CurrentPage == null) 
                return;

            var goalItem = Sitecore.Context.Database.GetItem("{710AC40A-EFC3-43AE-B7F8-225716D73EC9}");
            var goal = new PageEventItem(goalItem);
            var pageEventsRow = Sitecore.Analytics.Tracker.CurrentPage.Register(goal);

            pageEventsRow.Data = "Demo goal test information : " 
                + DateTime.Now.ToString("F");
            pageEventsRow.ItemId = Sitecore.Context.Item.ID.Guid;
            pageEventsRow.DataKey = Sitecore.Context.Item.Paths.Path;

            Sitecore.Analytics.Tracker.Submit();

Add

using Sitecore.Analytics.Data.Items;

Getting a report of the goals triggered is not really straightforward when you use the Sitecore Engagement Analytics option.
You could instead directly fire a query on the analytics database to see immediate results with ease.

SELECT Name, DataKey, ItemId, Data, 
pe.PageEventDefinitionId AS GoalItemId, Value AS GoalValue, 
VisitorId, VisitId, [DateTime] 
FROM dbo.PageEvents pe, dbo.PageEventDefinitions ped
WHERE pe.PageEventDefinitionId = ped.PageEventDefinitionId
AND ped.IsGoal = 1
AND ped.Value!=0
ORDER BY [DateTime] DESC

2014-06-28_161451

Advertisements

One thought on “Programmatically trigger Sitecore DMS goals

  1. Hi ,

    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 with update-4. Could you let me know Is there any issue in the code, If not how can we view that changes in analytics report.

    One more strange issue here was “PageEvents” table is not exists in the analytics DB. 😦 Let me know what would be the issue here

    Thanks in advance..

    Regards,
    Kali Krishna

    Like

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