Tracking contacts on MVC sites

Contacts represent website visitors and store marketing-related information about them. To learn more about contacts, see Working with contacts.

To track contacts on websites presented by a separate MVC application, you need to:

  1. Enable on-line marketing in the Settings application of the related Kentico instance.
  2. Assign existing contacts to registered or signed-in users to make contact tracking effective.

Default cookie level or consent requirements

The contact tracking functionality in Kentico EMS only works if the Default cookie level setting is set to Visitor or All, or for visitors who give tracking consent and increase their cookie level. For more information, see Working with consents on MVC sites.

Contact detail updates are batch processed and logged at an interval together with activities. To learn how to change the logging interval, see Configuring activity and contact update processing.

Obtaining the current contact

The system uses a browser cookie as persistent storage for the current contact. By default, the storage is empty. However, once you ask for the current contact, the system sets the contact's GUID (unique identifier) to every response cookie.

To get the current contact in the implementation of an MVC website, call the ContactManagementContext.GetCurrentContact method. The context class and method are available in the CMS.ContactManagement namespace (part of the API provided by the Kentico.Libraries NuGet package).

using CMS.ContactManagement;

Example
            // Gets the current contact
            ContactInfo currentContact = ContactManagementContext.GetCurrentContact();
If there is no contact related to the currently processed request, the GetCurrentContact method automatically creates and returns a new anonymous contact by default. You can disable the automatic creation of new contacts by calling the method with a false bool parameter.

Tip: If you wish to use the dependency injection design pattern on your website, you can create a custom service to provide the required contact management functionality. Call the methods of the ContactManagementContext class within the service's implementation.

Assigning the related contact to registered users

For effective tracking of contacts, you need each registered user to use the same contact for all their visits. To ensure that registered users correctly use a permanent contact:

  • Assign a contact to users immediately after registration
  • Switch a visitor's contact to their user's contact after the visitor signs in

Use the following steps to implement the required functionality on your MVC site:

  1. Open your MVC project in Visual Studio.

  2. Edit the controllers that process sign-ins and registrations.

  3. Call the ContactManagementContext.UpdateUserLoginContact method in the controller's sign-in and registration actions.
    • using CMS.ContactManagement;

                  ContactManagementContext.UpdateUserLoginContact(userName);
      

The UpdateUserLoginContact method performs the following functions:

  • Permanently assigns the current contact to the specified user (for newly registered users)
  • If the user already has a contact assigned, merges the current contact into it (as long as the current contact does not belong to a different user)
  • Updates the contact cookie in the visitor's browser to the correct contact (if necessary)

Merging contacts

In some cases, multiple contacts on your websites may actually represent a single real‑world person. Automatic merging allows you to get rid of duplicates by combining several contacts into a single object. The system automatically merges contacts that are associated with the same email address.  


Was this page helpful?