0

I have a custom module . In my observer i have to know the current user duration & current user ip in fronted . So i write the code for user ip . Now if a user visit my website i want to get is this is the first page he visited in my website.

For example i have home , about, contact page . So when customer first time visit home then i have to get count=1 , if customer visiting about after home page then count =2 , if customer visiting contact after about then count=3 etc. Is there any method i can get this ?

Or visiting pages i want to know how much duration he is in the website . If the visitor closed window and take the website again the time should start from zero .

I have to solve this question in order to get answer for getStoreCurrency VS getCurrentCurrency ( compare store currency & getCurrentCurrency)

Please help.

0

Could you just use a session variable for this? Maybe add to it a cookie only valid for the current session. If that cookie doesn’t already exist: initialize counter with 1 and any other data you want to capture (landing page, referrer, etc); otherwise, increment the current value.

0

Please try the below code:

app/code/Shasha/CustomModule/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <event name="controller_action_predispatch">
      <observer name="browser_predispatch" instance="Shasha/CustomModule\Observer\BrowserPredispatch" shared="false" />
  </event>
</config>

app/code/Shasha/CustomModule/Observer/BrowserPredispatch.php

<?php

namespace Shasha\CustomModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;


class BrowserPredispatch implements ObserverInterface {

    protected $_session;
    
    protected $request;
    
    
    public function __construct(
        \Magento\Framework\Session\SessionManagerInterface $session,
        \Magento\Framework\App\Request\Http $request,

    )
    {
        $this->_session     =    $session;
        $this->request      =    $request;
    }


    /*
     * Observer that works on every page load
     *
    */
    public function execute(Observer $observer)
    {
        $request        = $observer->getEvent()->getRequest();
        $controller     = $this->request->getControllerName();
        $module         = $this->request->getModuleName();
        if($module == 'cms'){
            // write here your counter logic to set in session or cookie
        }
                
        $visitor      = $this->_session->getVisitorData(); // visitor all details
        
                        
    }
    
}

I hope it helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.