Track visitors’ activities with Sitecore goals

Dominik Markowski
3 min readNov 16, 2020

In Sitecore CMS, goals describe activities that visitors have performed on the website. Sitecore provides out-of-the-box functionality that allows to create, assign and track goals. You can create goals that represent various actions such as registration, login, visit on a particular page, completion of a survey and many more. Once created, goals need to be configured to be triggered once a certain objective is achieved.

The article covers the following topics:
1. how to create a goal,
2. how to assign a selected goal to items and automate that process with SPE,
3. how to register a goal in the code.

Create a goal item

To create a goal item, open the Marketing Control Panel. Then, create a new item, based on the Goal template, under folder /sitecore/system/Marketing Control Panel/Goals/. Goal item has a defined workflow, so you can’t assign it to content right away. First, you need to move it to Deployed state in the workflow. To do so, click on the Deploy icon in the Review tab.

Assign a goal to item

Once the goal has been created, you can assign it to a page item. When a visitor opens a page, the assigned goal is registered for her/his contact. To add a new goal to an item, open the Content Editor view and select the item from the content tree. Then, open the Analyze tab and click on the Goals icon. Select the goal from the list in the displayed modal window and accept with the OK button. The goal is now assigned to the content item. Don't forget to publish the item once you finish editing.

Very often, you have to assign a goal to many pages, e.g. 100 or 100,000 items. Such repetitive work could create thoughts of self-harm, but luckily it can be automated with SPE and a simple script in PowerShell.

Function AddGoalToItem {
Param ($item, $goalItem)
$trackingTag = $item.PSFields."__Tracking".Value
$goalId = $goalItem.ID
$goalName = $goalItem.Name
$goalTag = '<event id="' + $goalId + '" name="' + $goalName + '" />'
$trackingStartTag = '<tracking>'
$trackingEndTag = '</tracking>'
if ($trackingTag.Indexof($goalTag) -eq -1) {
if ($trackingTag.Indexof($trackingStartTag) -eq -1) {
$trackingTag = $trackingStartTag + $goalTag + $trackingEndTag
}
else {
$trackingTag = $trackingTag.Replace($trackingEndTag, $goalTag) + $trackingEndTag
}
$item.Editing.BeginEdit()
$item["__Tracking"] = $trackingTag
$item.Editing.EndEdit()
Write-Host "Goal: $goalName added to item: $($item.Name)"
}
else {
Write-Host "Goal: $goalName already added to item: $($item.Name)"
}
}
$goalItem = Get-Item -Path "master:\system\Marketing Control Panel\Goals\Test goal"
$items = Get-ChildItem -Path "master:\content\home" -Recurse
Foreach ($item in $items) {
AddGoalToItem $item $goalItem
}

Register a goal in code

To trigger a goal with a different action than opening a page change in code is needed. Tracker.MarketingDefinitions.Goals contain a list of goal definitions items. Specific item can be received by its Guid or ID object. Then, call Tracker.Current.CurrentPage.RegisterGoal to register the goal for current visitor’s contact.

var goalIdString = "{66722F52-2D13-4DCC-90FC-EA7117CF2298}";if (Guid.TryParse(goalIdString, out var goalGuid))
{
var goalDefinition = Tracker.MarketingDefinitions.Goals[goalGuid];
Tracker.Current.CurrentPage.RegisterGoal(goalDefinition);
}

Thanks for reading. All code snippets are available on my repo. As always, your feedback is highly appreciated.

--

--

Dominik Markowski
0 Followers

Dominik is a software engineer with background in web development and space engineering. Currently working with Sitecore and ASP.NET technologies.