First I don’t want to suggest you shouldn’t use a plugin for this. I think for many people that would make the most sense. So please consider using a plugin.
However, if you want to do this through a child theme or a code snippet plugin I don’t suggest overriding templates because that makes you responsible to update those templates if an update takes place.
Instead, I suggest hooking into the top of your page using built-in WordPress hooks to place the tag manager code in the head tag just after the opening body tag. This is done by adding functions into your child theme functions.php file.
<?php
/**
* Add google tag manager code in head tag.
*/
function custom_add_gtm_code() {
?>
<!-- Add your GTM Code below this line -->
<?php
}
add_action( 'wp_head', 'custom_add_gtm_code' );
/**
* Add google tag manager noscript just after opening body tag.
*/
function custom_add_gtm_code_noscript() {
?>
<!-- Add your GTM noscript Code below this line -->
<?php
}
add_action( 'wp_body_open', 'custom_add_gtm_code_noscript' );