I understand I can hard-code this snippet into html.html.twig but i would prefer a cleaner method. My research is only instructing me how to attach libraries etc, but I can't find any example of injecting code into the header area needed by some third-party services.
I tried the following code, but it doesn't seem to work.
/** * Implements hook_page_attachments(). */ function THEME_page_attachments(&$page) { /** * Initial JS. */ $javascript_header = "-----js code -----"; $page['#attached']['html_head'][] = [ // The data. [ // Add a <script> tag. '#tag' => 'script', // Add JavaScript to the <script> tag. '#value' => \Drupal\Core\Render\Markup::create($javascript_header), // Give weight so it appears after meta tags, etc. '#weight' => -1, ], 'key' ]; }