Guides
/
Building Search UI
/
Going further
Jun. 27, 2022
Integrate Google Analytics in Vue InstantSearch
Even though Algolia provides analytics that are tailored to your search implementation, you might want to integrate your search into your existing analytics tools. You can implement a custom middleware to do that.
First, you follow the steps on how to set up the Google Analytics library in your page.
Then you can create a middleware. The In the event listener, you can send events to Google Analytics or any solution.
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import debounce from 'lodash.debounce';
function googleAnalyticsMiddleware() {
const sendEventDebounced = debounce((uiState) => {
window.ga('set', 'page', window.location.pathname + window.location.search);
window.ga('send', 'pageView');
}, 3000);
return {
onStateChange({ uiState }) {
sendEventDebounced(uiState);
},
subscribe() {},
unsubscribe() {},
};
}
Once the middleware is created, you can inject it into the InstantSearch lifecycle:
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
<ais-instantsearch
:search-client="searchClient"
index-name="instant_search"
:middlewares="middlewares"
>
<!-- ... -->
</ais-instantsearch>
</template>
<script>
export default {
data() {
return {
searchClient,
middlewares: [googleAnalyticsMiddleware],
};
},
}
</script>
Did you find this page helpful?