AI agents: the content index is at /llms.txt. Every page is also available as Markdown — send the request header Accept: text/markdown, or append .md to the page URL.
Xperience Docs

Cookie consent

We use necessary cookies (opens in a new tab) to run our website and improve your experience while browsing. Additional cookies are only used with your consent. You may revoke your consent on the Cookies Policy (opens in a new tab) page or in your browser at any time.

Configure (opens in a new tab)
  • Documentation
    InstallationUsing the Kentico interfaceDeveloping websitesConfiguring KenticoManaging usersSecuring websitesDeploying websitesManaging website contentMultilingual websitesMacro expressionsExternal utilitiesIntegrating 3rd party systemsCustom developmentE-commerce featuresOn-line marketing featuresCommunity featuresRunning Kentico on Microsoft AzureUsing Amazon web services with KenticoCertified extensionsReferencesRelease notes - Kentico 10
  • Tutorial
    Getting started with KenticoUsing the Kentico interfaceSite development overviewCreating a new websiteAdding styles to the websiteWorking with the Pages applicationCreating the master pageCreating website pagesCreating page templatesNext steps
  • API Examples
    Content managementOn-line marketingE-commerceSocial and CommunityDevelopmentConfiguration

Is this really the version you are looking for? Kentico Xperience 10 Documentation is no longer updated. Use the switcher in the header to choose your product version.

  1. Home
  2. Developing websites
  3. Developing sites using ASP.NET MVC
  4. Implementing on-line marketing features on MVC sites
  5. Tracking contacts on MVC sites

Tracking contacts on MVC sites


Go back to top
  • Community Questions & Answers (opens in a new tab)
  • Contact support (opens in a new tab)

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. Install the Kentico.ContactManagement NuGet integration package into your MVC project.
  2. Enable on-line marketing in the Settings application of the related Kentico instance.
  3. Assign existing contacts to registered or signed-in users to make contact tracking effective.

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 updates 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.

Whenever you need to get the current contact during the implementation of an MVC website, use the asynchronous GetCurrentContactAsync method that returns a ContactInfo instance. The GetCurrentContactAsync method is available in the ContactTrackingService class, which is provided by the Kentico.ContactManagement integration package. Specify the user name (string userName) of the currently signed-in user as the parameter (if there is a signed-in user). If the user name does not exist, the system creates a new contact for the given user name.

C#
using System.Web.Mvc;

using CMS.ContactManagement;
using Kentico.ContactManagement;
C#Example
// Gets the current contact
IContactTrackingService contactService = new ContactTrackingService();
ContactInfo currentContact = contactService.GetCurrentContactAsync(User.Identity.Name).Result;

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. Call the MergeUserContactsAsync method from the ContactTrackingService class to: 

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

To ensure that every registered user gets their own permanent contact and every signed-in user is assigned their permanent contact:

  1. Open your MVC project in Visual Studio.

  2. Edit the controllers that process sign-ins and registrations (referred to as MembershipController in the example code below).

  3. Initialize a ContactTrackingService instance in the sign-in and registration controller's constructor. This enables you to work with contacts.

    We recommend using a dependency injection container to initialize service instances. When configuring the lifetime scope for ContactTrackingService, create a separate instance for each request.

    C#
    using Kentico.ContactManagement;
    C#
    public class MembershipController : Controller
    {
        private readonly IContactTrackingService contactTrackingService;
    
        public MembershipController()
        {
            contactTrackingService = new ContactTrackingService();
    
            // ...
        }
    
        // ...
  4. Call the MergeUserContactsAsync method of the ContactTrackingService instance in the controller's sign-in and registration actions.

    • Call the method after a successful sign-in or registration.

    • The userName variable in the code example represents the user name of the visitor that signs in or registers.

      C#
      await contactTrackingService.MergeUserContactsAsync(userName);

For every registered user, the system permanently assigns their current contact. For every signed-in user, the system merges the current contact with the given user's contact (if the current contact does not belong to a different user).

Merging contacts

In some cases, multiple contacts⁠ (opens in a new tab) 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.  

On this page

  • Obtaining the current contact
  • Assigning the related contact to registered users
  • Merging contacts

Related pages

  • Contact management
  • Working with contacts
  • Merging contacts
  • Developing MVC applications
  • Developing sites using ASP.NET MVC