2

I am working on XDB analytics code upgrade from Sitecore 8.2 to Sitecore 9.3. I have some doubts in custom contact facet. The below code written in Sitecore 8.2 analytics. I would like understand the below analytics code will support in Sitecore 9.3 or Do i need to modify the code to xconnect.

Step1: StartTracking & IdentifyContact

public async Task StartTracking(global::Sitecore.Security.Accounts.User user)
    {
      IdentifyContact(ScUser);
    }
    
    public Contact IdentifyContact(global::Sitecore.Security.Accounts.User user)
    {
        if (user == null)
            return null;

        string contactIdentifier = GetContactIdentifier(user);

        if (string.IsNullOrEmpty(contactIdentifier) || contactIdentifier.Contains("anonymous") || Tracker.Current == null || Tracker.Current.Session == null)
        {
            return null;
        }
       
        //Tracker.Current.Contact.Identifiers.AuthenticationLevel = AuthenticationLevel.PasswordValidated;
        Tracker.Current.Session.IdentifyAs("website", contactIdentifier);
        
        return Tracker.Current.Contact;
        
    }
    
    
    private Sitecore.Security.Accounts.User _scUser;
    protected Sitecore.Security.Accounts.User ScUser
    {
        get
        {
            if (_scUser == null)
            {
                string username = _userProvider.Globalize(Sitecore.Context.Domain.Name, Sitecore.Context.User.LocalName);

                _scUser =
                    Sitecore.Security.Accounts.User.FromName(username, true);
            }
            return _scUser;
        }
    }

Step2: Getting Session contact

public Contact GetCurrentContact()
    {
        if (Tracker.IsActive && Tracker.Current != null && Tracker.Current.Session != null)
        {              
            return Tracker.Current.Session.Contact;
        }
        return null;
    }

Step3: Fetching Contact from GetCurrentContact method

   Sitecore.Analytics.Tracking.Contact contact = GetCurrentContact();

   string searchFormatPref = _contactRepository.GetSearchResultsFormatPreference(contact);

Step4: Passing analytics Session contact to GetSearchResultsFormatPreference

public string GetSearchResultsFormatPreference(Sitecore.Analytics.Tracking.Contact contact)
    {
        if (contact == null)
            return string.Empty;

        IFacetModel facet = contact.GetFacet<FacetModel>(FacetModel.FacetName);
        return facet.Preferences.SearchResultsFormat;
    }

Step5: Analytics Custom facet Model

public class FacetModel : Facet, IFacetModel
  {
    public const string FacetName = "Preference";

    
    public int PersonId
    {
        get; set;
       
    }

    public FacetModel()
    {
    
    }
}

Step6: facet config

<entities>
    <contact>
      <facets>
        <facet name="Preference" contract="Central.Provider.Sitecore.xDB.Facets.FacetModel, Central.Provider.Sitecore" />
      </facets>
    </contact>
  </entities>

I would like to understand the above functionality and code will support in sitecore 9.3 or not.

Some peoples are telling to use xconnect facet. If i use Xconnect facet I'm facing conversion facet issue.

Example: XConnect facet

public Contact GetCurrentContact()
    {
        if (Tracker.IsActive && Tracker.Current != null && Tracker.Current.Session != null)
        {              
            return Tracker.Current.Session.Contact;
        }
        return null;
    }

 Sitecore.Analytics.Tracking.Contact contact = GetCurrentContact();

 GetSearchResultsFormatPreference(contact)

// Getting facet conversion issue in the below line of code if i use xconnect facet because contact is coming from Tracker.Current.Session.Contact

 IFacetModel facet = contact.GetFacet<FacetModel>(FacetModel.FacetName)


 public string GetSearchResultsFormatPreference(Sitecore.Analytics.Tracking.Contact contact)
    {
        if (contact == null)
            return string.Empty;

        IFacetModel facet = contact.GetFacet<FacetModel>(FacetModel.FacetName);
        return facet.Preferences.SearchResultsFormat;
    }

Can Someone please help me how to proceed the above code in Sitecore 9.3.

1

1 Answer 1

0

First, you need to create the Custom Facet in xDB that will be saved in your shard0 or shard1 table. After that, you can perform operations on those custom facets. You can find below listed documentation for more understandings:

A method CurrentTracker.Session.IdentifyAs() in Sitecore.Analytics interface is used to make your contact from Unknown (Anonymous) to Known.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.