Skip to content

Commit c008a5d

Browse files
committed
MAGE-899 refactor insights for flexibile loading and mixin support
1 parent 173fbc0 commit c008a5d

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

view/frontend/web/js/autocomplete.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ define([
1818
'algoliaAutocompleteSuggestionsHtml',
1919
'algoliaAutocompleteAdditionalHtml',
2020

21-
// TODO: Refactor legacy global object dependencies
2221
'algoliaInsights',
2322
'algoliaHooks',
24-
2523
'domReady!'
2624
], function (
2725
Component,
@@ -35,7 +33,8 @@ define([
3533
categoriesHtml,
3634
pagesHtml,
3735
suggestionsHtml,
38-
additionalHtml
36+
additionalHtml,
37+
algoliaInsights
3938
) {
4039
const DEFAULT_HITS_PER_SECTION = 2;
4140
const DEBOUNCE_MS = algoliaConfig.autocomplete.debounceMilliseconds;

view/frontend/web/js/insights.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@ define([
44
'algoliaCommon',
55
'mage/cookies',
66
], function ($, algoliaAnalyticsWrapper, algoliaCommon) {
7+
const USE_GLOBALS = true;
8+
79
algoliaAnalytics = algoliaAnalyticsWrapper.default;
810

9-
window.algoliaInsights = {
11+
const algoliaInsights = {
1012
config : null,
1113
defaultIndexName : null,
1214
isTracking : false,
1315
hasAddedParameters: false,
1416

17+
initialize() {
18+
if (!window.algoliaConfig) return;
19+
20+
this.addSearchParameters();
21+
this.bindConsentButtonClick(algoliaConfig);
22+
this.track(algoliaConfig);
23+
},
24+
1525
useCookie() {
1626
return !this.config.cookieConfiguration.cookieRestrictionModeEnabled
1727
|| !!algoliaCommon.getCookie(this.config.cookieConfiguration.consentCookieName);
@@ -95,7 +105,7 @@ define([
95105
(allWidgetConfiguration) => {
96106

97107
allWidgetConfiguration.configure =
98-
algoliaInsights.applyInsightsToSearchParams(allWidgetConfiguration.configure);
108+
this.applyInsightsToSearchParams(allWidgetConfiguration.configure);
99109

100110
return allWidgetConfiguration;
101111
}
@@ -104,7 +114,7 @@ define([
104114
algoliaCommon.registerHook(
105115
'afterAutocompleteProductSourceOptions',
106116
(options) => {
107-
return algoliaInsights.applyInsightsToSearchParams(options);
117+
return this.applyInsightsToSearchParams(options);
108118
}
109119
);
110120

@@ -343,20 +353,19 @@ define([
343353
algoliaConfig.cookieConfiguration.cookieAllowButtonSelector,
344354
(event) => {
345355
event.preventDefault();
346-
algoliaInsights.initializeAnalytics(algoliaConfig, true);
356+
this.initializeAnalytics(algoliaConfig, true);
347357
}
348358
);
349359
}
350360
};
351361

352-
algoliaInsights.addSearchParameters();
353-
354362
$(function ($) {
355-
if (window.algoliaConfig) {
356-
algoliaInsights.bindConsentButtonClick(algoliaConfig);
357-
algoliaInsights.track(algoliaConfig);
358-
}
363+
algoliaInsights.initialize()
359364
});
360365

366+
if (USE_GLOBALS) {
367+
window.algoliaInsights = algoliaInsights;
368+
}
369+
361370
return algoliaInsights;
362371
});

view/frontend/web/js/instantsearch.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ define([
1414
// Magento core libs
1515
'Magento_Catalog/js/price-utils',
1616

17-
// TODO: Refactor legacy global object dependencies
1817
'algoliaInsights',
1918
'algoliaHooks',
2019
], function (Component, $, algoliasearch, instantsearch, algoliaCommon, algoliaBase64, templateEngine, priceUtils) {
@@ -793,7 +792,7 @@ define([
793792
(facet.label ? facet.label : facet.attribute) +
794793
'</div>',
795794
},
796-
hidden : function (options) {
795+
hidden: (options) => {
797796
if (
798797
options.results.nbPages <= 1 &&
799798
algoliaConfig.instant.hidePagination === true

0 commit comments

Comments
 (0)