Loading...
Area: Episerver Commerce, Episerver Tracking
Applies to versions: Tracking.Core 1.6 and higher

Product tracking

This topic describes the product-specific tracking for Episerver Commerce. Use tracking in Commerce to provide product recommendations, and for tracking data to any source for business intelligence data visualization purposes. 

Tracking

The tracking service supports product-specific tracking for Episerver Commerce. Depending on the setup, you can track Episerver Commerce data to any source, to the Episerver recommendation service (Episerver Perform) or to the Episerver Profile Store. To get tracking running for a site, you need to configure access to the tracking API. 

Installation and configuration

See Install and configure tracking about installing and setting up the tracking.

Tracking types

Use the following tracking types and attributes for tracking to any source. See also Tracking and recommendations

Visitor activities that can be tracked are contained in the TrackingType enum, shown in the following table. The Attribute Support column indicates whether you can track the activity by decorating controller actions with an attribute. See CommerceTrackingAttribute.

TrackingType enum values

TrackingType Activity Attribute Support
Home View the start page. Yes
Product View a product. Yes
Category View a branch of the catalog tree, for example Fashion>Mens>Shoes. Yes
SearchResults Search for a term. No *
Wishlist Modify the wishlist. Yes
Basket Modify the basket/shopping cart. Yes
Checkout User is about to place order. Yes
Order User placed order. No *
Other View a page not covered by other tracking types. Yes

  
SearchResults and Order tracking cannot be tracked via the CommerceTrackingAttribute because the required data is not available when the CommerceTrackingAttribute code is executed. Track these activities without using the tracking attribute.

Note: While the following tracking types are supported by the native integration API, they should not be used with the default widget setup for recommendations because they add overhead without improving recommendations.

TrackingType Activity Attribute Support
Attribute View products with a specific attribute value, for example Color: Red. No
Brand View products of a specific brand. No

Sending tracking information

Using the CommerceTrackingAttribute

The CommerceTrackingAttribute is an ActionFilterAttribute designed to be placed on controller actions. The parameter for the CommerceTrackingAttribute constructor is a TrackingType from the table above. If the passed TrackingType is supported by the CommerceTrackingAttribute, it automatically collects the necessary data and performs a tracking request in the OnActionExecuting method. In the following example, the Index method of the start page controller sends a home page tracking request when executed.

[CommerceTracking(TrackingType.Home)]
public ViewResult Index(StartPage currentPage)
{ ... }

Tracking without using the CommerceTrackingAttribute

If the TrackingType is not supported by the TrackingAttribute, or if the action to be tracked cannot be tied directly to a controller action, you need to handle tracking in a more hands-on way. In these cases, you must manually:

  1. Create the relevant TrackingData object.
  2. Pass the TrackingData object to the ITrackingService.Track extension method.

To construct TrackingData objects, call methods on TrackingDataFactory. This acts as a bridge between the Commerce system and the tracking system, accepting objects from the Commerce world as parameters and returning objects that are disconnected from Commerce. To send tracking data, call the ITrackingService Track extension method. In the following example, both TrackingDataFactory and ITrackingService are injected via constructor:

var trackingData = _trackingDataFactory.CreateCategoryTrackingData(category, httpContext);
var result = _trackingService.Track(trackingData, httpContext, currentContent);

The Track extension method also has an asynchronous version, TrackAsync, that can be awaited in asynchronous controller actions for improved performance.

Sending tracking cart with changes information

To construct the CartTrackingData object with information about its changes relative to previous cart, follow these steps.

1. Create a list of cart change data against previous cart. To do this, create a CartChangeData object. This describes changes to the cart and takes two parameters: type of change, and line item code.

var changeAction = new CartChangeData(CartChangeType.ItemAdded, itemCode);

By default, there are two types of changes in the cart defined in CartChangeType enum.

Change Type Description
ItemAdded Added item to cart
ItemRemoved Removed item from cart

The change type is a string property. You can create a custom type by passing a string in the constructor, like this.

var changeAction = new CartChangeData("reasonforchangecart", itemCode);

Also, you can add information to describe the change detail for the action by adding information to changes dictionary property.

changeAction.SetChange("oldPrice", oldPlacedPrice);

changeAction.SetChange("oldSize", oldSize);

Finally, create a list of change data objects to create the CartTrackingData object.

var cartChanges= new List<CartChangeData>(changeAction);

2. Create the CartTrackingData object by calling a method on TrackingDataFactory, with a list of the change objects you constructed above.

var _trackingDataFactory = ServiceLocator.GetInstance<TrackingDataFactory >();

var cartTrackingData= _trackingDataFactory.CreateCartTrackingData(httpContext, cartChanges);

3. Pass the CartTrackingData object to the ITrackingService Track extension method.

var _trackingService = ServiceLocator.GetInstance<ITrackingService>();

await _trackingService.TrackAsync(cartTrackingData, httpContext, currentContent);

Note: The change information of CartTrackingData is not sent to Perform, since it doesn't understand that data.

Related topics

Last updated: Apr 23, 2019

Feedback?