javascript/ajax中的Google Analytics(分析)目标跟踪 [英] Google Analytics Goal tracking in javascript/ajax

查看:60
本文介绍了javascript/ajax中的Google Analytics(分析)目标跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站页面上有一个基于javascript/ajax的联系表.如果有人单击发送表格,我希望此点击由Google Analytics(分析)进行注册.为此,我创建了一个目标,由于某种原因,我无法使其正常工作.有帮助吗?

I have a javascript/ajax based contact form on a website page. If people click to send the form, I want this click to be registered by Google Analytics. I created a goal for this, for some reason I cannot get it to work. Any help?

表单的代码为:

        <form id="footer_quick_contact_form" name="footer_quick_contact_form" class="quick-contact-form" action="includes/quickcontact.php" method="post">
          <div class="form-group">
            <input id="form_email" name="form_email" class="form-control" type="text" required="" placeholder="E-mail">
          </div>
          <div class="form-group">
            <textarea id="form_message" name="form_message" class="form-control" required placeholder="message" rows="3"></textarea>
          </div>
          <div class="form-group">
            <input id="form_botcheck" name="form_botcheck" class="form-control" type="hidden" value="" />
            <button type="submit" class="btn btn-default btn-transparent text-gray btn-xs btn-flat mt-0" data-loading-text="One moment please...." onClick="ga('send', 'event', { eventCategory: 'Contact', eventAction: 'ContactRequest'});">Verstuur nu!</button>
          </div>
        </form>

        <!-- Quick Contact Form Validation-->
        <script type="text/javascript">
          $("#footer_quick_contact_form").validate({
            submitHandler: function(form) {
              var form_btn = $(form).find('button[type="submit"]');
              var form_result_div = '#form-result';
              $(form_result_div).remove();
              form_btn.before('<div id="form-result" class="alert alert-success" role="alert" style="display: none;"></div>');
              var form_btn_old_msg = form_btn.html();
              form_btn.html(form_btn.prop('disabled', true).data("loading-text"));
              $(form).ajaxSubmit({
                dataType:  'json',
                success: function(data) {
                  if( data.status == 'true' ) {
                    $(form).find('.form-control').val('');
                  }
                  form_btn.prop('disabled', false).html(form_btn_old_msg);
                  $(form_result_div).html(data.message).fadeIn('slow');
                  setTimeout(function(){ $(form_result_div).fadeOut('slow') }, 6000);
                }
              });
            }
          });
        </script>

如您所见,我在发送按钮上添加了一个单击事件.在Google Analytics(分析)中,我创建了一个目标,方法是转到管理>目标>新目标>自定义单选按钮>下一步.我给目标命名,选择事件"单选按钮并填写以下字段:

As you can see I added an on-click event to the send button. In google analytics I created a goal, by going to admin>goals>new goal>custom radio button>next. I gave the goal a name, selected the Event radio button and filled in the following fields:

类别:联系方式 行动:ContactRequest 标签:空 值:空

Category: Contact Action: ContactRequest Label: Empty Value: Empty

我以为我会解决它,但是直到现在我仍无法在GA中跟踪任何结果.有什么建议吗?

I thought I'd have fixed it, but until now I can't track any results in GA. Any suggestions?

推荐答案

在阅读您的评论后,看来问题在于您在点击事件处理程序中使用了错误的语法.

After reading your comment it would seem the problem is that you are using the wrong syntax in your click event handler.

您正在调用ga()函数,该函数是Universal Analytics代码的一部分,该代码在一段时间内已被gtag.js取代.

You are calling the ga() function, which is a part of the Universal Analytics Code, which for some time now has been replaced by gtag.js.

我通常不使用gtag.js(我更喜欢使用Google跟踪代码管理器),但是根据文档,正确的调用应如下所示:

I do not usually use gtag.js (I prefer to use Google Tag Manager), but according to the documentation the correct call would look like this:

gtag('event', 'contact_request', { // second parameter is event action
  'event_category': 'contact', 
  'event_label': '',
  'value': 0
});

(实际上,如果不需要标签和值,可以省略它们.)

(Actually you can leave out label and value if you do not need them).

这篇关于javascript/ajax中的Google Analytics(分析)目标跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆