> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate Google Analytics

> Get insights from user searches in your InstantSearch.js app.

<div className="not-prose algolia-flavor-switcher">
  <div className="afs-dropdown">
    <div className="afs-trigger" role="button" tabIndex="0" aria-haspopup="listbox">
      <span className="afs-current">JavaScript</span>

      <svg className="afs-chevron lucide lucide-chevron-down" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
        <path d="m6 9 6 6 6-6" />
      </svg>
    </div>

    <ul className="afs-menu" role="listbox">
      <li role="option" aria-selected="true"><a className="afs-option is-current" href="/doc/guides/building-search-ui/going-further/integrate-google-analytics/js"><span className="afs-option-name">JavaScript</span><span className="afs-option-lib">InstantSearch.js</span></a></li>
      <li role="option" aria-selected="false"><a className="afs-option" href="/doc/guides/building-search-ui/going-further/integrate-google-analytics/react"><span className="afs-option-name">React</span><span className="afs-option-lib">React InstantSearch</span></a></li>
      <li role="option" aria-selected="false"><a className="afs-option" href="/doc/guides/building-search-ui/going-further/integrate-google-analytics/vue"><span className="afs-option-name">Vue</span><span className="afs-option-lib">Vue InstantSearch</span></a></li>
    </ul>
  </div>
</div>

Although Algolia provides analytics tailored to your search implementation,
you might want to send your search data to your existing analytics tools.

To do this, implement some custom middleware:

<Steps>
  <Step title="Set up Google Analytics">
    See [Set up the Google tag with gtag.js](https://developers.google.com/tag-platform/gtagjs).
  </Step>

  <Step title="Create the middleware to send events">
    In the event listener, send events to Google Analytics.
    This example debounces the event for 3 seconds but adjust this to your needs.

    ```js JavaScript icon="code" theme={"system"}
    function googleAnalyticsMiddleware() {
      let timer;
      const sendEventDebounced = () => {
        clearTimeout(timer);
        timer = setTimeout(() => {
          gtag('event', 'page_view', {
            page_location: window.location.pathname + window.location.search,
          });
        }, 3000);
      });

      return {
        onStateChange() {
          sendEventDebounced();
        },
        subscribe() {},
        unsubscribe() {},
      };
    }
    ```
  </Step>

  <Step title="Inject the middleware into the InstantSearch lifecycle">
    ```js JavaScript theme={"system"}
    const search = instantsearch({
      searchClient,
      indexName,
    });

    search.use(googleAnalyticsMiddleware);
    ```
  </Step>
</Steps>
