Archive for category Digital Marketing Suite (DMS)

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

, , , ,

1 Comment