Use tracking in Commerce to provide product recommendations, and for tracking data to any source for business intelligence data visualization purposes.
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
|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.
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.
|Attribute||View products with a specific attribute value, for example _Color: Red_.||No|
|Brand||View products of a specific brand.||No|
### Send tracking information
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.
#### 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:
Create the relevant TrackingData object.
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:
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.
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.
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.
Also, you can add information to describe the change detail for the action by adding information to changes dictionary property.
Finally, create a list of change data objects to create the CartTrackingData object.
Create the CartTrackingData object by calling a method on TrackingDataFactory, with a list of the change objects you constructed above.
Pass the CartTrackingData object to the ITrackingService Track extension method.
The change information of CartTrackingData is not sent to Product Recommendations, because it does not understand that data.