1

We want to bind current visitor profile with Sitecore contact. We want to do whenever user enters his or her email id in newsletter form or contact us form or registration form. This is to create know experience profile. Please suggest.

8

From the Sitecore Launch site. You can use the email address or the domain\username as inputs for for Tracker.Current.Session.Identify(domainUser); This will link the current contact with the sitecore user.

SC 8.x

public static void SetVisitTagsOnLogin(string domainUser, bool IsNewUser)
{
   string name = Sitecore.Context.User.Profile.FullName;
   if (name == String.Empty) name = Sitecore.Context.User.LocalName;
   Tracker.Current.Contact.Tags.Add("Username", domainUser);
   Tracker.Current.Contact.Tags.Add("Full name", name);

   Tracker.Current.Contact.Identifiers.AuthenticationLevel = AuthenticationLevel.PasswordValidated;
   Tracker.Current.Session.Identify(domainUser);

   if (IsNewUser)
   {
     IContactPersonalInfo personalFacet = Tracker.Current.Contact.GetFacet<IContactPersonalInfo>("Personal");
     personalFacet.FirstName = GetFirstName(name);
     personalFacet.Surname = GetSurName(name);

     IContactEmailAddresses addressesFacet = Tracker.Current.Contact.GetFacet<IContactEmailAddresses>("Emails");
     IEmailAddress address;
     if (!addressesFacet.Entries.Contains("work_email"))
     {
        address = addressesFacet.Entries.Create("work_email");
        address.SmtpAddress = GetEmailAddressFromUser(domainUser);
        addressesFacet.Preferred = "work_email";
     }
   }   
}

SC 9.x

var contactReference = _contactIdentificationRepository.GetContactReference();

using (var client = _contactIdentificationRepository.CreateContext())
{
    // we can have 1 to many facets
    // PersonalInformation.DefaultFacetKey
    // EmailAddressList.DefaultFacetKey
    // Avatar.DefaultFacetKey
    // PhoneNumberList.DefaultFacetKey
    // AddressList.DefaultFacetKey
    // plus custom ones
    var facets = new List<string> { PersonalInformation.DefaultFacetKey };

    // get the contact
    var contact = client.Get(contactReference, new ContactExpandOptions(facets.ToArray()));

    // pull the facet from the contact (if it exists)
    var facet = contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey);

    // if it exists, change it, else make a new one
    if (facet != null)
    {
        facet.FirstName = $"Jimmy";
        facet.LastName = $"McSitecore";

        // set the facet on the client connection
        client.SetFacet(contact, PersonalInformation.DefaultFacetKey, facet);
    }
    else
    {
        // make a new one
        var personalInfoFacet = new PersonalInformation()
        {
            FirstName = "Jimmy",
            LastName = "McSitecore"
        };

        // set the facet on the client connection
        client.SetFacet(contact, PersonalInformation.DefaultFacetKey, personalInfoFacet);
    }

    if (contact != null)
    {
        // submit the changes to xConnect
        client.Submit();

        // reset the contact
        _contactIdentificationRepository.Manager.RemoveFromSession(Analytics.Tracker.Current.Contact.ContactId);
        Analytics.Tracker.Current.Session.Contact = _contactIdentificationRepository.Manager.LoadContact(Analytics.Tracker.Current.Contact.ContactId);
    }
}

my contact repo

using System.Linq;
using Sitecore.Analytics;
using Sitecore.Analytics.Model;
using Sitecore.Analytics.Tracking;
using Sitecore.Configuration;
using Sitecore.XConnect;
using Sitecore.XConnect.Client.Configuration;

namespace Sitecore.Foundation.Accounts.Repositories
{
    public class ContactIdentificationRepository
    {
        private readonly ContactManager contactManager;

        public ContactManager Manager => contactManager;

        public ContactIdentificationRepository()
        {
            contactManager = Factory.CreateObject("tracking/contactManager", true) as ContactManager;
        }

        public IdentifiedContactReference GetContactReference()
        {
            // get the contact id from the current contact
            var id = GetContactId();

            // if the contact is new or has no identifiers
            var anon = Tracker.Current.Contact.IsNew || Tracker.Current.Contact.Identifiers.Count == 0;

            // if the user is anon, get the xD.Tracker identifier, else get the one we found
            return anon
                ? new IdentifiedContactReference(Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource, Tracker.Current.Contact.ContactId.ToString("N"))
                : new IdentifiedContactReference(id.Source, id.Identifier);
        }

        public Analytics.Model.Entities.ContactIdentifier GetContactId()
        {
            if (Tracker.Current?.Contact == null)
            {
                return null;
            }
            if (Tracker.Current.Contact.IsNew)
            {
                // write the contact to xConnect so we can work with it
                this.SaveContact();
            }

            return Tracker.Current.Contact.Identifiers.FirstOrDefault();
        }

        public void SaveContact()
        {
            // we need the contract to be saved to xConnect. It is only in session now
            Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
            this.contactManager.SaveContactToCollectionDb(Tracker.Current.Contact);
        }

        public IXdbContext CreateContext()
        {
            return SitecoreXConnectClientConfiguration.GetClient();
        }
    }
}
  • It looks like "AuthenticationLevel " is obsolette in Sitecore 9.0 onward. Does any one know the replacement for this? Tracker.Current.Contact.Identifiers.AuthenticationLevel = AuthenticationLevel.PasswordValidated; – PaRsH Jan 3 at 8:30
  • I added the way to do it in Sitecore 9 now – Chris Auer Jan 3 at 15:41
1

Yes, it can be done through identifying contact. You can access this method using the following path:

Sitecore.Analytics.Tracker.Current.Session.Identify (identifier)

Identifiers can be user login name, email id etc.

You can refer to https://doc.sitecore.net/sitecore_experience_platform/xdb_configuration/identifying_contacts

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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