From a743752d4f5354e5c2d3e78edc7290ba7668547e Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Tue, 16 Aug 2022 11:54:42 +0300 Subject: [PATCH] docs: added search track event (#2051) * added search track event * handle all search inputs events --- www/docs/src/theme/SearchBar/index.js | 34 ++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/www/docs/src/theme/SearchBar/index.js b/www/docs/src/theme/SearchBar/index.js index 381d9012c0..4d3a68954e 100644 --- a/www/docs/src/theme/SearchBar/index.js +++ b/www/docs/src/theme/SearchBar/index.js @@ -1,7 +1,39 @@ -import React from 'react'; +import React, { useEffect } from 'react'; + import SearchBar from '@theme-original/SearchBar'; +import useIsBrowser from '@docusaurus/useIsBrowser'; +import {useLocation} from '@docusaurus/router'; export default function SearchBarWrapper(props) { + + const isBrowser = useIsBrowser(); + const location = useLocation(); + + useEffect(() => { + if (isBrowser) { + + function trackSearch(e) { + if (!e.target.classList.contains('DocSearch-Input') && !(e.target.tagName.toLowerCase() === "input" && e.target.getAttribute('type') === 'search')) { + return; + } + + const query = e.target.value; + if (query.length >= 3) { + //send event to segment + window.analytics.track('search', { + query + }); + } + } + + document.body.addEventListener('keyup', trackSearch); + + return () => { + document.body.removeEventListener('keyup', trackSearch); + } + } + }, [isBrowser, location.pathname]); + return ( <>