This page contains a guide to integrate Add to Cart on product page.
Tracking the Add to Cart event is crucial to determine the performance of an instance and take business decisions based on data.
There are two situations where the Add to Cart should be attributed to the Search:
Add to Cart button in the Search Results Page (SRP): this is fully handled by the Overlay.
Add to Cart button in the Product Details Page (PDP): the Overlay only tracks an Add to Cart event if the user has clicked on the product in the SRP in the last 60 seconds (60 seconds is the default time and can be modified). In order to get this to work, you will need to provide some additional information to the Overlay: the selector of your Add to Cart button (ebConfig.domA2CTrigger
). This way the Overlay will know how to handle this event.
Note: We used to have a separate library to perform this task (EmpathyTAG), but it made the integration process more complex, so we decided to include this behavior into this Overlay version.
Note: Add to Cart used to be named Conversion in the Overlay, so most documentation for older versions will still be referencing this name.
The workflow is:
If you have the Overlay initialization piece of code, you have to modify it to tell it that it has to execute product page tracking steps, using the parameter "conversionOnProduct":
<script> function clickCallback(data) { // GA Code } function searchCallback(data) { // GA Code } EmpathyOverlay.config('YOUR_EB_SEARCH_URL', { lang: 'en', displayLang: 'en', scope: 'default', defaultCurrency: 'EUR', customTrackSearch: searchCallback, customTrackClick: clickCallback, conversionOnProduct: true }, document.getElementById('eb-root')); </script> |
First of all, you have to know that this process uses EmpathyTag library, so this library must be included inside your product page code before using the API. This piece of code should be inserted at the bottom of the page.
<script src="https://URL_EMPATHYBROKER/js/api/empathyutils.js" type="text/javascript"></script> <script src="https://URL_EMPATHYBROKER/js/api/empathytag.js" type="text/javascript"></script> |
Or use the minified version:
<script src="https://URL_EMPATHYBROKER/js/empathy.resources.min.js" type="text/javascript"></script> |
<script src="https://static.empathybroker.com/{EB_INSTANCE_NAME}/js/api/empathyutils.js" type="text/javascript"></script> <script src="https://static.empathybroker.com/{EB_INSTANCE_NAME}/js/api/empathytag.js" type="text/javascript"></script> |
Or use the minified version:
<script src="https://static.empathybroker.com/{EB_INSTANCE_NAME}/js/empathy.resources.min.js" type="text/javascript"></script> |
For more information, read the following wiki: https://bitbucket.org/empathybroker/empathybroker-api-guide/wiki/tag/JavascriptLibraries
To make all the process work, you have to add a snippet at the bottom of your product page code, just after the resources import. This snippet executes the following steps:
var overlayProduct; window.onload = function () { if (window.location.hash != "") { var hash = window.location.hash; hash = hash.replace('#', ''); overlayProduct = JSON.parse(localStorage.getItem('eb-' + hash)); document.getElementById("ADD_TO_CART_BUTTON_ID").onclick = empathyTrackConversion; //remove hash and localStorage entry localStorage.removeItem('eb-' + hash); window.location.hash = ''; } } function empathyTrackConversion() { var empathyTAG = new EmpathyBrokerTAG('URL_EMPATHYBROKER').init(); empathyTAG.trackConversion(overlayProduct.query, overlayProduct.page, overlayProduct.name, DESTINATION_URL, overlayProduct.scope, JSON.parse(overlayProduct.options), trackConversionCallback); //If DESTINATION_URL is the product page, use overlayProduct.url } function trackConversionCallback(data) { //GA Code } |
Note: This snippet is using Javascript, but It can be modified to use a library like jQuery.
The available fields in "data" are:
Data | Key |
---|---|
Language | data.options.lang |
Page | data.page |
Scope | data.scope |
Search terms | data.terms |
Product Name | data.title |
Url | data.url |
For more data contact us.
The available fields in "overlayProduct" are:
Data | Key |
---|---|
name | overlayProduct.name |
id | |
url | overlayProduct.url |
query | overlayProduct.query |
page | overlayProduct.page |
lang | overlayProduct.options.lang |
scope | overlayProduct.scope |
For more data contact us.
If you want to use Google Analytics with Google Tag Manager, you have to follow the steps for the chosen option (See Google Tag Manager Detailed Instructions), and do a few things more into the product page. The steps you have to follow for this page are:
Finally, you have to modify the "trackConversionCallback" of the product page for the chosen option.
The trackConversionCallback for this option should be something like this:
function trackConversionCallback(data) { dataLayer.push({'eventCategory':'EmpathyOverlay', 'eventAction':'conversionOnProductPage', 'eventLabel':data.title, 'eventValue':1, 'event': 'gaEvent'}); //The chosen keys and values can be different. The keys must have the same name in the code and in Google Tag Manager } |
The trackConversionCallback for this option should be something like this:
function trackConversionCallback(data) { dataLayer.push({'data': data}); dataLayer.push({'event': 'ebConversionCallback'}); } |
Related articles appear here based on the labels you select. Click to edit the macro and add or change labels.