This link explains how to add HTML content to the header (Since you want to add Javascript make sure you use the script tag)
From what I recall though the best way to do it universally would be to create a Bubble plugin and put it in there
To do so you will need to open the Plugin editor of your newly created Bubble plugin…you will then see a Shared tab which has a place to enter the HTML Header (Make sure to add your Javascript within a script tag) and if I am not mistaken this HTML gets added to all your pages by default. Also remember to install the newly created Bubble plugin to your app.
Cheers!
EDIT: I just realized you can not have dynamic content added via in the Bubble plugin method…
I think you will need to use something along the lines of a “Reusable Header Element” to get what you are wanting…not sure though.