Activities represent actions that your contacts perform. In addition to the default activity types, you can also define custom activity types, which allow you to track any other required actions.
Marketers can view custom activities in the standard activity log for contacts. You can also segment contacts according to custom activities by preparing contact groups with activity-based conditions. The contact groups can then be used to personalize content on your websites.
For example, a custom activity could be used on a bank's website in the following scenario. The site uses a custom Page Builder widget to display a loan calculator. The widget logs a custom "Loan calculator usage" activity whenever a visitor runs the calculator. A contact group stores all contacts who have performed this custom activity, which marketers can use to send targeted loan offers and display personalized content on other pages.
Open the Contact management application in the Xperience administration.
Switch to the Activity types tab.
Select New activity type.
Fill in the fields:
Select Save.
Your custom activity type is now ready to be logged.
After defining custom activity types, developers need to set up the system to log the activities when the corresponding visitor actions occur. The following options are available:
Tracking consent required
Due to personal data protection policies, custom activities on website channels are only logged for visitors who give consent to be tracked as contacts and have the required cookie level set (Visitor or All).
You can log custom activities using the Xperience server-side API. For example, this approach can be used in the code of Page Builder components.
Log method of the ICustomActivityLogger service (e.g., obtained using dependency injection).customActivityType – the Code name of the activity type.activityData – a CustomActivityData object that specifies the properties of the logged activity. We recommend setting at least the ActivityTitle (displayed in the activity log). Optionally, you can set a string as the ActivityValue for every logged activity.using CMS.Activities;
// Stores an instance of the ICustomActivityLogger service (e.g., obtained using dependency injection)
private readonly ICustomActivityLogger customActivityLogger;
..
// Prepares the activity data (e.g., using the loan size as the activity value)
var loanCalculatorActivityData = new CustomActivityData() {
ActivityTitle = "Loan calculator usage",
ActivityValue = "50000"
};
// Logs the 'custom_loan_calculator' activity for the current contact
customActivityLogger.Log("custom_loan_calculator", loanCalculatorActivityData);The activities are now logged when your custom code is triggered.
To track some types of visitor actions, you may wish to log custom activities using JavaScript on the client side. This can be useful for basic interactions with important elements on your pages, for example when a visitor clicks a "call to action" button or link.
As a prerequisite, you need to register an activity logging script onto your pages.
Make sure the Html.Kentico().ActivityLoggingScriptV2 extension method is called on all pages where you wish to log custom activities – either in your site's main layout, or in the views of individual pages. We recommend adding the script at the end of the <head> tag in the page code. The method's logCustomActivity parameter must be true. This is the default state if you call the method without any parameters.
@using Kentico.Activities.Web.Mvc
@* Registers the script that allows custom activity logging *@
@Html.Kentico().ActivityLoggingScriptV2()You can then log custom activities in your scripts by calling the kxt('customactivity') function. Specify the following parameters for the logged activity:
type – the Code name of the activity type.valuetitle<button id="btnCTA" onclick="logCtaClickActivity();">See our special offers!</button>
...
<script type="text/javascript">
// Click handler for the CTA button, which logs a custom activity
var logCtaClickActivity = function logCtaClickActivity() {
kxt('customactivity', {
type: 'custom_cta',
value: 'btnCTA', // Stores the id of the clicked button element as the activity value
title: 'Custom CTA click'
persona: admin, developer
});
}
</script>The kxt('customactivity') function sends a request that logs the specified custom activity for the current contact. These activity logging requests are handled by a dedicated endpoint, which is added by calling the UseActivityTracking method in your Xperience application's startup code (see Set up activities).
If you encounter script conflicts with the kxt function (for example when using custom or third-party JavaScript libraries), you can rename the function by setting a custom name in the loggingFunctionName parameter of the ActivityLoggingScriptV2 registration method.
@* Changes the name of the custom activity logging function to 'customActivityTracker' *@
@Html.Kentico().ActivityLoggingScriptV2(loggingFunctionName: "customActivityTracker")
...
<script type="text/javascript">
customActivityTracker('customactivity', {
type: 'custom_cta',
value: 'btnCTA',
title: 'Custom CTA click'
persona: admin, developer
});
</script>You can log custom activities through the headless tracking API.
Make sure the tracking API is enabled.
Consider and evaluate consent requirements for the current contact.
Send a POST request from your external application to the /Kentico.Tracking/activities endpoint. The following example demonstrates the data structure required for the request body:
{
"Channel": "df6721ee-2562-4ef3-ae0f-9d733458550a",
"Contact": "052fff6b-cedb-4757-b939-b5cbee2a7aff",
"Activities": [
{
"Type": "custom_loan_calculator",
"Value": "5000"
}
]
}You can log custom activities from external sites covered by cross-site tracking. Call the kxt('customactivity') function in your cross-site tracking scripts.
// Logs a custom activity
kxt('customactivity', {
// The 'type' parameter must match the code name of the custom activity type
type: 'custom_loan_calculator',
value: 'value'
});