简体   繁体   English

在表单提交上触发谷歌分析事件

[英]Firing a google analytics event on form submit

I'm trying to add analytics event tracking to an onclick event for a submit input on a form. 我正在尝试将分析事件跟踪添加到onclick事件中,以便在表单上提交输入。

I've tried multiple different examples and referenced a couple of different SO posts to get to this point. 我尝试了多个不同的例子,并引用了几个不同的SO帖子来达到这一点。 I'm able to get the onclick to either submit the form OR fire the tracking event but not both. 我可以让onclick提交表单或触发跟踪事件,但不能同时触发。

First example: (Submits form + logs to console but doesn't fire event) 第一个例子:(将表单+日志提交到控制台,但不会触发事件)

<!-- Analytics tracking code -->

<form action="test.php" id="form" method="post">

    <input type="text" name="name" id="name">

    <!-- Submits form + logs to console but doesn't fire event -->

    <input type="submit" name="submit" value="Submit" onclick="
        var _this = this; 
        _gaq.push(
            ['_trackEvent', 'Forms', 'Submit', 'Home Contact'], 
            function() {
                console.log('submitting'); 
                $(_this).parents('form').submit();
            }
        );
    ">

</form>

Second example: (Fires event + logs to console but doesn't submit form) 第二个例子:(触发事件+日志到控制台但不提交表单)

<!-- Analytics tracking code -->

<form action="test.php" id="form" method="post">

    <input type="text" name="name" id="name">

    <!-- Fires event + logs to console but doesn't submit form -->

    <input type="submit" name="submit" value="Submit" onclick="
        var _this = this;
        _gaq.push(['_set','hitCallback',function(){
            console.log('submitting');
            $(_this).parents('form').submit();
        }]);
        _gaq.push(
            ['_trackEvent','My category','My action']
        );
        return !window._gaq;
    ">

</form>

I've also found that if I add return false to the end of the first example's onclick event it will fire the analytics event tracking but not submit the form. 我还发现,如果我将return false添加到第一个示例的onclick事件的末尾,它将触发分析事件跟踪但不提交表单。

(This really needs to be a FAQ...) (这真的需要成为FAQ ...)

Google Analytics records data by requesting an image from the analytics servers, with the tracking data as parameters on the image request. Google Analytics(分析)通过从分析服务器请求图像来记录数据,并将跟踪数据作为图像请求的参数。 If a new page is rendered in the current window immediately after a _trackEvent or _pageView , the image request can be canceled, and no data recorded. 如果在_trackEvent_pageView之后立即在当前窗口中呈现新页面,则可以取消图像请求,并且不记录任何数据。

The usual pattern is to add a small delay before loading a new page or submitting a form. 通常的模式是在加载新页面或提交表单之前添加一个小延迟。

Try 尝试

<input type="submit" name="submit" value="Submit" onclick="
  var _this = this; 
  _gaq.push(['_trackEvent', 'Forms', 'Submit', 'Home Contact']);
  setTimeout(function() {$(_this).parents('form').submit()}, 150);
  return false;"
>

My preference is bind to the jQuery submit event on the form itself, like 我的偏好是绑定到表单本身的jQuery提交事件,比如

$('#form').submit(function(e){
  e.preventDefault();
  _gaq.push(['_trackEvent', 'Forms', 'Submit', 'Home Contact']);
  var form = this;
  setTimeout(function(){ form.submit()}, 150);
});

It is possible with the new analytics.js (universal analytics). 可以使用新的analytics.js(通用分析)。

Links: 链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM