
<% helpers.includeJs("jquery.ba-dotimeout.min.js") %>

<% if (!helpers.isGASuppressed() && appjet.config.disableAnalytics != "true") { %>
<%- helpers.mixPanel() %>
<%- helpers.googleAnalytics() %>


<script type="text/javascript" nonce="<%= helpers.cspNonce() %>">
//<![CDATA[

var logSearchPerformed = (function () {
  var lastSearchPerformed = null;
  return function (query, numResults, label) {
    var trimmedQuery = $.trim(query);
    $.doTimeout('log-search-query', 800, function () {
      if (trimmedQuery && (trimmedQuery != lastSearchPerformed)) {
        trackEvent("search_performed", null /* action */, label /* label */, { query: query, numResults: numResults });
        lastSearchPerformed = trimmedQuery;
      }
    });
  }
})();

function trackEvent(eventName, action, label, extras) {
  var dict = extras || {};
  dict["action"] = action;
  dict["label"] = label;
  mixpanel.track(eventName, dict);
  _gaq.push(["_trackEvent", eventName, action, label]);
}

function _isMeta(e) {
	if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) return true;

	// Logic that handles checks for the middle mouse button, based
	// on [jQuery](https://github.com/jquery/jquery/blob/master/src/event.js#L466).
	var which = e.which, button = e.button;
	if (!which && button !== undefined) {
	  return (!button & 1) && (!button & 2) && (button & 4);
	} else if (which === 2) {
	  return true;
	}
	return false;
};

function trackLinks(css, eventName, extras) {
  $(css).bind('click', function(e) {
  	var el = this;
  	var dict = extras || {};
  	dict['url'] = el.href;

    trackEvent(eventName, null, null, dict);

  	if (el.href && el.target !== '_blank' && !_isMeta(e)) {
      e.preventDefault();

      // Navigate to the url after just enough of a timeout (300 ms).
      setTimeout(function() {
        window.location.href = el.href;
      }, 300);
    }
  });
}

function trackPageview(optPath) {
  optPath = optPath || document.location.path + document.location.query;
  mixpanel.track_pageview(optPath);
  _gaq.push(['_trackPageview', optPath]);
}

//]]>
<% } else { %>
<script type="text/javascript" nonce="<%= helpers.cspNonce() %>">
function trackEvent(){}
function trackLinks(){}
function trackPageview(){}
function logSearchPerformed(){}
<% } %>
</script>
