How to add a script at the beginning of page header?

You have to do it in the Head: Customize => Theme => Edit CSS/HTML => Head

Or create a theme component with the script (the process to add it to the head is the same) and add it to your theme: Beginner's guide to using Discourse Themes