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

My site tracks pageviews using Google Tag Manager and downloads the advanced e-commerce plugin on some pages using ga('require', 'ec'), but there is an error:

Uncaught ReferenceError: ga undefined

Code snippet:

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                                              
    });

What is wrong with him?

+4
source share
1 answer

GA loads after running the script.

GTM is asynchronous, so the tags included in it are not guaranteed to run before running the built-in scripts.

script HTML- GTM Analytics, script .

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

()

+10

Source: https://habr.com/ru/post/1671955/


All Articles