未捕获的ReferenceError:ga未定义为ga('require','ec')

Lin*_*hai 11 google-analytics google-tag-manager

我的网站使用Google跟踪代码管理器跟踪综合浏览量,并在某些网页中加载增强型电子商务插件ga('require', 'ec'),但有一个错误:

未捕获的ReferenceError:未定义ga

代码片段:

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                                              
    });
Run Code Online (Sandbox Code Playgroud)

它出什么问题了?

小智 25

此脚本运行后,GA正在加载.

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

您可以将此脚本添加为GTM中的HTML标记,并将Universal Analytics标记设置为其要求,或者在页面头部添加更多脚本.

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

(资源)