How to send ga(…) events with Google Tag Manager?

北城以北 提交于 2021-01-28 08:51:34

问题


I'm changing google analytics for google tag manager on an existing website. This website also uses google e-commerce, so I have a few custom ga() calls related to the e-commerce being done on some pages.

The problem is that because the google analytics script loads asynchronously, ga() is not defined anymore when I'm trying to send some e-commerce related data. I've found a workaround somewhere:

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

But, although I've not the "ga is not defined" problem anymore, it still doesn't work.

I don't see anything on the google dashboard. I also don't see anything on the debugging messages (in the developer console).

What seems to be happening is that the GA script loaded with GMT is not using these variables I've set but I'm not sure how to fix it or if it is even possible to keep using my ga() calls with GMT.

I know that I can do it with dataLayer but I'm trying to avoid to rewrite a bunch of working code just for this.


回答1:


I have seen an implementation that does it like this:

  window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
  ga('create', UA-XXXXXXXXXX, 'auto');
  ga('require', 'ecommerce');

maybe that works out for you as well.

if you use enhanced e-commerce then the code below the "ga('create..." hast to change from:

ga('require', 'ecommerce');

to:

ga('require', 'ec');

The order is important here, events can be send after those functions.



来源:https://stackoverflow.com/questions/50487788/how-to-send-ga-events-with-google-tag-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!