简体   繁体   English

用于事件跟踪的Google Analytics Onclick按钮

[英]Google Analytics Onclick button for Event Tracking

I have an index page and I am planning to put a Subcribe button below. 我有一个索引页面,我打算在下面放置一个“订阅者”按钮。 When the viewers key in their email and click on the button. 当观众键入他们的电子邮件并单击按钮时。 The record is supposed to be an event the Google analytics that is allowed me to track. 该记录应该是允许我跟踪的Google Analytics(分析)事件。 However,I am having the error in getting the data from this. 但是,我在从中获取数据时出错。 The code is below: 代码如下:

The code is below: 代码如下:

    <script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-87810245-1', 'auto');
  ga('send', 'pageview');

</script>
      <script>

$('#sub-form').on('post', function(event) {
  // Prevent the browser's default form submission action.
  event.preventDefault();

  ga('send', 'event', {
    eventCategory: 'subscribe',
    eventAction: 'post',
    eventLabel: 'subscribe sent',
    hitCallback: function() {
      $('sub-form').trigger('post');
    }
  });
});
      </script>

The code for the button: 该按钮的代码:

<div class="jumbotron text-center">
  <p>We specialize in all flower arrangements</p> 
  <form class="form-inline" id="sub-form">
    <div class="input-group">
      <input type="email" class="form-control" size="50" placeholder="Email Address" required>
      <div class="input-group-btn">
        <button type="post" class="btn btn-danger" id="post" onclick="ga('send', 'event', 'subscribe', 'post', 'subscribe sent', 10);">Subscribe</button>
      </div>
    </div>
  </form>
</div>

However, the result is not shown in the Google Analytics. 但是,结果不会显示在Google Analytics(分析)中。 在此处输入图片说明

The form result is my another event. 表单结果是我的另一个事件。

The script should be initiated first and then you should invoke your $('#sub-form'). 应该先启动脚本,然后再调用$('#sub-form')。 Check your jquery initialisation too. 也检查您的jQuery初始化。

BTW: Did you look at this tool? 顺便说一句:您看过这个工具吗? www.customerlabs.co - it can get your job done in less than 5 mins without the need of the coding. www.customerlabs.co-它可以在不到5分钟的时间内完成您的工作,而无需编写代码。

It is working now after I remove my form class. 删除表单类后,它现在可以正常工作。 I changed it to <form action="thankyou.html" id="sub-form"> 我将其更改为<form action="thankyou.html" id="sub-form">

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

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