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:
- Enable on-line marketing in the Settings application of the related Kentico instance.
- 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).
// Gets the current contact ContactInfo currentContact = ContactManagementContext.GetCurrentContact();
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:
Open your MVC project in Visual Studio.
Edit the controllers that process sign-ins and registrations.
- Call the ContactManagementContext.UpdateUserLoginContact method in the controller's sign-in and registration actions.
Pass the user name of the signed in or registered user via the method's parameter (represented by the userName variable in the code example).
Call the method only after a successful sign-in or registration.
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)
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.