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?
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>