Logging activities on MVC sites

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 and registrations (referred to as MembershipController in the example code below).

  3. Initialize an instance of the IMembershipActivityLogger service.

    We recommend using a dependency injection container to initialize service instances.

    
    
    
     using CMS.Activities.Loggers;
     using CMS.Core;
    
    
     
    
    
    
         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.

      
      
      
                    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.

      
      
      
                    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 (referred to as SampleSearchController in the example code below).

  3. Initialize an instance of the IPagesActivityLogger service.

    We recommend using a dependency injection container to initialize service instances.

    
    
    
     using CMS.Core;
     using CMS.WebAnalytics;
    
    
     
    
    
    
         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.

    
    
    
                 pageActivityLogger.LogInternalSearch(searchKeywords);
    
    
    
     

The Internal search activity will now be logged when visitors use your website’s local search functionality.

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.

      MVC projects created by the installer 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: 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.

      
      
      
        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.



@using Kentico.Activities.Web.Mvc





    @* 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.