Uncaught ReferenceError: ga is not defined with ga('require', 'ec')

Uncaught ReferenceError: ga is not defined with ga('require', 'ec')

我的站点使用Google标签管理器跟踪浏览量,并在某些页面使用ga('require', 'ec')加载增强型电子商务插件,但出现错误:

Uncaught ReferenceError: ga is not defined

代码段:

ga('require', 'ec');
function onProductClick(product, url, key) {
    ga('ec:addProduct', {
        'id': "'" + product.product_id + "'",                        
        'name': product.model_name,                                  
        'category': product.series_name + '/' + product.dealer_name, 
        'brand': product.brand_name,                                 
        'variant': product.model_name,                               
        'position': key                                              
    });

怎么了?

GA 在此脚本 运行 秒后加载。

GTM 是异步的,因此内联脚本运行 之前,不保证其中包含的标签运行。

您可以将此脚本添加为 GTM 中的 HTML 标记,并将 Universal Analytics 标记设置为其要求,或者 add some more script 到您的页面头部。

<script>
  window['GoogleAnalyticsObject'] = 'ga';
  window['ga'] = window['ga'] || function() {
    (window['ga'].q = window['ga'].q || []).push(arguments)
  };
</script>

(Source)