---
title: Logging custom activities through the API
related:
  - https://docs.kentico.com/k9/on-line-marketing-features/contact-management/tracking-contact-activities/adding-custom-activities.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.

In addition to the [default activity types](https://docs.kentico.com/display/K9/Reference+-+Activity+types) used by the [contact management](https://docs.kentico.com/k9/on-line-marketing-features/contact-management.md) feature, you can also create custom activity types:

1. Open the **Contact management** application in the Kentico administration.
2. Switch to **Configuration -> Activity** **types**.
3. Click **New activity type**.
4. Fill in the display name andnote the code name of your new activity type.

![New activity type](https://docs.kentico.com/docsassets/k9/logging-custom-activities-through-the-api/New+activity+type.png "New activity type")

After you add a custom activity type, you can use the Kentico API to log the activity for contacts. You need to:

1. Open your Kentico project in Visual Studio.
2. Create a class representing your activity type. The class must inherit from **CMS.WebAnalytics.Activity**.

   > **Info:** Either add the class into a custom project within the Kentico solution (recommended) or directly into the Kentico web project (anywhere in the **CMSApp** project for _web application_ installations, into the **App\_Code** folder for _web site_ installations).

   ```csharp title="Custom activity class example"

   using CMS.WebAnalytics;

   public class MyActivity : Activity
   {
       /// <summary>
       /// Default constructor.
       /// </summary>
       /// <param name="variables">Environment variables required for proper logging of activities</param>
       public MyActivity(ActivityContext variables) : base(variables)
       {
           Data.Title = "Activity title";
       }

       /// <summary>
       /// The code name of the corresponding activity type in Kentico.
       /// </summary>
       protected override string ActivityType
       {
           get
           {
               return "MyActivity";
           }
       }

       /// <summary>
       /// The code name of the settings key that determines whether the system logs the activity.
       /// </summary>
       protected override string SettingsKeyName
       {
           get
           {
               // The code name of the "Custom activities" setting (in Settings -> On-line marketing -> Contact management -> Activities)
               return "CMSCMCustomActivities";
           }
       }
   }

   ```
3. Call the **Log** method of your activity class within the code where you want the activity to be logged.

   ```csharp title="Logging the activity"

   using CMS.WebAnalytics;

   ...

   var activity = new MyActivity(AnalyticsContext.ActivityEnvironmentVariables);
   activity.Log();

   ```

The system now automatically logs the custom activity when your custom code is triggered.
