Skip to content

Tracking User Interactions, Saving them to Database Table and then send it to GA4 . Events and data events are saved as null in my table

This is the function that captures some data

protected function captureRequestData(RequestInterface $oRequest, $aPostParams)
    {

        return [
            'request' => [
                'status'      =>'pending',
                'params'      => json_encode($aPostParams),
                'endpoint'    => $oRequest->getRequestUri(),
                'view'        => json_encode($oRequest->getHeaders()->toArray()),
                'event'       => $this->_oEventobserver->getEventName(),
                'data'        => json_encode($this->_oEventobserver->getEventData() ),
                'ip'          => $oRequest->getClientIp(),
                'session'     => $this->_oCustomerSession->getCustomer()->getName(),
                'customer_id' => $this->_oCustomerSession->getCustomerId(),
                'created_at'  => date('Y-m-d H:i:s'),
                'updated_at'  => date('Y-m-d H:i:s')
            ],
        ];
    }

This is the function that saves them to database table

protected function saveApiRequestData(array $aData)
    {
        $connection = $this->_oApiRequestModel->getResource()->getConnection();
        $sTableName="data_saves";
        $aData = [
            'status' => $aData['request']['status'],
            'params' => $aData['request']['params'],
            'endpoint' => $aData['request']['endpoint'],
            'view' => $aData['request']['view'],
            'event' => $aData['request']['event'],
            'data' => $aData['request']['data'],
            'ip' => $aData['request']['ip'],
            'session' => $aData['request']['session'],
            'customer_id' => $aData['request']['customer_id'],
            'created_at' => $aData['request']['created_at'],
            'updated_at' => $aData['request']['updated_at'],
        ];
       $connection->insert($sTableName,$aData);
    }

Observer code

 /**
     * @var ManagerInterface
     */
    protected $eventManager;
    protected $eventName;
    protected $eventData;

    /**
     * YourObserver constructor.
     *
     * @param ManagerInterface $eventManager
     */
    public function __construct(
        ManagerInterface $eventManager
    ) {
        $this->eventManager = $eventManager;
    }
    /**
     * @inheritDoc
     */
    public function execute(Observer $observer)
   {

        $this->eventData = $observer->getEvent()->getData();
        $this->eventName = $observer->getEvent()->getName();

    }
    public function getEventName()
    {
        return $this->eventName;
    }
    public function getEventData()
    {
        return $this->eventData;
    }