<commit_before>var global = require('global')
var elementUtils = require('../utils/element')
var assign = require('../utils/lodash').assign

module.exports = {
  configure: function () {
    this._clickTrackingInstalled = false
  },

  trackClicks: function (trackClicksOptions) {
    if (this._clickTrackingInstalled) return

    var instance = this
    var options = assign({
      element: global.document.body,
      ignoreAttribute: 'td-ignore',
      onClick: function (event, data) {
        return data
      }
    }, trackClicksOptions)
    var treeHasIgnoreAttribute = elementUtils
      .createTreeHasIgnoreAttribute(options.ignoreAttribute)
    var removeClickTracker = elementUtils
      .addEventListener(options.element, 'click', clickTracker)
    instance._clickTrackingInstalled = true
    return function () {
      removeClickTracker()
      instance._clickTrackingInstalled = false
    }

    function clickTracker (e) {
      var target = e.target || e.srcElement
      if (
        !treeHasIgnoreAttribute(target) &&
        !elementUtils.shouldIgnoreElement(target)
      ) {
        var elementData = elementUtils.getElementData(target)
        var data = options.onClick(e, elementData)
        if (data) {
          instance.trackEvent('clicks', data)
        }
      }
    }
  }
}<commit_msg>Improve readability and use more element utils with clicks plugin<commit_after>var global = require('global')
var elementUtils = require('../utils/element')
var assign = require('../utils/lodash').assign
var disposable = require('../utils/misc').disposable

function defaultOnClick (event, data) {
  return data
}

function configure () {
  this._clickTrackingInstalled = false
}

function trackClicks (trackClicksOptions) {
  if (this._clickTrackingInstalled) return

  var instance = this
  var options = assign({
    element: global.document,
    ignoreAttribute: 'td-ignore',
    onClick: defaultOnClick
  }, trackClicksOptions)

  var treeHasIgnoreAttribute = elementUtils
    .createTreeHasIgnoreAttribute(options.ignoreAttribute)

  var removeClickTracker = elementUtils
    .addEventListener(options.element, 'click', clickTracker)

  instance._clickTrackingInstalled = true
  return disposable(function () {
    removeClickTracker()
    instance._clickTrackingInstalled = false
  })

  function clickTracker (e) {
    var target = elementUtils.getEventTarget(e)
    if (
      !treeHasIgnoreAttribute(target) &&
      !elementUtils.shouldIgnoreElement(target)
    ) {
      var elementData = elementUtils.getElementData(target)
      var data = options.onClick(e, elementData)
      if (data) {
        instance.trackEvent('clicks', data)
      }
    }
  }
}

module.exports = {
  configure: configure,
  trackClicks: trackClicks
}
{'fraction_non_alphanumeric': 0.1007611453425154, 'fraction_numerical': 0.0, 'mean_word_length': 3.437299035369775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6472490', 'n_tokens_mistral': 776, 'n_tokens_neox': 761, 'n_words': 167}