Add variable to twig context
So the way Twig works is that it populates a Twig Environment with a context of global variables.

Then any time a template includes another template, the context is passed down to it.

So if you want something to be globally available you have to insert it into the Twig environment before Twig is used, for it to be truly globally available.

You can do that via a Twig extension that implements GlobalsInterface like this:

https://github.com/nystudio107/craft-seomatic/blob/v3/src/twigextensions/SeomaticTwigExtension.php#L36

So you might need to revise how you're doing it actually, and instead of using View::EVENT_BEGIN_PAGE you can just do whatever you need to do inside your Twig extension.