How To Add Website Total Visitor Count On Admin Dashboard In Magento 2

Hello Everyone,

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

Display Total Number of Visitors of Websites on Admin Dashboard is very important for any e-commerce store.

This provides data that can help store owners to understand the audience.

STEPS FOR ADD WEBSITE TOTAL VISITOR ON ADMIN DASHBOARD IN MAGENTO 2

Step 1: Create di.xml file

app/code/Vendor/Extension/etc/adminhtml/di.xml

<?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: Create Sales.php file

app/code/Vendor/Extension/Block/Dashboard/Sales.php

<?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->addWebsiteVisitor(__('Total Visitors'), count($collection));

    }

    public function addWebsiteVisitor($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: Finally run the below commands

$ php bin/magento cache:clean

$ php bin/magento cache:flush

Step 4: Output:

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add Website Total Visitor in Admin Dashboard in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

Leave a Reply

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