HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Product tracking

Describes the product-specific tracking for Optimizely 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 Optimizely Commerce. Depending on the setup, you can track Optimizely Commerce data to any source, to Optimizely Product Recommendations or to the Optimizely Profile Store. To get tracking running for a site, you need to configure access to the tracking API.

Install and configure

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.

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

TrackingTypeActivityAttribute Support
HomeView the start page.Yes
ProductView a product.Yes
CategoryView a branch of the catalog tree, for example Fashion>Mens>Shoes.Yes
SearchResultsSearch for a term.No *
WishlistModify the wishlist.Yes
BasketModify the basket/shopping cart.Yes
CheckoutUser is about to place order.Yes
OrderUser placed order.No *
OtherView 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 product recommendations because they add overhead without improving product recommendations.

TrackingTypeActivityAttribute Support
AttributeView products with a specific attribute value, for example Color: Red.No
BrandView products of a specific brand.No

Send tracking information

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)
{ ... }

Track 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.

Send tracking cart with changes information

[New in Commerce version 13.2.0 and EPiServer.Tracking.Commerce version 3.1.0 ]

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 TypeDescription
    ItemAddedAdded item to cart
    ItemRemovedRemoved 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 Product Recommendations, because it does not understand that data.