1

What would be the best practice to save the Contact in XDB,

At present if "Contact" not exists in XDB (validating by phone + email) and it submit the "Contact-us-Form" then I am using

 Session.Abandon() 
 Session.IdentifyAs(phone,email) 

method to save contact in XDB.(my code :- https://sitecore.stackexchange.com/a/23830/1338)

By doing this I can see below facets and the goals (goal attached with Form) attached to contact in XDB + Experience Profile.

enter image description here

but if i not use

Session.Abandon() 

and use only

Session.IdentifyAs(phone,email) 

Then I am not able to get goals , interaction, engagement facets in XDB + EP (see below pic) But If I resubmit the form with new Phone and Email then these current(last)goals , interaction, engagement facets attaching to new Contact Profile

enter image description here

1

If your forms are in AJAX mode, your issue is going to be that tracker is configured to not run on Forms Ajax mode. Not sure why.

At the top of your code, add this.

if (Tracker.Current == null && Tracker.Enabled)
{
   Tracker.StartTracking();
}

Then do your normal xConnect code

var firstNameField = GetFieldById(data.FirstNameFieldId, formSubmitContext.Fields);
var lastNameField = GetFieldById(data.LastNameFieldId, formSubmitContext.Fields);
var emailField = GetFieldById(data.EmailFieldId, formSubmitContext.Fields);
if (firstNameField == null && lastNameField == null && emailField == null)
{
    return false;
}

XdbContactService.IdentifyByEmailOrAddIdentifier(GetValue(emailField));

var contactReference = this.contactIdentificationRepository.GetContactReference();

using (var client = this.contactIdentificationRepository.CreateContext())
{

    var contact = client.Get(contactReference, new ContactExpandOptions(EmailAddressList.DefaultFacetKey, PersonalInformation.DefaultFacetKey));
    if (contact == null)
    {
        throw new ArgumentNullException(nameof(contact));
    }

    var personalInformation = contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey) ?? new PersonalInformation();
    contactFacetsProvider.SetContactInfo(GetValue(firstNameField), GetValue(lastNameField), personalInformation);

    contactFacetsProvider.SetPreferredEmail(GetValue(emailField), contact, client);

    client.Submit();
    contactFacetsProvider.UpdateTracker();
}

You can find the contactIdentificationRepository here

| improve this answer | |
0

This is what I have done:

private readonly string IdentifierSource = "Subcribe.Form";
protected virtual ITracker CurrentTracker => Tracker.Current;

protected override bool Execute(XYZ data, FormSubmitContext formSubmitContext)
        {
            if (CurrentTracker == null && Tracker.Enabled)
            {
                Tracker.StartTracking();
            }
            var manager = Factory.CreateObject("tracking/contactManager", true) as ContactManager;
            if (manager != null)
            {

                using (var client = this.CreateClient())
                {
                    try
                    {
                        var firstName = GetFieldById(data.FirstNameFieldId, formSubmitContext.Fields);
                        var lastName = GetFieldById(data.LastNameFieldId, formSubmitContext.Fields);
                        var email = GetFieldById(data.EmailFieldId, formSubmitContext.Fields);
                        var mobile = GetFieldById(data.MobileFieldId, formSubmitContext.Fields);

                        var fName = GetValue(firstName);
                        var lName = GetValue(lastName);
                        var emailValue = GetValue(email);

                        string identifier = emailValue.ToLower().Trim();
                        var trackerIdentifier = new IdentifiedContactReference(IdentifierSource, identifier);

                        XConnect.Contact contact = client.Get(trackerIdentifier,
                        new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, CollectionModel.FacetKeys.EmailAddressList, PhoneNumberList.DefaultFacetKey));
                        if (contact != null)
                        {
                            SetPersonalInformation(GetValue(firstName), GetValue(lastName), contact, client);
                            SetEmail(GetValue(email), contact, client);


                        }
                        else
                        {
                            XConnect.Contact newContact = null;
                            if (CurrentTracker.Contact.IsNew)
                            {
                                CurrentTracker.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
                                manager.SaveContactToCollectionDb(CurrentTracker.Contact);
                                CurrentTracker.Session.IdentifyAs(IdentifierSource, identifier);
                                newContact = client.Get(trackerIdentifier, new ContactExpandOptions());
                            }
                            else
                            {
                                var anyIdentifier = CurrentTracker.Contact.Identifiers.FirstOrDefault();
                                Sitecore.XConnect.Contact existingContact = client.Get(new IdentifiedContactReference(anyIdentifier.Source, anyIdentifier.Identifier),
                       new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, CollectionModel.FacetKeys.EmailAddressList,  PhoneNumberList.DefaultFacetKey));
                                if (existingContact.IsKnown)
                                {
                                    CurrentTracker.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
                                    manager.SaveContactToCollectionDb(CurrentTracker.Contact);
                                    CurrentTracker.Session.IdentifyAs(IdentifierSource, identifier);
                                    newContact = client.Get(trackerIdentifier, new ContactExpandOptions());
                                }
                                else
                                {
                                    CurrentTracker.Session.IdentifyAs(IdentifierSource, identifier);
                                    newContact = client.Get(trackerIdentifier, new ContactExpandOptions());
                                }
                            }
                            if (newContact != null)
                            {
                                SetPersonalInformation(GetValue(firstName), GetValue(lastName), newContact, client);
                                SetEmail(GetValue(email), newContact, client);


                            }
                        }

                        client.Submit();
                        manager.RemoveFromSession(CurrentTracker.Contact.ContactId);
                        CurrentTracker.Session.Contact = manager.LoadContact(CurrentTracker.Contact.ContactId);
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex.Message, ex + ex.StackTrace);
                        return false;
                    }
                }
            }
            return false;
        }

| improve this answer | |

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.