Proper way to add external JavaScript file What is the proper way to add Amazon oneTag code to Drupal 8. Instructions say to add it before the ending body tag. 

I think it should be loaded to the /theme.libraries.yml. Here is the code I am trying to add: 

&lt;div id="amzn-assoc-ad-123456789"&gt;&lt;/div&gt;&lt;script async src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US&amp;adInstanceId=123456789"&gt;&lt;/script&gt;

Do I need to create a file onelink.js with this code? Then reference it in libraries? 

Thanks
It's actually pretty well documented: https://www.drupal.org/docs/8/theming-drupal-8/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-theme#external

MYTHEME.libraries.yml (or MYMODULE.libraries.yml)

amazon-onetag:
  js:
    //z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US&amp;adInstanceId=123456789: { type: external, attributes: { async: true } }


MYTHEME.theme (or MYMODULE.module)

function MYTHEME/MYMODULE_page_attachments_alter(&amp;$page) {
  $page['#attached']['library'][] = 'MYTHEME/amazon-onetag';
}




Now we could start to discuss what is "the proper way"?

As Amazon wants it directly before the closing body tag (which is irrelevant in my experience) and as they also put a nice container with an ID around the script (which very likely is irrelevant as well) you could also simply copy the whole snippet into your (custom or sub-)theme's html.html.twig directly before the closing &lt;body&gt; tag.

But that would at the same time take this script a little bit out of Drupal's influence to handle, preprocess and order all scripts according a desired logic. And it can become a little bit more difficult to maintain in the end.