6

I am trying to create an XDB contact when someone submits a contact form.

Tracker.Current.Session.Identify(model.Email);

Above line is creating a contact with an identifier (in this case email).

So, is there any special reason to create a contact separately like mentioned in here

 public override Contact GetXdbContact(string identifier)
 {
    var contact = XdbContactRepository.LoadContactReadOnly(identifier);

    if (contact != null) return contact;

    var leaseOwner = new LeaseOwner(GetType()  + Guid.NewGuid().ToString(),LeaseOwnerType.OutOfRequestWorker);

    contact = XdbContactRepository.CreateContact(Guid.NewGuid());

    if (contact == null) return null;

    contact.Identifiers.Identifier = identifier;

    //Setting these values allows processing and aggregation to process this contact without erroring.
    contact.System.Value = 0;
    contact.System.VisitCount = 0;

    //MAKE OTHER Contact Changes here too if you need to on Create.

    XdbContactRepository.SaveContact(contact, new ContactSaveOptions(true,leaseOwner));

    return contact;
}

Which way is better?

5

@VicentGaliana is exactly right. The difference between identifying a contact and creating a contact essentially boils down to whether or not you are able to "recognize" the contact.

What is contact identification and why is it important?

Think of a contact's session like short-term memory. The contact is here, and while the contact is here you remember who they are. You can personalize them based on their interactions, etc. Once that uses leaves the site and you no longer have the contact in session, how are you supposed to know who that user was?

Identifying a contact is essentially using data that is unique to the user to remember who (s)he is on return visits. For sites that have membership, the most common indentifier that I have seen is "username". As soon as the visitor logs into the site, the contact is identified by username, causing the information from their current visit to automatically merge with information from their previous visit and enabling you to personalize their experience based on their previous behavior, as well as their current behavior.

Example

Imagine that you have membership on your website, and once a user logs in you then identify that user's contact via the following:

Tracker.Current.Session.Identify(Sitecore.Contact.User.Profile.UserName);

Every time the user returns to your site and logs in, the contact/tracking information from their current visit will combine with that of their previous visits via contact merging, enabling you to personalize the user's experience based on both their past and present contact history.

Contact creation with VisitorIdentification

Like Vicent said, you don't need to actually create contacts on your own, manually. Remember that @Html.Sitecore().VisitorIdentification() call that everyone says you need to have in the <head> of all of the pages that you want Sitecore to track on? IMHO this method should have been named GetOrCreateVisitor, or something like that. When you think about this method, think "creation" instead of "identification".

The VisitorIdentification method is basically responsible for creating the contact if this is the first visit, or else retrieving the contact (which may or may not have already been identified) from session.

More on Contact Identification and Merging

Have a look at this post (it's a bit lengthy), and scroll to the sub-section entitled "Understanding Contact Identification". There you will find a more detailed explanation of contact identification and merging.

5

TL;DR

No, just use that method, it's enough.

Long answer

In your case, Sitecore has already created a contact the first time it requested a page and tracked information against that contact. You may not see it in mongo yet if it's the first visit, but Sitecore has already created that contact in memory, as unknown and without an identifier, so there is no point creating a new one. Even more, creating a new one, you would lose all the information already tracked against the first one.

When you use the method "identify", you are telling Sitecore that you know that "person". If the system already has information about that "person" as a contact in Mongo, it will merge both, having all the information in a single contact, if not, it will simply mark this contact (The one in memory) as "known" and assign the email as identifier. Once the session ends, it will then be saved to mongo.

The example that you mentioned is designed for creating contacts from scratch, in your case, as the user is browsing the site, Sitecore already did the dirty job for you.

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.