How to add a specific script to the head section of WordPress theme

I want to add different Google Analytics scripts (I prefer not using any plugin), for each blog in my multisite network and all blogs using different sub domains and with one theme.
let say I have

one.example.com 
two.example.com 
three.example.com

Right now to apply all Google Analytics script to all sub domains I wrote the script inside header.php main theme (I know this is not a good practice) so now I want to add Google analytics script using child theme in the functions.php

Read More

what is the best approach to do this? using require_once or write it inside child’s functions.php or anything else?

I use this right now inside the main theme’s header.php

<?php if ($_SERVER['SERVER_NAME'] == 'one.example.com'){ ?>

<script>
// GA script for one.example.com
</script>

<?php }elseif ($_SERVER['SERVER_NAME'] == 'two.example.com'){ ?>

<script>
//GA script for two.example.com
</script>

<?php } ?>

Related posts