In this blog we’ll see How to Add Custom Tab on Admin Dashboard and Website Total Visitor in Magento 2.
Creating a custom tab on the admin dashboard in Magento 2 allows you to provide quick access to specific information or tools that are essential for your business.
Monitoring the total number of visitors over time helps you assess the overall performance of your Magento 2 store. You can identify trends, spikes, or declines in traffic and correlate them with specific events or marketing campaigns.
we will create Custom module and follow below steps to Add Custom Tab on Admin Dashboard and Website Total Visitor in Magento 2.
Step 1: Create register.php
app/code/Bluethinkinc/AdminDashboardTab/registration.php
1 2 3 4 5 6 7 8 9 |
<?php /** * Copyright © mukesh singh All rights reserved. * See COPYING.txt for license details. */ use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Bluethinkinc_AdminDashboardTab', __DIR__); ?> |
Step 2: Create module.xml
app/code/Bluethinkinc/AdminDashboardTab/etc/module.xml
1 2 3 4 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Bluethinkinc_AdminDashboardTab"/> </config> |
Step 3: Create di.xml
app/code/Bluethinkinc/AdminDashboardTab/etc/adminhtml/di.xml
1 2 3 4 |
<?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\Grids" type="Bluethinkinc\AdminDashboardTab\Block\Dashboard\Grids"/> </config> |
Step 4: Create routes.xml
app/code/Bluethinkinc/AdminDashboardTab/etc/adminhtml/routes.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="admin"> <route id="bluethinkinc" frontName="bluethinkinc"> <module name="Bluethinkinc_AdminDashboardTab" before="Bluethinkinc_AdminDashboardTab" /> </route> </router> </config> |
Step 5: Create Visitor.php
app/code/Bluethinkinc/AdminDashboardTab/Controller/Adminhtml/Dashboard/Visitor.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php namespace Bluethinkinc\AdminDashboardTab\Controller\Adminhtml\Dashboard; use Magento\Backend\App\Action\Context; use Magento\Framework\View\Result\PageFactory; use Magento\Backend\Controller\Adminhtml\Dashboard\AjaxBlock; use Magento\Framework\Controller\Result\RawFactory; use Magento\Framework\View\LayoutFactory; class Visitor extends AjaxBlock { protected $resultRawFactory; protected $layoutFactory; public function __construct( Context $context, RawFactory $resultRawFactory, LayoutFactory $layoutFactory, PageFactory $resultPageFactory ) { parent::__construct($context,$resultRawFactory,$layoutFactory); $this->_resultPageFactory = $resultPageFactory; } public function execute() { $resultPage = $this->_resultPageFactory->create(); $resultPage->getConfig()->getTitle()->prepend(__('Add visitor Tab')); $block = $resultPage->getLayout() ->createBlock('Magento\Framework\View\Element\Template') ->setTemplate('Bluethinkinc_AdminDashboardTab::visitor.phtml') ->toHtml(); $this->getResponse()->setBody($block); } } ?> |
Step 6: Create Grids.php
app/code/Bluethinkinc/AdminDashboardTab/Block/Dashboard/Grids.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace Bluethinkinc\AdminDashboardTab\Block\Dashboard; class Grids extends \Magento\Backend\Block\Dashboard\Grids { protected function _prepareLayout() { parent::_prepareLayout(); $this->addTab( 'visitor', [ 'label' => __('visitor'), 'url' => $this->getUrl('bluethinkinc/dashboard/visitor', ['_current' => true]), 'class' => 'ajax', 'active' => false ] ); } } ?> |
Step 7: Create Data.php
app/code/Bluethinkinc/AdminDashboardTab/Helper/Data.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace Bluethinkinc\AdminDashboardTab\Helper; use Magento\Customer\Model\Visitor; class Data extends \Magento\Framework\App\Helper\AbstractHelper { /** * This is a visitors * * @var visitors $visitors */ protected $visitors; public function __construct( Visitor $visitors ) { $this->visitors = $visitors; } public function visitorscount() { $collection = $this->visitors->getCollection(); $data=count($collection); return $data; } } ?> |
Step 8: Create visitor.phtml
app/code/Bluethinkinc/AdminDashboardTab/view/adminhtml/templates/visitor.phtml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $helper = $this->helper(Bluethinkinc\AdminDashboardTab\Helper\Data::class); $visitor = $helper->visitorscount(); ?> <table class="admin__table-primary dashboard-data" id="productsOrderedGrid_table"> <thead> <tr> <th class="data-grid-th col-product no-link col-name"> <div class="dashboard-item-title"><?php echo __("Total Visitor : ");?><span style='color: #eb5202;font-size: 2.4rem;'><?php echo $visitor; ?></span></div> </th> </tr> </thead> </table> |
Step 9: After create the module if you run the command as
php bin/magento module:status
You should see the module is disable now:
List of disabled modules: Bluethinkinc_AdminDashboardTab
enable the module right now, let run the command as:
php bin/magento module:enable Bluethinkinc_AdminDashboardTab
After Enable Module Then Run this Command
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento c:f
sudo chmod -R 777 var/ pub/static generated/
Now, please login admin section and see the result.
Mukesh Singh
2024-02-02