I'm using episerver find with ajax functionality and pagination and I noticed that FindApi()
is adding the onclick attribute for tracking but it is not exposing the function itself.
Thus when i'm using the pagination on ajax call i loose the tracking onclick script since it is triggered on window onload
only and the function is defined on the addEvetListener
as anonymous in bindWindowEvents
.
It would be nice to extract the function like attachOnClickAttribute
(sorry the code in find.js comes minified already):
this.bindWindowEvents = function() {
var t = this;
window.history && (window.onbeforeunload = function() {
var e = document.location.href;
e.indexOf("q=") > 0 && -1 == e.indexOf(t._dontTrackQueryParam + "=") && window.history.replaceState(window.history.state, window.document.title, e + (e.indexOf("?") > 0 ? "&" : "?") + t._dontTrackQueryParam + "=true")
}
),
window.addEventListener("load", function() { t.attachOnClickAttribute() }, !1)
},
// Extract function
this.attachOnClickAttribute = function() {
var t = this;
var e = t._toArray(document.getElementsByTagName("A"))
, r = document.createElement("A");
r.href = document.location.href;
for (var n = 0; e.length > n; n++) {
var a = function() {
for (var a = !1, i = e[n].href, o = t._getTrackQueryParamsFromUrl(i), s = 0; o.trackQueryParams.length > s; s++)
if (0 == o.trackQueryParams[s].indexOf("id")) {
a = !0;
break
}
if (o.trackQueryParams.length > 0 && a) {
var c = document.createElement("A");
c.href = i,
("/" != t._applicationUrl && r.hostname + t._applicationUrl != c.hostname + "/" + c.pathname.split("/")[1] + "/" || "/" === t._applicationUrl) && (e[n].href = t._serviceApiBaseUrl + "_click?" + t._trackParam + o.trackQueryParams.join("&" + t._trackParam) + "&" + t._trackParam + "redirect=" + encodeURIComponent(t._getUriWithoutTrackParams(i, o)),
e[n].setAttribute("onclick", "window.location.href = '" + t._serviceApiBaseUrl + "_click?" + t._trackParam + o.trackQueryParams.join("&" + t._trackParam) + "&" + t._trackParam + "redirect=" + encodeURIComponent(t._getUriWithoutTrackParams(i, o)) + "'; return false;"))
}
};
a()
}
}
,
And then expose the find api object:
<script>
if (FindApi) {
var api = new FindApi();
api.setApplicationUrl('/');
api.setServiceApiBaseUrl('/find_v2/');
api.processEventFromCurrentUri();
api.bindWindowEvents();
api.bindAClickEvent();
api.sendBufferedEvents();
window.findApi = api; // expose the object
}</script>
In a way that on js after clicking on the pagination i could trigger something like this
// On ajax pagination
if (window && window.findApi) {
window.findApi.attachOnClickAttribute();
}