I am trying to save the data to the Experience Profile
when the user submits the form, but the data is not getting displayed in the dashboard.
Below is my code for storing data:
public void IdentityUser(string FirstName, string LastName, string Email, string PhoneNumber)
{
// identify the user
Tracker.Current.Session.IdentifyAs("contactform",Email);
// get the contact
var contact = Tracker.Current.Contact;
// get the personal facet
var contactPersonalInfo = contact.GetFacet<IContactPersonalInfo>("Personal");
// set the contact's name
contactPersonalInfo.FirstName = FirstName;
contactPersonalInfo.Surname = LastName;
// get the email facet
var contactEmail = contact.GetFacet<IContactEmailAddresses>("Emails");
// Create an email if not already present.
// This can be named anything, but must be the same as "Preferred" if you want
// this email to show in the Experience Profiles backend.
if (!contactEmail.Entries.Contains("Personal"))
{
contactEmail.Entries.Create("Personal");
}
// set the email
var email = contactEmail.Entries["Personal"];
email.SmtpAddress = Email;
contactEmail.Preferred = "Personal";
IContactPhoneNumbers phoneNumberInfo = contact.GetFacet<IContactPhoneNumbers>("Phone Numbers");
if (!phoneNumberInfo.Entries.Contains("Mobile"))
{
phoneNumberInfo.Entries.Create("Mobile");
}
var mobile = phoneNumberInfo.Entries["Mobile"];
mobile.Number = PhoneNumber;
//abandoning session so the data get persisted straight into xDb. I have tried removing them as well no effect
Sitecore.Context.HttpContext.Session.Abandon();
HttpContext.Current.Session.Abandon();
}
The Experience Profile looks like this:
I have updated my code as below after comments:
public void TrackAnonymousUser(string FirstName, string LastName, string Email, string PhoneNumber)
{
if (Sitecore.Analytics.Tracker.Current.Contact.IsNew)
{
var manager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as Sitecore.Analytics.Tracking.ContactManager;
if (manager != null)
{
// Save contact to xConnect; at this point, a contact has an anonymous
// TRACKER IDENTIFIER, which follows a specific format. Do not use the contactId overload
// and make sure you set the ContactSaveMode as demonstrated
Sitecore.Analytics.Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
manager.SaveContactToCollectionDb(Sitecore.Analytics.Tracker.Current.Contact);
// Now that the contact is saved, you can retrieve it using the tracker identifier
// NOTE: Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource is marked internal in 9.0 Initial - use "xDB.Tracker"
var trackerIdentifier = new IdentifiedContactReference("ContactForm", Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString("N"));
// Get contact from xConnect, update and save the facet
using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
{
try
{
//contact is null
var contact = client.Get<Sitecore.XConnect.Contact>(trackerIdentifier, new Sitecore.XConnect.ContactExpandOptions());
if (contact != null)
{
if (contact.Personal() != null)
{
contact.Personal().FirstName = FirstName;
contact.Personal().LastName = LastName;
}
else
{
client.SetFacet<PersonalInformation>(contact, "MyFacetKey", new PersonalInformation()
{
FirstName = FirstName,
LastName = LastName,
});
}
if (contact.PhoneNumbers() != null)
{
contact.PhoneNumbers().PreferredPhoneNumber = new Sitecore.XConnect.Collection.Model.PhoneNumber(string.Empty, PhoneNumber);
}
else
{
client.SetFacet<PhoneNumberList>(contact, "MyFacetKey", new PhoneNumberList(new PhoneNumber(string.Empty, PhoneNumber), "mobile"));
}
if (contact.Emails() != null)
{
contact.Emails().PreferredEmail = new EmailAddress(Email, true);
}
else
{
client.SetFacet<EmailAddressList>(contact, "MyFacetKey", new EmailAddressList(new EmailAddress(Email, true), "email"));
}
client.Submit();
// Remove contact data from shared session state - contact will be re-loaded
// during subsequent request with updated facets
//manager.RemoveFromSession(Sitecore.Analytics.Tracker.Current.Contact.ContactId);
}
}
catch (XdbExecutionException ex)
{
// Manage conflicts / exceptions
}
}
}
}
}
The problem is that my contact object is null even after pushing it to the xConnect repository. I can see in the comments as below:
// Now that the contact is saved, you can retrieve it using the tracker identifier
// NOTE: Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource is marked internal in 9.0 Initial - use "xDB.Tracker"
var trackerIdentifier = new IdentifiedContactReference("ContactForm", Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString("N"));
I am not too sure where to find xDB.Tracker I can't seem to have reference to it. Is that the reason for the contact is null?
Furthermore, I see the following in the xConnect logs
:
2018-02-14 09:37:26.897 +10:00 [Error] Sitecore.XConnect.Operations.AddContactOperation: Sitecore.XConnect.Operations.EntityOperationException: Operation #0, AlreadyExists, Contact 2018-02-14 09:37:26.897 +10:00 [Error] Sitecore.XConnect.Operations.SetFacetOperation`1[Sitecore.XConnect.Facet]: Sitecore.XConnect.Operations.FacetOperationException: Operation #1, ReferenceNotFound, Contact, Classification 2018-02-14 09:37:26.897 +10:00 [Error] ["XdbContextLoggingPlugin"] XdbContext Batch Execution Exception Sitecore.XConnect.Operations.EntityOperationException: Operation #0, AlreadyExists, Contact