Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin for Matomo to track site search keywords #17

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/docsearch-react/src/DocSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface DocSearchProps {
navigator?: AutocompleteOptions<InternalDocSearchHit>['navigator'];
translations?: DocSearchTranslations;
getMissingResultsUrl?: ({ query }: { query: string }) => string;
matomoSearchAnalytics?: boolean;
}

export function DocSearch(props: DocSearchProps) {
Expand Down
11 changes: 11 additions & 0 deletions packages/docsearch-react/src/DocSearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { AutocompleteState } from '@algolia/autocomplete-core';
import { createAutocomplete } from '@algolia/autocomplete-core';
import React from 'react';

import { createMatomoPlugin } from './MatomoPlugin';

import { MAX_QUERY_SIZE } from './constants';
import type { DocSearchProps } from './DocSearch';
import type { FooterTranslations } from './Footer';
Expand Down Expand Up @@ -50,6 +52,7 @@ export function DocSearchModal({
initialQuery: initialQueryFromProp = '',
translations = {},
getMissingResultsUrl,
matomoSearchAnalytics = false,
}: DocSearchModalProps) {
const {
footer: footerTranslations,
Expand Down Expand Up @@ -125,6 +128,13 @@ export function DocSearchModal({
[favoriteSearches, recentSearches, disableUserPersonalization]
);

var plugins_to_load: any = [];

if (matomoSearchAnalytics) {
const matomoPlugin = createMatomoPlugin();
plugins_to_load.push(matomoPlugin);
};

const autocomplete = React.useMemo(
() =>
createAutocomplete<
Expand Down Expand Up @@ -282,6 +292,7 @@ export function DocSearchModal({
);
});
},
plugins: plugins_to_load,
}),
[
typesenseCollectionName,
Expand Down
27 changes: 27 additions & 0 deletions packages/docsearch-react/src/MatomoPlugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import debounce from 'lodash/debounce';

declare global {
interface Window {
_paq: any;
}
}

var query_cache = "";

function _matomoSiteSearch(query: string, hits: string) {
query_cache = query;
var _paq = window._paq = window._paq || [];
_paq.push(['trackSiteSearch', query, false, hits]);
}

var matomoSiteSearch_debounced = debounce(_matomoSiteSearch, 500);

export function createMatomoPlugin() {
return {
onStateChange({ state }) {
if ( state.isOpen && state.query.length > 2 && query_cache !== state.query ) {
matomoSiteSearch_debounced(state.query, state.context.nbHits);
}
},
};
};