You can find a general guide for how to implement Analytics Tags here [1] , and you can learn more details about our analyticsTags parameter here [2].
With Algolia's Magento extension, you can implement analytics tags by following these steps:
1. Register a custom hook [3] in the Magento 2 extension. You can use the beforeWidgetInitialization hook to set the analyticsTags.
2. Write logic to identify the information that you want to tag (ie: "fromCategoryPage" or "mobileDevice"). For example, you can identify if the current page is a category page by checking the binary value of the algoliaConfig.isCategoryPage variable provided by the extension and adding the appropriate tag based on the value.
3. Add analyticsTags information to the request configuration.
You can add search parameters as a part of the allWidgetConfiguration['configure'] object.
//register hook
algolia.registerHook('beforeWidgetInitialization', function (allWidgetConfiguration) {
allWidgetConfiguration['configure'] = allWidgetConfiguration['configure'] || {}
//function to return tag name according to the algoliaConfig information
const getPageType=()=>{
return algoliaConfig.isCategoryPage ? 'categoryPage' : 'searchPage';
};
//get page tag for the current page
constpageTag=getPageType();
//set page tag as analyticsTags for InstantSearch.js widgets
if (typeof allWidgetConfiguration['configure'].analyticsTags==='undefined') {
allWidgetConfiguration['configure'].analyticsTags= [pageTag];
} else {
allWidgetConfiguration['configure'].analyticsTags.push(pageTag);
}
return allWidgetConfiguration;
});
In this way, search requests sent from this configuration will get analyticsTags, and you can segment your analytics data with these tags in the dashboard.
This supplemental documentation [4] highlightst best practices when customizing the extension's behavior.
Links
[1]: https://www.algolia.com/doc/guides/getting-insights-and-analytics/search-analytics/segmenting-your-analytics-data/how-to/tag-your-users-by-platform/
[2]: https://www.algolia.com/doc/api-reference/api-parameters/analyticsTags
[4]: https://www.algolia.com/doc/integration/magento-2/guides/create-a-custom-extension?language=php