Behavioral Analytics using Search UI - trackSearchClick

Hello all,

I am currently working on adding behavioral analytics to track metrics such as search queries, top click results, etc on our website.

I am using the following imports

import {
  getTracker,
  createTracker,
  trackSearch,
  trackSearchClick
} from "@elastic/behavioral-analytics-javascript-tracker";

I have created the tracker using fields: endpoint, collectionName, apiKey.
I initialize this tracker on the file where we have the function to get search results using the SearchDriver (to run a search query + pagination).

Within the searchDriver, I have added the tracker like below,

const driver = new SearchDriver({
      apiConnector,
      searchQuery,
      initialState: {
        ...
      },
      trackUrlState: false,
      plugins: [
        AnalyticsPlugin({
          client: tracker,
        })
      ]
    });

and once search is done running

I use trackSearch function like so

trackSearch({
          search: {
            query: query,
            results: {
              total_results: state.totalResults,
            },
            page: {
              current: pageNumber,
              size: numResults,
            }
          }
        });

This works great!
However, we have another file where we use this exported search function and we display the results as links.

What we want to achieve here is when user clicks on the link, we would like to track that data, so I have written a method to do just that

handleLinkClick(title, url) {
      trackSearchClick({
        page: {
          url,
          title,
        },
        search: {
          query: this.query,
          filters: [
            { field: "[some field]", value: [some filters] },
          ],
          page: { current: this.pageNumber, size: 10 },
        }
      });

      window.location.href = url;
    }
  }

However, this event is not showing up in our analytics collection's logs or even in the overview dashboard.

Do we need to initialize tracker everywhere we call these methods?
or is there some sort of issue on how I have written the tracker code.

I have been following this documentation : behavioral-analytics-tracker/packages/javascript-tracker at main · elastic/behavioral-analytics-tracker · GitHub

Thanks in advance!

UPDATE:
I am able to get this working through window.elasticAnalytics.trackSearchClick. Just wanted to see if I can make it working through search UI instead as we are using mainly search UI functionality for searching.

Update:

I was able to fix this issue on my end.
I had to initialize at the tracker at a higher level than where I was initially creating the tracker. This allowed it to be more global and track search and track search click are now working :slight_smile: