---
title: Tracking contacts on MVC sites
related:
  - https://docs.kentico.com/k10/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management.md
  - https://docs.kentico.com/k10/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/working-with-contacts.md
  - https://docs.kentico.com/k10/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/working-with-contacts/merging-contacts.md
  - https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications.md
  - https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

Contacts represent website visitors and store marketing-related information about them. To learn more about contacts, see [Working with contacts](https://docs.kentico.com/k10/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/working-with-contacts.md).

To track contacts on websites presented by a [separate MVC application](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc.md), you need to:

1. Install the **Kentico.ContactManagement** NuGet [integration package](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) 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](#assigning-the-related-contact-to-registered-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](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/implementing-on-line-marketing-features-on-mvc-sites/logging-activities-on-mvc-sites.md#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.

```csharp

using System.Web.Mvc;

using CMS.ContactManagement;
using Kentico.ContactManagement;

```

```csharp title="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](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/working-with-users-on-mvc-sites.md) and [registrations](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/working-with-users-on-mvc-sites/user-registration-on-mvc-sites.md) (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.

   > **Tip:** We recommend using a [dependency injection container](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/initializing-kentico-services-with-dependency-injection.md) to initialize service instances. When configuring the lifetime scope for _ContactTrackingService_, create a separate instance for each request.

   ```csharp

   using Kentico.ContactManagement;

   ```

   ```csharp

       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](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/working-with-users-on-mvc-sites.md) and [registration](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/working-with-users-on-mvc-sites/user-registration-on-mvc-sites.md) 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.

     ```csharp

                 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](https://docs.kentico.com/display/K10/Working+with+contacts) on your websites may actually represent a single real‑world person. [Automatic merging](https://docs.kentico.com/k10/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/working-with-contacts/merging-contacts.md) 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.  
