I have a JS file custom-head.js
that I have enqueued to run in the header as noted in functions.php
here:
function enqueue_files() {
wp_enqueue_script( 'head_scripts', get_stylesheet_directory_uri() . '/js/custom-head.js', array('jquery'), null, false);
}
add_action( 'wp_enqueue_scripts', 'enqueue_files' );
Within that file I want to include my Google Analytics code. Yes, I realize I can add it to header.php or functions.php, but ideally I want to keep all of my scripts that are designed to be in the header in this one custom-head.js
file.
//Global site tag (gtag.js) - Google Analytics
jQuery(document).ready(function($) {
async src=";
//check if on the live domain versus dev
if (document.location.hostname == 'website') {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxxxx');
}
});
However, I am not getting the tag to fire correctly. Can you please advise on how to add these scripts?