cancel
Showing results for 
Search instead for 
Did you mean: 

Product View Count Report - Stopping IP or User-Agent From Counting Towards It

Product View Count Report - Stopping IP or User-Agent From Counting Towards It

Hello,

 

I'm hoping someone may have a work-around/solution.

 

A bit of an extra: I am using the LiteSpeed Enterprise software (replacing Apache) and it comes with a crawler to basically pre-cache the pages (so no one has to hit an uncached page on first load).

 

Well, if I have the report turned on for the "Product Views" it will gather the crawl bot's view and inflate the view count per product every time I run the crawler. This is unwanted. I want to leave the view count report turned on but it's pointless if the numbers aren't correct.

 

Some background info: With the crawler I see it uses my server IP to "access" the page, so I'm wondering if there would be a way to stop Magento from counting the product view from a certain IP (my server IP) or by user-agent (User-Agent would be helpful to stop bots from counting as well). Pretty sure that would solve the problem. 

 

I'm not sure how Magento collects the view counts - is it by IP, by session or by cookies? Or some other way? I notice that adblocker don't stop the count (which is awesome). 

 

Thank you in advance for any help you can give.

4 REPLIES 4

Re: Product View Count Report - Stopping IP or User-Agent From Counting Towards It

To prevent the LiteSpeed crawler from inflating the view counts in Magento, you can consider doing this:

Exclude user agent: If the crawler has a unique user agent, you can configure Magento to ignore views from that user agent. This method requires adding JavaScript code to the HTML head section of your Magento configuration to check the user agent and prevent tracking if it matches the crawler's user agent.

 

Remember to test the changes thoroughly, before dependently relying on the process.

Re: Product View Count Report - Stopping IP or User-Agent From Counting Towards It

Thanks for the reply I appreciate that.

 

So that is exactly what I was looking for, a way to block the view based on user-agent or IP. User-agent would work (and would help to prevent views from other sources like googlebot, etc.). 

 

I am just having an issue on finding the code to implement. I searched google and can't find anything. Seeing how this is probably more geared towards Magento only, there isn't much on this. Hence why I posted this question. If I search for JS to block tracking, it talks about google analytics and such - not specifically Magento page view tracking. 

 

Could you possibly point me to a reference on how to implement this? I can create modules and modify code (through my theme) so that's no issue. But it's the actual code that I do not know or cannot find.


Thanks again in advance

Re: Product View Count Report - Stopping IP or User-Agent From Counting Towards It

@ibtisamdev8aef would you happen to know of the code I could use to make this happen?

 

Thanks in advance!

Re: Product View Count Report - Stopping IP or User-Agent From Counting Towards It

Sorry for the delayed reply. Here is some sample code you can use in Magento to exclude the LiteSpeed crawler user agent from counting towards product views:

1. Create an observer

Create an observer that will trigger on the catalog_controller_product_view event. This event runs when a product page is viewed.
// Observer.php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class ExcludeCrawlerObserver implements ObserverInterface
{

public function execute(\Magento\Framework\Event\Observer $observer)
{
// Observer code goes here
}

}
2. Check user agent and return if crawler

In the observer, check the HTTP user agent from the request. If it matches the LiteSpeed crawler user agent, return/exit early to avoid counting the view.


public function execute(\Magento\Framework\Event\Observer $observer)
{

$userAgent = $observer->getEvent()->getData('controller_action')->getRequest()->getUserAgent();

if (strpos($userAgent, 'LiteSpeed-Crawler') !== false) {
return;
}

// Rest of tracker code

}
3. Configure events.xml

Make sure to configure events.xml to enable your observer:
<!-- events.xml -->

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_controller_product_view">
<observer name="exclude_crawler_view" instance="Vendor\Module\Observer\ExcludeCrawlerObserver"/>
</event>
</config>


This should exclude any views from the LiteSpeed crawler user agent and give you accurate product view counts.