module – How to handle dynamic data in a script tag in head section for every page

I am absolutely new to Magento 2 (1 week in). I am trying to develop a Magento module that lets the admin specify a string (token) and when the admin saves the module I would like the token to inserted together with a tag into the section of each page.

If the admin changes the token and saves the config, it would be nice if it can update the same tag. Or even remove it if no token is specified.

The module UI is no problem and also the saving works fine and intercepting the token value (with PHP), but I got stuck on how exactly to control the that gets inserted.

I experimented with view/frontend/layout/default.xml and can influence the header, but this seems to be very static.

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <script src="https://some-external-website.com/external-script.js" 
                data-token="token_value" 
                src_type="url"></script>
    </head>
</page>

So basically I would like to change the value in data-token (“token_value”) when the admin saves the config from the module, which I do from a PluginConfig file:

    ...
    public function aroundSave(MagentoConfigModelConfig $subject, callable $proceed) {
        // your custom logic
        $section = $subject->getSection();
        $returnValue = $proceed();
        $newConfigs = $this->scopeConfig->getValue($section);
        $token = $newConfigs("general")("token_value");

        // +++ here would like to write value of $token to the <script> tag +++
        
        return $returnValue;
    }
    ...

Any help appreciated!