So within administrator function.
echo("<script>console.log('PHP: " . $data . "');</script>");
It seems it doesn't insert the js script on the page. This works from a simple PHP file, but not from Joomla. Any ideas?
You can use Joomla's Web Asset Manager to insert inline scripts via PHP, like so:
use Joomla\CMS\Factory;
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->addInlineScript('console.log("PHP: ' . $data . '")');