Enabling tracking of activities on MVC sites

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 a MembershipActivitiesLogger instance in the sign-in and registration controller's constructor. This enables you to log activities.

    We recommend using a dependency injection container to initialize service instances. When configuring the lifetime scope for MembershipActivitiesLogger, create a shared instance (singleton) for all requests.

    using Kentico.Activities;

        public class MembershipController : Controller
        {
            private readonly IMembershipActivitiesLogger membershipActivityLogger;
            
            public MembershipController()
            {
                membershipActivityLogger = new MembershipActivitiesLogger();
                
                // ...
            }
            
            // ...
    

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

    • User registration activity – to log the user registration activity, call the LogRegisterActivity method of the MembershipActivitiesLogger instance. Specify the user name of the newly registered user as the method's parameter.

                  membershipActivityLogger.LogRegisterActivity(userName);
      

    • User login activity – to log the user login activity, call the LogLoginActivity method of the MembershipActivitiesLogger instance. Specify the user name of the signed in user as the method's parameter.

                  membershipActivityLogger.LogLoginActivity(userName);
      

       

After adding the code mentioned above, the User registration activity will 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.

On this page

Kentico EMS required

Features described on this page require the Kentico EMS license.

Enabling tracking of the External search and the Page related activities in MVC

For logging of the External search and the Page related activities, you need to adjust your MVC application by registering routes that receive logging requests.

Internet Explorer 8 and older versions do not support this feature. Tracking of visitors using Internet Explorer 8 and older will not work.

To ensure that the system logs every performed External search, Page visit, and Landing page activity, you need to:

  1. Open your MVC project in Visual Studio.

  2. Make sure the Kentico.Activities.Web.Mvc NuGet integration package is installed.
  3. Edit the application's App_Start\RouteConfig.cs file.

    1. Add a using statement for the Kentico.Activities.Web.Mvc namespace.
    2. Call the predefined MapActivitiesRoutes method to register routes that receive activity logging requests (typically inside the RegisterRoutes method, before you map your own custom routes).

    using System.Web.Mvc;
    using System.Web.Routing;
     
    using Kentico.Activities.Web.Mvc;
    using Kentico.Web.Mvc;
    

                // Maps routes for logging of on-line marketing campaign page visits and page-related activities
                routes.Kentico().MapActivitiesRoutes();
    

  4. Add the below code to your MVC website's main layout (view), so that the performed activities are logged for every page.

        @* Registers scripts that ensure logging of campaign page visits and page-related activities *@
        @Scripts.Render(Url.RouteUrl("KenticoLogActivityScript"))
    

After adding the code mentioned above, the External search activity will be logged every time a site visitor uses an external search engine that leads them to the website. Similarly, the Page visit activity will be logged every time the page is visited, and the Landing page activity will be logged every time a visitor opens the page when first viewing the website.


Was this page helpful?