Module: Activities and contacts
4 of 9 Pages
Log custom activities from the client side
Write logging javascript
The client-side activity logging example from the documentation calls a function from the onclick attribute of a specific button.
This example uses a similar function, but stores it in a separate file rather than inline, and dynamically registers it to any links with the downloadattribute. Make sure to include this attribute on any downloadable file links that you want to track.
- Add a new file called FileDownloadActivityLogger.js to the ~/wwwroot/assets/js folder of the TrainingGuides.Web project.
- Create a function called
handleClick
that logs a custom activity as outlined in the documentation example.- Assign the filedownload custom activity type.
- Set the value to the path of the current page.
- Assign a meaningful title that includes the
alt
attribute of the specific download link if it exists.
- In the
window.onload
event, iterate through all the links on the page, and assign thehandleClick
function to the click event of any links with the download attribute present.
window.onload = function () {
const links = document.getElementsByTagName("a");
for (let i = 0; i < links.length; i++) {
if (links[i].hasAttribute("download")) {
links[i].addEventListener("click", handleClick);
}
}
}
function handleClick() {
kxt('customactivity', {
type: 'filedownload',
value: window.location.pathname,
title: 'File downloaded - ' + this.getAttribute("alt"),
persona: developer
onerror: t => console.log(t)
});
}
Create a view component
Following the same process as earlier in this series, create a view component class to conditionally render a script reference to your new JavaScript file to the page.
Add a folder called CustomActivityScripts in TrainingGuides.Web/Features/Activities/ViewComponents.
Create a view component to conditionally render the custom activity script if the current contact has consented to tracking.
C#CustomActivityScriptsViewComponent.csusing Microsoft.AspNetCore.Mvc; using TrainingGuides.Web.Features.Activities.ViewComponents.Shared; using TrainingGuides.Web.Features.DataProtection.Services; namespace TrainingGuides.Web.Features.Activities.ViewComponents.CustomActivityScripts; public class CustomActivityScriptsViewComponent : ViewComponent { private readonly ICookieConsentService cookieConsentService; public CustomActivityScriptsViewComponent(ICookieConsentService cookieConsentService) { this.cookieConsentService = cookieConsentService; } public IViewComponentResult Invoke() { var model = new ContactTrackingAllowedViewModel() { ContactTrackingAllowed = cookieConsentService.CurrentContactCanBeTracked() }; return View("~/Features/Activities/ViewComponents/CustomActivityScripts/CustomActivityScripts.cshtml", model); } }
Add a view that renders a script tag for your FileDownloadActivityLogger.js file.
cshtmlCustomActivityScripts.cshtml@using TrainingGuides.Web.Features.Activities.ViewComponents.Shared @model ContactTrackingAllowedViewModel @if (Model.ContactTrackingAllowed) { @*Scripts for logging custom activities*@ <script src="~/assets/js/FileDownloadActivityLogger.js"></script> }
Add the view component to the Layout view
The script is ready to be added to the layout view. To make the script work, you need to add Xperience activity logging API to the page, along with a reference to the javascript file from the previous section.
Navigate to ~Views/Shared/_Layout.cshtml in the TrainingGuides.Web project, and add a using directive for your new view component.
cshtml_Layout.cshtml... @using TrainingGuides.Web.Features.Activities.ViewComponents.CustomActivityScripts ...
At the bottom of the body tag, use the tag helper to invoke the
custom-activity-scripts
view component.cshtml_Layout.cshtml... <vc:custom-activity-scripts/> </body> ...
If you only expect to have download links in certain parts of your site, you can improve performance by using Razor sections to only include these scripts for views which you know will show downloads.
Test the code
The coding is done, so the functionality is ready to be tested.
Run the TrainingGuides.Web project on your dev machine and visit the site in your browser.
Check your browser cookies in your browser’s developer tools, and ensure that the CMSCookieLevel cookie is set to 1000 or above.
If you’ve completed the consent-related training guides, go to the /cookie-policy page and set your preferred cookie level to Marketing.
Visit the /policy-downloads page and click to download the privacy policy PDF.
Visit the /adminpath to log in to the administration interface.
Navigate to the Activities tab of the Contact management application to see that the download activity has been logged.