You are using onAfterRender event which is not suitable for modifying the document object because this event is triggered after the page has already been rendered. Use onAfterDispatch, onBeforeRender or onBeforeCompileHead instead.
$document->addScriptDeclaration() method is working, no problem with that.
window.event is deprecated:
instead of that you should use document.addEventListener() or window.addEventListener() on the event of DOMContentLoaded
Plugins can be fickle things to get right, so here's a short guide to getting your plugin working successfully.
Identify the Plugin Type and Event
Firstly work out which event you need to subscribe to.
The events which are emitted by Joomla are listed in https://docs.joomla.org/Plugin/Events, and these are segregated by plugin type. Unfortunately it's not ...
If I were you I would try this instead of onBeforeRender:
That way you are adding the meta to the document prior to it being rendered.
Here is an excellent video explaining all the different plugin types, but admittedly it can take a while to get your head around them. https://www.youtube.com/watch?v=_agXP5DHm_g&list=...