1

I am upgrading code from Sitecore 8.2 to Sitecore 9.3.

In Sitecore 8.2 we have a code snippet:-

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

        return null;
    }

    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.Identify(contactIdentifier);

        return Tracker.Current.Contact;
    }

In this Sitecore 8.2 Contact object is coming from Sitecore.Analystics.Tracking but in Sitecore 9.3 do you need to convert Contact to Sitecore.XConnect.Contact?

 Contact contact = _contactProvider.GetCurrentContact();

DocLanguageAndRegionPreferences docPreferences;

_contactRepository.SetDocumentLanguageAndRegionPreference(contact, docPreferences);

I have another class using this contact to add item to facet and then communicating with Xdb in Sitecore 8.2

public void SetDocumentLanguageAndRegionPreference(Contact contact, DocLanguageAndRegionPreferences docPreferences)
    {
        if (contact == null)
            return;

        IFacetData facet = contact.GetFacet<IFacetData>(FacetData.FacetName);
        
        if (docPreferences.Languages != null && docPreferences.Languages.Any())
        {
            foreach (var language in docPreferences.Languages)
            {
                var docLanguage = facet.FacetCentralData.Preferences.DocumentLanguageFilters.Create();
                docLanguage.Language = language;
            }
        }
    }

This is Sample code of that class. Facet is working fine, just have some doubt. In this class also I am getting parameter contact in form on Sitecore.Tracking.Analytics.Contact. How can I convert this contact to XConnect.Contact for Sitecore 9.3?

2

1 Answer 1

2

You don't need to explicitly convert Sitecore.Tracking.Analytics.Contact to XConnect.Contact as this is done on session end where data collected by the tracker is converted. You can take a look at the conversion pipelines described here https://doc.sitecore.com/developers/93/sitecore-experience-platform/en/conversion-pipelines.html, which are also responsible for loading existing contacts into tracker.

XConnect.Contact is used to save/load contact data to xConnect as shown on the diagram https://doc.sitecore.com/developers/93/sitecore-experience-platform/en/tracking-and-xconnect.html.

The Sitecore Tracker uses Sitecore.Analytics.Tracking.Contact to track and identify contacts and their interactions during their visit to the CD instance, it does not use the xConnect model. You should use this model if you wish to modify the contact values in session before being saved into xDB.

Your Answer

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

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