Description :
Registers a tracker in the application. A tracker enables you to listen for one or more events in the application – such as navigation, page views, and transactions – and then perform an action in response to those events. Trackers are especially useful if you want to integrate with third-party services that track user activity or behavior in a web store.
Note: If you want to track other events in the application, you can create custom events and trigger those events with triggerEvent().
In an extension, you can use any of the following predefined listener functions to listen for application events:
trackAddToCart – Called when a line is added to the cart, or the quantity of an item changes.
trackCartUpdate – Called when a line is added to the cart.
trackPageview – Called when a page is viewed.
trackProductView – Called when a product details page (PDP) is viewed.
trackTransaction – Called when a transaction is completed.
To register a tracker in the application, create an object with one (or several) of the predefined listener functions as an object method. Add whatever logic you require in the method – all the listener functions provide event data which you can utilise in your logic. Then call the addTracker() method to register the tracker.
In the following example, the tracker object myTracker defines a trackAddToCart function, which will be called when an item is added to the cart. In the function, we call the logShoppingEvent() method of the _exampleTrackingService object, which belongs to a fictitious third-party service. In the logShoppingEvent() method, we pass in a tracking identifier or trigger (in this case, addToCart), and some item information from the event data. To register the tracker in our extension, we call addTracker() with myTracker as the argument. Now, whenever a user adds an item to the cart, it is tracked in the third-party service.
var environment = container.getComponent("Environment");
var myTracker = {
trackAddToCart: function(line) {
_exampleTrackingService.logShoppingEvent(
'addToCart',
{
name: line.item.displayname,
sku: line.item.itemid,
amount: line.amount,
quantity: line.quantity
}
);
}
}
environment.addTracker(myTracker);
Note: The method of implementation in the example above is a simplification of how to integrate with a third-party website service. Third-party services and agents have different requirements and methods of implementation, which you should familiarise yourself with beforehand.
Parameters
| Name | Type | Description |
|---|---|---|
Tracker | Object | NameTypeAttributesDescriptiontrackAddToCartfunction<optional>A function that is called when a line is added to the cart, or the quantity of a line changes. Provides a Line object in its event data. trackCartUpdatefunction<optional>A function that is called when a line is added to the cart. Provides an array of Line objects in its event data corresponding to the items currently in the cart. trackPageviewfunction<optional>A function that is called when a page in the application is viewed. Provides a PageViewInfo object in its event data. trackProductViewfunction<optional>A function that is called when a product details page in the application is viewed. Provides a ProductViewInfo object in its event data. trackTransactionfunction<optional>A function that is called when a transaction is completed. Provides a TransactionInfo object in its event data. |