Install GTM in pure javascript through functions.php

The problem is that your code is inside the <head> element, but the code is trying to access document.body which won’t exist at the time that your script is run. If I recall correctly you’re supposed to place this code immediately after the opening <body> tag. To do this from functions.php your theme needs to support the new wp_body_open() hook.

header.php:

<body <?php body_class(); ?>>
<?php wp_body_open(); ?>

functions.php

add_action( 'wp_body_open', 'webplus_hook_gtm' );