简体   繁体   English

使用Parsley提交的自定义事件

[英]Custom Event Using Parsley Submit

I'm trying to add a custom event to a form for Google Analytics tracking. 我正在尝试将自定义事件添加到Google Analytics跟踪的表单中。 The form is using Parsley to validate. 表单使用Parsley进行验证。 I'm confused on where to add the snippet: 我很困惑在哪里添加片段:

dataLayer.push({'event' : 'signUpForm'});

into the javascript: 进入javascript:

    initParsley: function()
{
    $body.find('form.js-validate').parsley();
},

Does anyone know a good way to handle this, or do I need to provide more information? 有没有人知道处理这个的好方法,还是我需要提供更多信息?

Take a look Parsley Events Demo 看看Parsley Events Demo

I haven't worked with parsley before, but it looks like you can do something like this: 我之前没有和欧芹一起工作,但看起来你可以这样做:

initParsley: function () {
    $body.find('form.js-validate')
         .parsley()
         .subscribe('parsley:form:validate', function (formInstance) {
            if (formInstance.isValid()) {
                dataLayer.push({
                    'event': 'signUpForm'
                });
                return;
            }
        });
},

So when the form is submitted, you're subscribing to the parsley:form:validate event, and passing-in the formInstance to actual check to see if the form is valid. 因此,当提交表单时,您正在订阅parsley:form:validate事件,并将formInstance传递给实际检查以查看表单是否有效。 If the form is valid, then you're pushing your dataLayer event to GTM, and then submitting the form. 如果表单有效,那么您将dataLayer事件推送到GTM,然后提交表单。 Nice thing about this is that it will only send the event if the form is valid - which I believe is what you're after. 关于这一点的好处是它只会在表格有效的情况下发送活动 - 我相信这是你所追求的。

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

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