Accessing facets and services

Abstract

How to access contact facets, interaction facets, and services such as the xConnect Client API in the context of an interaction aggregation processor.

This topic describes how to access contact facets, interaction facets, and services such as the xConnect Client API in the context of an interaction aggregation processor.

The interaction aggregation processing pipeline uses the xConnect Client API to retrieve contacts and interactions. You must specify which contact and interaction facets to retrieve by decorating your processor with one or both of the following attributes:

  • UsesInteractionFacets

  • UsesContactFacets

Note

Facets are requested from xConnect when the aggregation pipeline is executed. Only request the facet that a processor requires in order to reduce the impact on performance.

In the following example, the processor loads one interaction facet and one contact facet:

[UseInteractionFacets(WebVisit.DefaultFacetKey)]
[UsesContactFacets(PersonalInformation.DefaultFacetKey,Classification.DefaultFacetKey)]
public class UserAgentProcessor:InteractionAggregationPipelineProcessor
{
 protected override void OnProcess(InteractionAggregationPipelineArgsargs)
 {
 }
}

Important

If you do not explicitly request a particular facet, it will not be available during processing. To understand why, read more about the role of expand options when getting contacts and interactions.

Accessing the contact and interaction

The contact and interaction are available on the args.Context property:

[UseInteractionFacets(WebVisit.DefaultFacetKey)]
[UsesContactFacets(PersonalInformation.DefaultFacetKey, Classification.DefaultFacetKey)]
public class UserAgentProcessor: InteractionAggregationPipelineProcessor
{
protected override void OnProcess(InteractionAggregationPipelineArgsargs)
  {
  var contact=args.Context.Contact;
  var interaction=args.Context.Interaction;
  }
}

If you requested any interaction or contact facets, they can be accessed via the .GetFacet<T> extension method:

var personalFacet=args.Context.Contact.GetFacet<PersonalInformation>
(PersonalInformation.DefaultFacetKey);

Alternatively, use the extension methods found in Sitecore.XConnect.Collection.Model:

var personalFacet=args.Context.Contact.Personal();

Every processor has access to the following APIs via args.Context.Sources:

  • xConnect Client API

  • Reference Data Service

  • Definition managers

  • Taxonomy managers

Important

In the context of an aggregation processor, all service end points - xConnect, definitions, taxonomy, and the reference data service - are read-only.

Accessing xConnect

A read-only instance of the xConnect Client API is accessed via args.Context.Sources.Collection. You can search or get contact and interaction data:

[UseInteractionFacets(WebVisit.DefaultFacetKey)]
[UsesContactFacets(PersonalInformation.DefaultFacetKey, Classification.DefaultFacetKey)]
public class UserAgentProcessor: InteractionAggregationPipelineProcessor
{
  protected override void OnProcess(InteractionAggregationPipelineArgsargs)
  {
    // Searching
    var contacts = args.Context.Sources.Collection.Contacts.Where(x=>x.Identifiers.Any(c=>c.IdentifierType==ContactIdentifierType.Known));
  var interactions = args.Context.Sources.Collection.Interactions.Where(x=>x.EngagementValue>20);
  // Get some contact or interaction that is not available in the args
  var someID=Guid.NewGuid();
  var contactRef=newList<ContactReference>(){newContactReference(someID)};
  var getContact=args.Context.Sources.Collection.GetAsync<Contact>(contactRef,newContactExpandOptions());
  }
}

Note

Remember that the contact and interaction are available via args.Context.Contact and args.Context.Interaction respectively.

Accessing definition managers

[UseInteractionFacets(WebVisit.DefaultFacetKey)]
[UsesContactFacets(PersonalInformation.DefaultFacetKey, Classification.DefaultFacetKey)]
public class UserAgentProcessor:InteractionAggregationPipelineProcessor
{
  protected overridevoidOnProcess(InteractionAggregationPipelineArgsargs)
  {
    protected overridevoidOnProcess(InteractionAggregationPipelineArgsargs)
    {
      var goalManager=
args.Context.Sources.Definitions.GetDefinitionManager<IGoalDefinition>();
    }
  }
}

Note

There is no generic definition manager, which means that you must know which type of event you are looking for and use the appropriate definition manager.

Accessing taxonomy managers

[UseInteractionFacets(WebVisit.DefaultFacetKey)]
[UsesContactFacets(PersonalInformation.DefaultFacetKey, Classification.DefaultFacetKey)]
public class UserAgentProcessor: InteractionAggregationPipelineProcessor
{
  protected override void OnProcess(InteractionAggregationPipelineArgsargs)
  {
   protected override void OnProcess(InteractionAggregationPipelineArgsargs)
  {
    var assetTaxonomyManager = 
args.Context.Sources.Taxonomies.GetManager<AssetTaxonomyManager>();
  }
 }
}

Note

There is no generic taxonomy manager, which means that you must know which type of taxonomy you are looking for and use the appropriate taxonomy manager.

Accessing the reference data service

[UseInteractionFacets(WebVisit.DefaultFacetKey)]
[UsesContactFacets(PersonalInformation.DefaultFacetKey, Classification.DefaultFacetKey)]
public class UserAgentProcessor: InteractionAggregationPipelineProcessor
{
  protected override void OnProcess(InteractionAggregationPipelineArgsargs)
  {
     var definitionType=args.Context.Sources.Reference.EnsureDefinitionType("alpha");
     var criteria=newDefinitionCriteria("enterprise",definitionType)
     {
        Culture=newCultureInfo("da")
     };
     Definition<string,string>definition=args.Context.Sources.Reference.GetDefinition<string,string>(criteria,true);
  }
}

Note

The aggregation pipeline uses a read only version of the Reference Data Service API that connects directly to the Reference Data Service database.