Optimize Elasticsearch autocomplete URL for Google Analytics

optimize_elasticsearch_featured

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.

Autocomplete Elasticsearch

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.


About Matej Djakovic

Frontend Developer

Matej is a Frontend Developer who is passionate about Wordpress, Symfony 2 and Magento. He is also enthusiastic about sports and spends some of his free time for inline roller skating.

Read more posts by Matej / Visit Matej's profile

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.