---
title: Logging activities on MVC sites
related:
  - https://docs.kentico.com/k12sp/on-line-marketing-features/configuring-and-customizing-your-on-line-marketing-features/configuring-activities/enabling-activity-tracking-on-mvc-sites.md
  - https://docs.kentico.com/k12sp/managing-users/user-registration-and-authentication/integrating-kentico-membership.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).

> **Info:** **Kentico EMS required**
>
> Features described on this page require the **Kentico EMS** license.

This page provides detailed instructions on logging of activities that require additional adjustments of your MVC application.

## Logging membership activities

To ensure that the system logs every performed _Membership_ related activity, such as _User registration_ and _User login_, you need to:

1. Open your MVC project in Visual Studio.
2. Edit the controllers that process  [sign-ins](https://docs.kentico.com/k12sp/managing-users/user-registration-and-authentication/setting-up-authentication.md)  and [registrations](https://docs.kentico.com/k12sp/managing-users/user-registration-and-authentication/enabling-user-registration.md) (referred to as  _MembershipController_  in the example code below).
3. Initialize an instance of the **IMembershipActivityLogger** service.

   > **Tip:** We recommend using a [dependency injection container](https://docs.kentico.com/k12sp/developing-websites/initializing-kentico-services-with-dependency-injection.md) to initialize service instances.

   ```csharp

   using CMS.Activities.Loggers;
   using CMS.Core;

   ```

   ```csharp

       public class MembershipController : Controller
       {
           private readonly IMembershipActivityLogger membershipActivityLogger;

           public MembershipController()
           {
               membershipActivityLogger = Service.Resolve<IMembershipActivityLogger>();

               // ...
           }

           // ...


   ```
4. Add code according to the activity types described below:

   - **User registration activity** – to log the user registration activity, call the _LogRegistration_ method of the _IMembershipActivityLogger_ instance. Specify the user name of the newly registered user as the method's parameter.

     ```csharp

                 membershipActivityLogger.LogRegistration(userName);


     ```
   - **User login activity** – to log the user login activity, call the _LogLogin_ method of the _IMembershipActivityLogger_ instance. Specify the user name of the signed in user as the method's parameter.

     ```csharp

                 membershipActivityLogger.LogLogin(userName);


     ```

The _User registration_ activity will now be logged for every user that registers as a site member, and the _User login_ activity will be logged for every user that signs into the site.

## Logging the Internal search activity

To ensure that your website logs the _Internal search_ activity, you need to:

1. Open your MVC project in Visual Studio.
2. Edit the [controller thatprocesses search requests](https://docs.kentico.com/k12sp/configuring-kentico/setting-up-search-on-your-website/using-locally-stored-search-indexes/building-a-search-interface-for-local-indexes-in-mvc.md)   (referred to as  _SampleSearchController_  in the example code below).
3. Initialize an instance of the **IPagesActivityLogger** service.

   > **Tip:** We recommend using a [dependency injection container](https://docs.kentico.com/k12sp/developing-websites/initializing-kentico-services-with-dependency-injection.md) to initialize service instances.

   ```csharp

   using CMS.Core;
   using CMS.WebAnalytics;

   ```

   ```csharp

       public class SampleSearchController : Controller
       {
           private readonly IPagesActivityLogger pageActivityLogger;

           public SampleSearchController()
           {
               pageActivityLogger = Service.Resolve<IPagesActivityLogger>();

               // ...
           }

           // ...


   ```
4. Call the **LogInternalSearch** method of the **IPagesActivityLogger** instance in the action that handles search requests. Specify the submitted search keywords as the method's parameter.

   ```csharp

               pageActivityLogger.LogInternalSearch(searchKeywords);


   ```

The _Internal search_ activity will now be logged when visitors use your website's local search functionality.

## Logging page related activities

For logging of the _External search_ and the _Page_ related activities, you need to enable the activity tracking feature for your MVC application.

1. Open your MVC project in Visual Studio.
2. Enable the activity tracking feature by calling the **UseActivityTracking** method of the _ApplicationBuilder_ instance.

   - Enable the feature at the start of your application's life cycle, for example in the **Application\_Start** method of your project's **Global.asax** file.

     > **Info:** MVC projects created by the [installer](https://docs.kentico.com/k12sp/installation/installing-kentico-mvc-projects.md) contain the _ApplicationConfig_ class, whose _RegisterFeatures_ method is called in the _Application\_Start_ method by default. You can use this class to encapsulate all of your _ApplicationBuilder_ code (enabling and configuring of Kentico MVC features).

     > **Note:** **Note**: The feature must be enabled **before** you register routes into the application's _RouteTable_. The _Kentico().MapRoutes()_ method adds required routes based on the set of enabled features.

     ```csharp

     using Kentico.Activities.Web.Mvc;
     using Kentico.Web.Mvc;

     ...

     protected void Application_Start()
     {
         ...

         // Gets the ApplicationBuilder instance
         // Allows you to enable and configure selected Kentico MVC integration features
         ApplicationBuilder builder = ApplicationBuilder.Current;

         // Enables the activity tracking feature
         builder.UseActivityTracking();

         ...
     }

     ```
3. Register the activity logging script onto all pages by calling the **ActivityLoggingScript** extension method in views.
   - Call the method in your MVC website's main layout (and any other used layouts) to ensure that performed activities are logged for every page.

```csharp

@using Kentico.Activities.Web.Mvc

```

```csharp

    @* Registers scripts that ensure logging of campaign page visits and page-related activities *@
    @Html.Kentico().ActivityLoggingScript()


```

Enabling the _UseActivityTracking_ feature ensures that the system registers routes that receive activity logging requests.

After adding the code mentioned above, the _Page visit_ activity is logged every time a page is visited, and the _Landing page_ activity is logged every time a visitor opens a page when first viewing the website. Similarly, the _External search_ activity is logged every time a site visitor uses an external search engine that leads them to the website.
