How To

How to Add Website Total Visitor on Admin Dashboard in Magento 2?

Hello Magento Friends,

In this Magento 2 blog tutorial, we will learn How to Add Website Total Visitor on Admin Dashboard in Magento 2.

Knowing the total number of visitors to your Magento 2 store is an essential part of website analytics. It provides valuable data that can help you understand your audience, assess the effectiveness of your strategies, and make informed decisions to improve your store’s performance and user experience.

Let’s find out How to Add Total Visitors on the Admin Dashboard in Magento 2.

Steps to Add Website Total Visitor on Admin Dashboard in Magento 2:

Step 1: First, we need to create a di.xml file inside the extension at the following path.

app\code\Vendor\Extension\etc\adminhtml\

Then add the code as follows

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Backend\Block\Dashboard\Sales" type="Vendor\Extension\Block\Dashboard\Sales"/>
</config>

Step 2: After that, we need to create a Sales.php file inside the extension at the following path.

app\code\Vendor\Extension\Block\Dashboard\

And add the following code-snippet

<?php
namespace Vendor\Extension\Block\Dashboard;

use Magento\Backend\Block\Template\Context;
use Magento\Framework\Module\Manager;
use Magento\Reports\Model\ResourceModel\Order\CollectionFactory;
use Magento\Customer\Model\Visitor;

class Sales extends \Magento\Backend\Block\Dashboard\Sales
{
    protected $visitors;
    protected $vendorCollectionFactory;

    public function __construct(
        Context $context,
        CollectionFactory $collectionFactory,
        Manager $moduleManager,
        Visitor $visitors,
        array $data = []
    ) {
        $this->_moduleManager = $moduleManager;
        $this->visitors = $visitors;
        parent::__construct($context, $collectionFactory,$moduleManager, $data);
    }

    protected function _prepareLayout() 
    {
        parent::_prepareLayout();
        $collection = $this->visitors->getCollection();
        
        $this->addCustomerVisitor(__('Total Visitors :'), count($collection));
    }
    public function addCustomerVisitor($label, $value, $isQuantity = false)
    {
        $decimals = '';
        $this->_totals[] = ['label' => $label, 'value' => "<span style='color: #eb5202;font-size: 2.4rem;'>".$value."</span>",'decimals' => $decimals];
        return $this;
    }
}

Step 3: Once all files are created in your Magento, you need to run Magento upgrade, compile and deploy commands as follows.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

Output:

Conclusion:

Hence, this way, you can add the total visitors count in the Magento 2 admin dashboard. If you have any doubts regarding the above steps, feel free to get in touch with me through the comment box. If you need further customization, contact experienced Magento Developers. Share the tutorial with your friends and stay in touch with us for more such useful solutions.

Happy Coding!

Click to rate this post!
[Total: 1 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

1 day ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

4 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

5 days ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

6 days ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

6 days ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

1 week ago