我需要使用自定义ga('send')
函数来发送在表单提交(特别是通过Marketo)上触发的数据。
在将GA作为脚本标签安装的网络媒体资源上,效果很好。但是,在一个站点上我需要这样做,GA是通过GTM作为标记安装的。这显然是最佳做法。
基本上,代码看起来像这样(针对上下文):
form.onSuccess(function (values, url) {console.log('success');
...ga('send', 'event', {eventCategory: 'form',eventAction: 'submit',eventLabel: 'Form Submit'});...return false;});
我做了一些研究
How to send ga(...) events with Google Tag Manager?
Google Analytics Code Explanation
前提是我首先需要定义ga()
函数-所以我这样做是这样的:
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;ga('create', "UA-3488044-2", 'auto');
form.onSuccess(function (values, url) {console.log('success');
...ga('send', 'event', {eventCategory: 'form',eventAction: 'submit',eventLabel: 'Form Submit'});...return false;});
我可以看到`console.log(“ success”),并且没有引发任何错误,但是我没有看到GA中记录了表单提交事件。
有什么想法吗?如何使用GA的Google跟踪代码管理器实现与脚本代码进行自定义事件?
在通过GTM标签实现GA的marketo表单页面上,那么您应该考虑为此使用custom datalayer events。
不必担心“ custom”一词,基本上可以代替ga()函数:
dataLayer.push({'event': 'event_name'});
所以您的代码变成:
form.onSuccess(function (values, url) {console.log('success');
...dataLayer.push({'event': 'form-submit'});...return false;});
THEN IN GTM: