Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Uncaught ReferenceError: ga is not defined

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's wrong with it?

like image 894
Lina Zhai Avatar asked Mar 10 '17 04:03

Lina Zhai


Video Answer


1 Answers

GA is loading after this script runs.

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

You could add this script as an HTML tag in GTM and set the Universal Analytics tag as a requirement for it, or add some more script to your page's head.

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

(Source)

like image 126
Chris Combs Avatar answered Sep 19 '22 11:09

Chris Combs