html 使用Google Analytics和beforeunload事件跟踪表单放弃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 使用Google Analytics和beforeunload事件跟踪表单放弃相关的知识,希望对你有一定的参考价值。

var FormAbandonmentTracker = {
  init: function(gtag, form_id, event_category, event_action = 'FormAbandonment') {
    this.$gtag = gtag;
    this.$eventCategory = event_category ? event_category : form_id;
    this.$eventAction = event_action;

    this.$formHistory = [];
    this.$formIsSubmitted = false;
    this.$formId = form_id;
    this.$form = document.getElementById(form_id);

    if(!gtag) {
      console.error("FormTracker Error: Please pass in your gtag object when initializing FormTracker.");
      return;
    }
    if(!this.$form) {
      console.error("FormTracker Error: Please specify the correct form id when initializing FormTracker.");
      return;
    }
    this.attachEvents();
  },
  attachEvents: function() {
    let that = this;
    this.$form.querySelectorAll('select').forEach(function(el) {
      el.addEventListener('change', function(e) { return that.onFieldChange(e); });
    });
    this.$form.querySelectorAll('input, textarea').forEach(function(el) {
      el.addEventListener('input', function(e) { return that.onFieldInput(e); });
    });
    this.$form.addEventListener('submit', function(e) { return that.onFormSubmit() });
    window.addEventListener('beforeunload', function(e) { return that.onFormAbandonment() } );
  },
  onFieldChange: function(event) {
    let field_id = event.target.id;
    this.addFieldToHistory(field_id);
  },
  onFieldInput: function(event) {
    let field_id = event.target.id;
    this.addFieldToHistory(field_id);
  },
  addFieldToHistory: function(field_id) {
    this.$formHistory.push(field_id);
    this.$formHistory = this.$formHistory.filter((v, i, a) => a.indexOf(v) === i).sort();
  },
  onFormSubmit: function() {
    this.$formIsSubmitted = true;
    this.clearFormHistory();
  },
  onFormAbandonment: function() {
    if(!this.$formIsSubmitted) {
      this.sendEvents();
    }
  },
  sendEvents: function() {
    let joined_history = this.$formHistory.join(', ');
    // Send the data off to Google
    this.$gtag('event', this.$eventAction, {
      'event_category': this.$eventCategory,
      'event_label': 'Fields with input: ' + joined_history,
      'event_callback': function() {
        console.log("Data sent to Google.");
      }
    });
  },
  clearFormHistory: function() {
    this.$formHistory = [];
  }
}
<script type="text/javascript">
    (function(){
      FormAbandonmentTracker.init(gtag, 'trackable-form', 'Test Form', 'FormAbandonment');
    })();
</script>

以上是关于html 使用Google Analytics和beforeunload事件跟踪表单放弃的主要内容,如果未能解决你的问题,请参考以下文章

转载Google Analytics 使用图文全攻略

html 使用Google Analytics跟踪HTML5视频观看次数

HTML HTML5主打Open Graph和Google Analytics

html 使用Google Analytics进行客户端错误记录

最小化的 Google Analytics 代码

Google Analytics(分析)iOS和Alamofire以及cocoapods