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&adInstanceId=123456789: { type: external, attributes: { async: true } }
MYTHEME.theme (or MYMODULE.module)
function MYTHEME/MYMODULE_page_attachments_alter(&$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 <body>
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.