---
title: Logging custom activities through the API
related:
  - https://docs.kentico.com/13/on-line-marketing-features/configuring-and-customizing-your-on-line-marketing-features/configuring-activities/adding-custom-activity-types.md
  - https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/tracking-contact-activities/manually-logging-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:** **Enterprise license required**
>
> Features described on this page require the **Kentico Xperience Enterprise** license.

In addition to the [default activity types](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/tracking-contact-activities/reference-activity-types.md) used by the [contact management](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management.md) feature, you can also create custom activity types:

> **Note:** **Note**
>
> Only users with the global administrator [privilege level](https://docs.kentico.com/13/managing-users/user-management.md) can define custom activity types.

1. Open the **Contact management** application.
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/13/logging-custom-activities-through-the-api/New+activity+type.png "New activity type")

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

1. Open your project in Visual Studio.
2. Create an initializer class for your activity type. The class must inherit from **CMS.Activities.CustomActivityInitializerBase**.

   > **Info:** Add the class into a custom _Class Library_ within your solution.

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

   using CMS.Activities;

   public class MyActivityInitializer : CustomActivityInitializerBase
   {
       private readonly string activityValue;

       /// <summary>
       /// Default constructor that takes activity data as parameters.
       /// </summary>
       public MyActivityInitializer(string activityValue)
       {
           this.activityValue = activityValue;
       }

       /// <summary>
       /// Initializes an activity with required data.
       /// </summary>
       public override void Initialize(IActivityInfo activity)
       {
           activity.ActivityTitle = "My custom activity title";
           activity.ActivityValue = activityValue;
       }

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

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

   ```csharp title="Logging the activity"

   using CMS.Core;
   using CMS.Activities;
   using CMS.Helpers;

   ...

   // Gets an instance of the activity logging service
   // In real-world scenarios, we recommend using dependency injection to get service instances
   var service = Service.Resolve<IActivityLogService>();

   // Prepares an initializer for logging the activity
   var activityInitializer = new MyActivityInitializer("value");

   // Logs the activity
   service.Log(activityInitializer);

   ```

   > **Tip:** **Tip**: If  you need to log your activity in code where the HTTP request context is not available (for example in certain types of custom [scheduled tasks](https://docs.kentico.com/13/configuring-xperience/scheduling-tasks/scheduling-custom-tasks.md)), call the **LogWithoutModifiersAndFilters(IActivityInitializer activityInitializer)** method instead of _Log_.

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