Access search results object in Magento

Access search results object in Magento

October 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Access search results object in Magento

Accessing Search Results Object in Magento for Google Tag Manager Integration

Integrating Google Tag Manager (GTM) into your Magento store allows you to track user behavior more effectively and optimize marketing campaigns. One key piece of data you’ll want to include in the datalayer is information about search queries and results returned.

Getting the Search Query Text

You’re already on the right track by using the following code snippet to retrieve the search query text:

Mage::helper('catalogsearch')->getQueryText();

This helper function returns the current search query entered by the user. Make sure you have access to this helper in your custom module or controller.

Accessing the Number of Search Results

To get the number of results returned for a specific search query, you can use the Mage_CatalogSearch_Model_Resource_Fulltext resource model’s getCount() method:

$searchModel = Mage::getResourceSingleton('catalogsearch/fulltext');
$query = 'YOUR_SEARCH_QUERY_HERE';
$count = $searchModel->getCount($query);

echo "Number of results: {$count}";

Replace 'YOUR_SEARCH_QUERY_HERE' with the actual search query text you obtained from Mage::helper('catalogsearch')->getQueryText().

This code initializes the full-text search resource model and uses it to retrieve the count of matching products for a given search query.

Best Practices

  1. Use Indexed Search: Ensure that your catalog index is set up correctly, especially if you’re dealing with large product collections or complex attributes. Magento’s Enterprise Edition offers advanced indexing options for better performance.

  2. Cache Results: If you need to display the result count on the search results page, consider caching the count using Magento’s built-in cache system or a third-party extension like Varnish Cache.

  3. Handle Pagination: When dealing with large datasets, it’s essential to handle pagination correctly. Magento’s getPageSize() and getCurrentPage() methods can help you retrieve the current page number and items per page.

Integrating with Google Tag Manager

Once you have retrieved both the search query text and the result count, you can push this data into your GTM datalayer:

$searchText = Mage::helper('catalogsearch')->getQueryText();
$searchResultsCount = $searchModel->getCount($searchText);

Mage::app()->setDesign(Mage::getDesign());
$block = Mage::getSingleton('core/layout')->createBlock('core/template', 'gtm_search_data');
$block->assign(array(
    'search_text' => $searchText,
    'results_count' => $searchResultsCount
));
echo $block;

This code snippet creates a custom block, assigns the search data to it, and then outputs the block. The core/template type can be rendered as a string for easy integration into your GTM tracking script.

Conclusion

Accessing search results in Magento requires using the appropriate resource models and helper functions. By combining these techniques with proper indexing and caching strategies, you can effectively integrate search-related data into your Google Tag Manager setup.

Don’t forget to test your implementation thoroughly across different search scenarios to ensure accurate data flow.

For more advanced customization options and performance tuning tips for Magento, consider exploring our services at Laravel Company. We specialize in helping businesses optimize their e-commerce platforms for improved user experience and conversions.

Catalog Indexing Database Performance

Our network