How To

How to get a list of events generated when you perform any action in Magento 2

You are probably familiar with a concept of an event whether you are a programmer or not. Basically, the event is an action that occurs in a response of the user interaction such as a user logged in, a user logged out, add to cart etc. And when an event occurs it passes data to observers and the observer runs the logic you have created.
But when there are a thousand lines of code and you want to create a particular event trigger, it becomes difficult to identify what existing events are triggered when you perform any action. To help Magento devs by saving their valuable time and efforts, we came up with a small code that will help you to get a list of events generated when you perform any Action.
To get a list of events we have to perform just two easy steps.

Firstly, we need to create ‘di.xml‘ file inside our extension folder at below path using following code.
app\code\Vendor\Extension\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <type name="Magento\Framework\Event\ManagerInterface">
        <plugin name="list_all_dispatched_event" type="Vendor\Extension\Plugin\ListDispatchedEvents" sortOrder="10" disabled="false"/>
    </type>
</config>

Now, we need to create on more file ‘ListDispatchedEvents.php‘ inside our extension folder.
app\code\Vendor\Extension\Plugin\ListDispatchedEvents.php

<?php
 
namespace Vendor\Extension\Plugin;
class ListDispatchedEvents
{
    public function beforeDispatch($subject, $eventName, array $data = [])
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/event.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($eventName);
    }
 
}

Tadaa! Your list of events is generated inside an event.log file which resides inside var/log folder. You can use this code anywhere to grab existing events list which triggered when you make any action.
And yes, please hit that stars if this code worked for you or comment down below if you face any issue.

Happy Coding?

Click to rate this post!
[Total: 18 Average: 4.7]
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.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

4 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago