Optimize Elasticsearch autocomplete URL for Google Analytics

Optimize Elasticsearch autocomplete URL for Google Analytics

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.

You made it all the way down here so you must have enjoyed this post! You may also like:

Magento 2 Product Quick View Tomas Novoselic
, | 6

Magento 2 Product Quick View

How to set up a CDN (Amazon CloudFront) in Magento Dario Trbovic
Dario Trbovic, | 18

How to set up a CDN (Amazon CloudFront) in Magento

Extending Magento 2 default JS components Filip Svetlicic
Filip Svetlicic, | 12

Extending Magento 2 default JS components

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>.

Tell us about your project

Drop us a line. We'd love to know more about your project.