Use the new tracking integration packages of Episerver

Since update 187 there are two new integration packages for tracking user data.

One for CMS and one for Commerce

You can use these packages to send tracking data to your own systems: to application insights e.g. or to the Azure Recommendations API.

For Commerce you can add an attribute [CommerceTracking] to a method or event, which tracks Commerce data based on the specified type e.g.

  • Product
  • Category
  • Basket
  • Checkout
  • Wishlist

For CMS you can add an attribute [Tracking] to a method or event, which will track only some basic things.

This post will focus on customizing/using the CMS tracking.

If you want to track more data than name, guid… you can create a custom tracking attribute that handles more data.

Say you want to track the following:

public class CmsTrackingData
    {
        public string Categories { get; set; }

        public Guid ContentGuid { get; set; }

        public ContentReference ContentLink { get; set; }

        public int ContentTypeID { get; set; }

        public string Name { get; set; }

        public ContentReference ParentLink { get; set; }

        public string Language { get; set; }

        public string Location { get; set; }
    }

In your custom tracking attribute, which you can add to a method or event instead of the default one, you could do the following:

this.trackingService.Service.Track(
    new TrackingData<CmsTrackingData>
        {
            EventType = this.TrackingType,
            PageTitle = content.Name,
            Value = content.ContentLink.ToString(),
            PageUri = httpContext.Request.Url?.AbsoluteUri ?? string.Empty,
            Payload = new CmsTrackingData
                {
                  Name = content.Name,
                  ContentGuid = content.ContentGuid,
                  ContentLink = content.ContentLink,
                  ContentTypeID = content.ContentTypeID,
                  ParentLink = content.ParentLink,
                  Language = content.LanguageBranch(),
                  Categories = this.GetCategoryNames(content, "|")
                },
            User = userData
        },
httpContext: httpContext);

You can enrich the data by using a TrackingDataInterceptor.

  • Create a class
  • Implement ITrackingDataInterceptor
  • Add [ServiceConfiguration(ServiceType = typeof(ITrackingDataInterceptor))]
public void Intercept<TPayload>(TrackingData<TPayload> trackingData)
{
    if (trackingData == null)
    {
        return;
    }

    CmsTrackingData payload = trackingData.Payload as CmsTrackingData;

    if (payload == null)
    {
        return;
    }

    payload.Location = this.GetCurrentUsersLocation()?.CountryCode ?? string.Empty;
}

The actual tracking, or in this case logging, is done by a TrackingAdapter

  • Create a class
  • Implement ITrackingAdapter
  • Add [ServiceConfiguration(ServiceType = typeof(ITrackingAdapter), Lifecycle = ServiceInstanceScope.Singleton)]
public Task Track<TPayload>(TrackingData<TPayload> trackingData, HttpContextBase httpContext)
{
    this.logger.Log(level: Level.Debug, message: trackingData.ToJson());
    Task<Task> task = Task.FromResult((Task)null);
    return task;
}

These new integration packages open a lot of possibilities ….

The complete sample is in a Gist

Happy holidays!

Advertisements

One thought on “Use the new tracking integration packages of Episerver

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s