Sha256: adb48f26e1cb00fa5e773c1285e08a7abbb14433d1d3fd0fa879a0551f9118c2

Contents?: true

Size: 1.29 KB

Versions: 12

Compression:

Stored size: 1.29 KB

Contents

'use strict'

/**
 * Wrapper for segment's track function that will track multiple elements,
 * normalize parameters, and easily switch between tracking links or events.
 * @param  {String} selector - query selector, multi element compatible
 * @param  {Function} cb - optional function that should return params, and will receive the element as a parameter
 * @param  {Boolean} [link] - if true, tracks a link click
 */
function track(selector, cb, link) {
  each(document.querySelectorAll(selector), function(el) {
    var params = cb
    if (typeof cb === 'function') {
      params = cb(el)
    }
    var event = params.event
    delete params.event
    if (link) {
      analytics.trackLink(el, event, params)
    } else {
      el.addEventListener('click', function() {
        analytics.track(event, params)
      })
    }
  })
}

/**
 * Iterates through a NodeList, not built-in for all browsers.
 * (https://developer.mozilla.org/en-US/docs/Web/API/NodeList)
 * @param  {NodeList} list a NodeList instance
 * @param  {Function} cb a function to execute for each node
 */
function each(list, cb) {
  for (var i = 0; i < list.length; i++) {
    cb(list[i], i)
  }
}

// Expose as commonjs for module bundlers if needed
if (typeof module !== 'undefined' && module.exports) {
  module.exports = { track: track }
}

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
middleman-hashicorp-0.3.47 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.46 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.45 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.44 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.43 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.41 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.40 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.39 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.38 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.37 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.35 assets/javascripts/hashicorp/analytics.js
middleman-hashicorp-0.3.34 assets/javascripts/hashicorp/analytics.js