# Swiftype-analytics issue

**URL:** <https://discuss.elastic.co/t/swiftype-analytics-issue/371377>\
**Category:** Elastic Search\
**Tags:** elastic-site-search\
**Created:** [December 3, 2024, 11:40am UTC](https://discuss.elastic.co/t/swiftype-analytics-issue/371377 "2024-12-03T11:40:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yevhen\_Marynych](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yevhen_marynych/32/139652_2.png) [@Yevhen\_Marynych](https://discuss.elastic.co/u/Yevhen_Marynych)\
**Post date:** [December 3, 2024, 11:40am UTC](https://discuss.elastic.co/t/swiftype-analytics-issue/371377/1 "2024-12-03T11:40:40Z")

</div>

I've been using Swifype for a while on the website and as soon as it's being used with default input-field (st-default-search-input) and default overlay, analytics worked just fine.  
But at some point I had to rework it a little bit. So, I made custom input that sends search-query to the Swiftype-API, receives the results and renders them on the page. It's working just fine, I get results from the Swiftype-API and render them on the page, but I noticed that analytics stopped collecting any data since then. There's no search results displayed in the Analytics-tab in the Swiftype's dashboard at all (screenshot attached).

 ![image (6)](https://us1.discourse-cdn.com/elastic/original/3X/1/1/116fb273fdaea668e360974acb7f5d8d71990795.png)

So, my question is if that's possible to make Swiftype-analytics work when using the "get search results by uisng Swiftype-API"-approach instead of using built-in default "st-default-search-input" as input and default overlay (or "st-search-container"-container) ?

Thanks in advance,  
Yevhen

---

<div class="post-metadata">

**Author:** ![atul\_mohan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atul_mohan/32/137776_2.png) [@atul\_mohan](https://discuss.elastic.co/u/atul_mohan)\
**Post date:** [December 30, 2024, 6:33am UTC](https://discuss.elastic.co/t/swiftype-analytics-issue/371377/2 "2024-12-30T06:33:48Z")

</div>

Ensure API Usage:  
Make sure you're using the correct API endpoints for searching. Swiftype's API should be set up to return track\_urls in the response if analytics is configured for your account.  
Implement Manual Tracking:  
Track Events: You need to manually send tracking events to Swiftype's analytics endpoint. Here's how you might do this in JavaScript:

```auto
function trackSearchEvent(query, resultClicked) {
  const endpoint = 'https://api.swiftype.com/api/v1/public/analytics';
  const apiKey = 'YOUR_SWIFTYPE_API_KEY'; // Replace with your actual API key
  const engineKey = 'YOUR_ENGINE_KEY'; // Replace with your engine key

  if (resultClicked) {
    // Track click event
    fetch(endpoint, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        api_key: apiKey,
        engine_key: engineKey,
        t: 'click',
        q: query,
        d: resultClicked.id,
        p: resultClicked.position // Position in search results
      })
    });
  } else {
    // Track search event
    fetch(endpoint, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        api_key: apiKey,
        engine_key: engineKey,
        t: 'search',
        q: query
      })
    });
  }
}

// Example usage:
document.getElementById('customSearchInput').addEventListener('submit', function(e) {
  e.preventDefault();
  let query = this.querySelector('input[name="q"]').value;
  trackSearchEvent(query, null); // Track the search event

  // Assuming you fetch and render results here
  fetchSearchResults(query).then(results => {
    renderResults(results);
    // Attach click handlers to results for tracking
    document.querySelectorAll('.search-result').forEach((result, index) => {
      result.addEventListener('click', function() {
        trackSearchEvent(query, { 
          id: this.dataset.resultId, 
          position: index 
        });
      });
    });
  });
});

// Helper functions to fetch and render results would go here
function fetchSearchResults(query) {
  // Fetch results from Swiftype API
}

function renderResults(results) {
  // Render results on your page
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 27, 2025, 6:33am UTC](https://discuss.elastic.co/t/swiftype-analytics-issue/371377/3 "2025-01-27T06:33:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
