In this article, we’re talking about Extension for Elasticsearch® by BubbleShop and optimizing URL for tracking autocomplete search through Google Analytics.
The Elasticsearch extension for Magento allows you to enhance drastically the default Magento search results which are not very relevant, and much more.
Where is the problem with Elasticsearch?
Magento default search has parameter in URL “?q=keyword“, but if you click on Elasticsearch suggested autocomplete item, you will get clear url without parameters.
This feature for Elasticsearch is adding query string to all URLs in quick search dropdown. That string allows you tracking “quick search autocomplete” through Google Analytics.
Default URL (before editing):
https://www.EXAMPLE.com/product.html
New URL (optimized):
https://www.EXAMPLE.com/product.html?q=keyword
Example from image:
https://www.EXAMPLE.com/product.html?q=Madison
Step 1 – Copy files in your theme:
/app/design/frontend/base/default/template/bubble/elasticsearch/autocomplete/category.phtml
/app/design/frontend/base/default/template/bubble/elasticsearch/autocomplete/cms.phtml
/app/design/frontend/base/default/template/bubble/elasticsearch/autocomplete/product.phtml
Step 2 – Implement query string in URL:
› category.phtml – Line 15.
<a href="<?php echo $this->escapeUrl($this->getCategoryUrl($_category)) ?>"
change to:
<a href="<?php echo $this->escapeUrl($this->getCategoryUrl($_category)) ?>?q=<?php echo Mage::helper('catalogsearch')->getQueryText(); ?>"
› cms.phtml – Line 15.
<a href="<?php echo $this->escapeUrl($this->getBaseUrl() . $_page->getIdentifier()) ?>"
change to:
<a href="<?php echo $this->escapeUrl($this->getBaseUrl() . $_page->getIdentifier()) ?>?q=<?php echo Mage::helper('catalogsearch')->getQueryText(); ?>"
› product.phtml – Line 16.
<a href="<?php echo $this->escapeUrl($this->getProductUrl($_product)) ?>"
change to:
<a href="<?php echo $this->escapeUrl($this->getProductUrl($_product)) ?>?q=<?php echo Mage::helper('catalogsearch')->getQueryText(); ?>"
Step 3 – Initialization Magento
/autocomplete.php – add this code on top files:
require_once "app/Mage.php";
Mage::app();
Step 4 – Don’t forgot to clear the cache!
Hope this will help you improve your tracking through Google Analytics.