I am using magento2.3.5-p2 community edition ( open source )
I want to create a custom Controller so that I can post data using ajax to the Controller function created
client side will call the controller action like this
$.ajax({
url: "<?php echo $this->getUrl('googletagmanager/gtm'); ?>",
type: 'POST',
success: function (response) {
console.log(response);
}
});
the link to get the controller path will be <?php echo $this->getUrl('googletagmanager/gtm'); ?>
Thus to create this link googletagmanager/gtm
I need to create a new module and put in the Controller class name Gtm.php
I have created a new module in
app/code/GShop/GoogleTagManager
I created a routes.xml
file inside it in path app/code/GShop/GoogleTagManager/etc/frontend/routes.xml
and the content as below
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="googletagmanager" frontName="googletagmanager">
<module name="GShop_GoogleTagManager"/>
</route>
</router>
</config>
I created a module.xml
inside it at path app/code/GShop/GoogleTagManager/etc/module.xml
and the content as below
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="GShop_GoogleTagManager" setup_version="1.1.0"/>
</config>
I created a registration.php
at path app/code/GShop/GoogleTagManager/registration.php
and content as below
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'GShop_GoogleTagManager',
__DIR__
);
I created the controller at app/code/GShop/GoogleTagManager/Controller/Gtm.php
with content as below
<?php
namespace GShop\GoogleTagManager\Controller;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;
class Gtm extends Action
{
protected $resultFactory;
protected $customerSession;
private $gtmLogger;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Customer\Model\Session $customerSession,
\GShop\GoogleTagManager\Model\Logger $gtmLogger
) {
$this->resultFactory = $resultFactory;
$this->logger = $logger;
parent::__construct($context);
$this->customerSession = $customerSession;
$this->gtmLogger = $gtmLogger;
}
public function execute()
{
$data = [];
/** User successful login */
if ($this->customerSession->getGTMSuccessfulLogin()) {
$data[] = $this->customerSession->getGTMSuccessfulLogin();
}
$this->customerSession->setGTMSuccessfulLogin(null);
/** User unsuccessful login */
if ($this->customerSession->getGTMUnsuccessfulLogin()) {
$data[] = $this->customerSession->getGTMUnsuccessfulLogin();
}
$this->customerSession->setGTMUnsuccessfulLogin(null);
/** Newsletter signup successful */
if ($this->customerSession->getGTMSuccessfulNewsletterSignup()) {
$data[] = $this->customerSession->getGTMSuccessfulNewsletterSignup();
}
$this->customerSession->setGTMSuccessfulNewsletterSignup(null);
/** Newsletter signup unsuccessful */
if ($this->customerSession->getGTMUnsuccessfulNewsletter()) {
$data[] = $this->customerSession->getGTMUnsuccessfulNewsletter();
}
$this->customerSession->setGTMUnsuccessfulNewsletter(null);
/** Product View */
if ($this->customerSession->getGTMProductView()) {
$data[] = $this->customerSession->getGTMProductView();
}
$this->customerSession->setGTMProductView(null);
/** Wishlist Product Add */
if ($this->customerSession->getGTMWishlistProductAdd()) {
$data[] = $this->customerSession->getGTMWishlistProductAdd();
}
$this->customerSession->setGTMWishlistProductAdd(null);
/** Wishlist Product Remove */
if ($this->customerSession->getGTMWishlistProductRemove()) {
$data[] = $this->customerSession->getGTMWishlistProductRemove();
}
$this->customerSession->setGTMWishlistProductRemove(null);
/** Wishlist Product Update */
if ($this->customerSession->getGTMWishlistProductUpdate()) {
$data[] = $this->customerSession->getGTMWishlistProductUpdate();
}
$this->customerSession->setGTMWishlistProductUpdate(null);
/** Wishlist Product Share */
if ($this->customerSession->getGTMWishlistProductShare()) {
$data[] = $this->customerSession->getGTMWishlistProductShare();
}
$this->customerSession->setGTMWishlistProductShare(null);
/** User successful register */
if ($this->customerSession->getGTMSuccessfulRegister()) {
$data[] = $this->customerSession->getGTMSuccessfulRegister();
}
$this->customerSession->setGTMSuccessfulRegister(null);
/** User unsuccessful register */
if ($this->customerSession->getGTMUnsuccessfulRegister()) {
$data[] = $this->customerSession->getGTMUnsuccessfulRegister();
}
$this->customerSession->setGTMUnsuccessfulRegister(null);
// Get Item Deleted From Cart
if ($this->customerSession->getGTMDeleteFromCart()) {
$data[] = $this->customerSession->getGTMDeleteFromCart();
}
$this->customerSession->setGTMDeleteFromCart(null);
// Get Event Of Wishlist Item Added To Cart
if ($this->customerSession->getGTMWishListItemAddedToCart()) {
$data[] = $this->customerSession->getGTMWishListItemAddedToCart();
}
$this->customerSession->setGTMWishListItemAddedToCart(null);
// Get Event Of Wishlist All Item Added To Cart
if ($this->customerSession->getGTMWishListAllItemAddedToCart()) {
$data[] = $this->customerSession->getGTMWishListAllItemAddedToCart();
}
$this->customerSession->setGTMWishListAllItemAddedToCart(null);
// Get Event Checkout Cart Quantity Added
if ($this->customerSession->getGTMCheckoutAddToCart()) {
$data[] = $this->customerSession->getGTMCheckoutAddToCart();
}
$this->customerSession->setGTMCheckoutAddToCart(null);
// Get Event Checkout Cart Quantity Decreased
if ($this->customerSession->getGTMCheckoutRemoveFromCart()) {
$data[] = $this->customerSession->getGTMCheckoutRemoveFromCart();
}
$this->customerSession->setGTMCheckoutRemoveFromCart(null);
// Clear Checkout Event If Checkout Stage Of GTM Not In Step 4
$clear_checkout_session = true;
if($this->customerSession->getGTMInCheckoutStage()){
if($this->customerSession->getGTMInCheckoutStage() == 4){
$clear_checkout_session = false;
$data[] = $this->customerSession->getGTMProceedToCheckout();
$this->customerSession->setGTMInCheckoutStage(null);
}
}
if($clear_checkout_session){
$this->customerSession->setGTMInCheckoutStage(null);
$this->customerSession->setGTMProceedToCheckout(null);
}
// $this->gtmLogger->info($data);
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($data);
return $resultJson;
}
}
I had make sure to follow this step in making the route
https://devdocs.magento.com/guides/v2.4/extension-dev-guide/routing.html
But I can't seem to figure out which part I miss that makes the URL cannot be accessed, it always throws out error 404
Can anyone please help me point out any places that I miss or guide me to understand why it is not working
Any help is kindly appreciated, thank you