Skip to content

Commit e9a3127

Browse files
committed
MAGE-899 Add separate method for attaching click analytics
1 parent 70b32cd commit e9a3127

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

view/frontend/web/js/autocomplete.js

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,36 +76,7 @@ define([
7676

7777
this.startAutocomplete(options);
7878

79-
//Autocomplete insight click conversion
80-
// TODO: Switch to insights plugin
81-
if (algoliaConfig.ccAnalytics.enabled) {
82-
$(document).on('click', '.algoliasearch-autocomplete-hit', function () {
83-
const $this = $(this);
84-
if ($this.data('clicked')) return;
85-
86-
const objectId = $this.attr('data-objectId');
87-
const indexName = $this.attr('data-index');
88-
const queryId = $this.attr('data-queryId');
89-
const position = $this.attr('data-position');
90-
91-
let useCookie = algoliaConfig.cookieConfiguration
92-
.cookieRestrictionModeEnabled
93-
? !!algoliaCommon.getCookie(algoliaConfig.cookieConfiguration.consentCookieName)
94-
: true;
95-
if (useCookie !== false) {
96-
algoliaInsights.initializeAnalytics();
97-
const eventData = algoliaInsights.buildEventData(
98-
'Clicked',
99-
objectId,
100-
indexName,
101-
position,
102-
queryId
103-
);
104-
algoliaInsights.trackClick(eventData);
105-
$this.attr('data-clicked', true);
106-
}
107-
});
108-
}
79+
this.trackClicks();
10980

11081
if (algoliaConfig.autocomplete.isNavigatorEnabled) {
11182
$('body').append(
@@ -821,6 +792,41 @@ define([
821792
},
822793
}
823794
);
824-
}
795+
},
796+
797+
/**
798+
* Autocomplete insight click conversion
799+
*/
800+
trackClicks() {
801+
// TODO: Switch to insights plugin
802+
if (algoliaConfig.ccAnalytics.enabled) {
803+
$(document).on('click', '.algoliasearch-autocomplete-hit', function () {
804+
const $this = $(this);
805+
if ($this.data('clicked')) return;
806+
807+
const objectId = $this.attr('data-objectId');
808+
const indexName = $this.attr('data-index');
809+
const queryId = $this.attr('data-queryId');
810+
const position = $this.attr('data-position');
811+
812+
let useCookie = algoliaConfig.cookieConfiguration
813+
.cookieRestrictionModeEnabled
814+
? !!algoliaCommon.getCookie(algoliaConfig.cookieConfiguration.consentCookieName)
815+
: true;
816+
if (useCookie !== false) {
817+
algoliaInsights.initializeAnalytics();
818+
const eventData = algoliaInsights.buildEventData(
819+
'Clicked',
820+
objectId,
821+
indexName,
822+
position,
823+
queryId
824+
);
825+
algoliaInsights.trackClick(eventData);
826+
$this.attr('data-clicked', true);
827+
}
828+
});
829+
}
830+
},
825831
});
826832
});

0 commit comments

Comments
 (0)