最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Using Google Analytics in an enqueued JS file

programmeradmin2浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论