Skip to content

Commit 4df8dff

Browse files
committed
Matomo plugin: Add conditions for sending search analytics data
The Matomo plugin only sends data under the following conditions: * The modal UI is opened * The query string has length greater than 0 * The query term wasn't used before Increase debounce limit to 500 ms.
1 parent cddc491 commit 4df8dff

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

packages/docsearch-react/src/MatomoPlugin.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ declare global {
66
}
77
}
88

9+
var query_cache = "";
10+
911
function _matomoSiteSearch(query: string, hits: string) {
12+
query_cache = query;
1013
var _paq = window._paq = window._paq || [];
11-
if (query.length > 0) {
12-
_paq.push(['trackSiteSearch', query, false, hits]);
13-
}
14+
_paq.push(['trackSiteSearch', query, false, hits]);
1415
}
1516

16-
var matomoSiteSearch_debounced = debounce(_matomoSiteSearch, 400);
17+
var matomoSiteSearch_debounced = debounce(_matomoSiteSearch, 500);
1718

1819
export function createMatomoPlugin() {
1920
return {
2021
onStateChange({ state }) {
21-
matomoSiteSearch_debounced(state.query, state.context.nbHits);
22-
}
22+
if ( state.isOpen && state.query.length > 0 && query_cache !== state.query ) {
23+
matomoSiteSearch_debounced(state.query, state.context.nbHits);
24+
}
25+
},
2326
};
2427
};

0 commit comments

Comments
 (0)