Accessing marketing definitions

In a tracking context, access to marketing definitions is required when triggering events. For example, the PageEventData class must be instantiated with an event alias. An event’s alias is represented by the Alias property of the base IDefinition as shown:

IDefinition ev = Sitecore.Analytics.Tracker.MarketingDefinitions.Goals[NewsletterSignupGoal.NewsletterGoalID];

if (ev != null)
{
    var newsletterId = Guid.NewGuid(); // Fake newsletter ID
    var pageData = new Sitecore.Analytics.Data.PageEventData(ev.Alias, ev.Id);

    pageData.Data = newsletterId.ToString();

    Sitecore.Analytics.Tracker.Current.CurrentPage.Register(pageData);
}

There are two ways to access marketing definitions from the tracker:

  • Use the Sitecore.Analytics.Tracker.MarketingDefinitions property, which defines collections such as Goals and Campaigns.

  • Use a definition manager, such as PageEventDefinitionManager

The following examples demonstrate how to retrieve all campaigns using the MarketingDefinitions property to get a campaign by ID:

var definitionID = Guid.NewGuid();

IDefinition campaign = Sitecore.Analytics.Tracker.MarketingDefinitions.Campaigns[definitionID];

Alternatively, use a definition manager. Every definition type has a dedicated definition manager. The following example demonstrates how to get a campaign using the campaign manager:

using System;
using System.Globalization;
using Sitecore.DependencyInjection;
using Sitecore.Marketing.Definitions;
using System.Globalization;
using Sitecore.Marketing.Definitions.Campaigns;

namespace Documentation
{
    public class GetCampaign
    {
        public void Example()
        {
            var definitionManager = ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<ICampaignActivityDefinition>();

            Guid campaignActivityID = Guid.NewGuid(); // Campaign activity ID

            // Call Get() to get concrete campaign by ID and culture
            var campaignActivityDefinition = definitionManager.Get(campaignActivityID, System.Globalization.CultureInfo.InvariantCulture); // Specific culture can also be used
        }
    }
}

Note

Using the definition manager makes it easier to unit test your own code.