Magento 2 core session variable unable to fetch first time when user comes from 3rd party website

We have to fetch and set a session variable when the user redirects to our magento2 website using session-id from 3rd party site.

It looks like http://magento2.com/?SID=322e00bfqm3g6rfpjumah3939n

For this, we have written a custom router & match method but it’s not triggering properly at the first time.

Could you please review the below logic and files and advise please.

Note: The implementation is in the same module only.
The concept of this implementation is if the user redirecting to the M2 website from 3rd party we need to save fetch the SID (session-id) & and the user will browse the products on the M2 website. who are coming from 3rd party we don’t want to display proceed to checkout button.

I have written a custom router and setting array value in the core session and trying to fetch that value in the plugin which is in the same module.

  1. app/code/Vendor/ModuleName/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoFrameworkAppRouterList">
        <arguments>
            <argument name="routerList" xsi:type="array">
                <item name="redirect_if_sid" xsi:type="array">
                    <item name="class" xsi:type="string">VendorModuleNameControllerRouterRewriteSID</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">20</item>
                </item>
            </argument>
            <argument name="cacheable" xsi:type="boolean">false</argument>
        </arguments>
    </type>
    <! -- For Proceed to checkout button -->
    <type name="MagentoCheckoutHelperData">
        <plugin name="CartPageProceeedtoToCheckoutButton" type="VendorModuleNamePluginAfterProceedToCheckoutButton" disabled="false"/>
    </type>
</config>
  1. Vendor/ModuleName/Controller/Router/RewriteSID.php
<?php
namespace VendorModuleNameControllerRouter;
use PsrLogLoggerInterface;
use MagentoFrameworkAppActionFactory;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkDataFormFormKey;
use MagentoFrameworkSessionSessionManagerInterface;
class RewriteSID implements MagentoFrameworkAppRouterInterface
{   

    private $logger;
    private $actionFactory;
    private $response;
    private $formKey; 
    private $coreSession;    

    public function __construct(
        LoggerInterface $logger,
        ActionFactory $actionFactory,
        ResponseInterface $response,
        FormKey $formKey,
        SessionManagerInterface $coreSession

    ) {
        $this->logger = $logger;
        $this->actionFactory = $actionFactory;
        $this->response = $response;
        $this->formKey = $formKey;
        $this->coreSession = $coreSession;

    }

    public function match(MagentoFrameworkAppRequestInterface $request)
    {
        $identifier = $request->getOriginalPathInfo();
        $condition = new MagentoFrameworkDataObject(['identifier' => $identifier, 'continue' => true]);
        $identifier = $condition->getIdentifier();
        if ($condition->getRedirectUrl()) {
            $this->response->setRedirect($condition->getRedirectUrl());
            $request->setDispatched(true);
            return $this->actionFactory->create('MagentoFrameworkAppActionRedirect');
        }
        if (!$condition->getContinue()) {
            return null;
        }
        if ($request->getParam('SID')) {

            $sessionData = array("Volvo", "BMW", "Toyota");
           try {  
                    $this->coreSession->setVIEUserData($sessionData);
            }catch(Exception $e) {
                    $this->logger->info("Something went wrong");
                    $this->logger->info($e->getMessage());
            }

        }//get params ends here
        else
        {
            return null;
        }  
    }

}
  1. app/code/Vendor/ModuleName/Plugin/AfterProceedToCheckoutButton.php
<?php
namespace VendorModuleNamePlugin;
use MagentoFrameworkSessionSessionManager;
class AfterProceedToCheckoutButton
{


    private $coreSession;

    public function __construct(
        SessionManager $sessionManager
    ) {
        $this->coreSession = $sessionManager;    
    }

    /**
     * @param $subject
     * @param $result
     * @return mixed
    */
    public function afterCanOnepageCheckout(MagentoCheckoutHelperData $subject, $result)
    {
        //Here $this->coreSession->getVIEUserData() unable to fetch here

        if($result) {
            if($this->coreSession->getVIEUserData()) {
                return false;
            }
            return $result;
        }

    }
}