I’m doing a Wordpress theme and I want to incorporate the following script in the head:
<! - Global site tag (gtag.js) - Google Analytics ->
<script async src = "https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"> </ script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag () {dataLayer.push (arguments);}
gtag ('js', new Date ());
gtag ('config', 'UA-XXXXXXXX-1');
</ script>
Automatically Pinegrow places the two scripts separately, but when I save and export the theme, then in the code only the second script appears and Google Analytics does not receive information.
HTML shows only this, the first part is missing:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-1');
</script>
I don’t know the reason why I can not incorporate a script manually, it has no sense…
I think it is not necessary to use a plugin just for that (I always try to avoid plugins).
Does anyone know any other solution?
I’ve entered the code from the Library option (left column) using the “insert code” option.
Then I drag it in the head and it’s when appears two script.
At the time of export the first script does not appear.
Check your functions.php file. I think PG strips those scripts and puts them there.
wp_deregister_script( 'script-1' );
wp_enqueue_script( 'script-1', 'https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1', false, null, false);
wp_add_inline_script( 'script-1', 'window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(\'js\', new Date());
gtag(\'config\', \'UA-XXXXXXXX-1\');');```
I'm not sure if there is a way to force them to be kept in the head section.