
function trackPageview(path, query) {
  path = slugify(path);
  path = path + genQueryString(query);

  if (_gaq) {
    try {
      _gaq.push(['_trackPageview', path]);
    } catch(err) {}
  }
}

function slugify(path) {
  path = trim(path);
  if (!path) return '';

  path = path.toLowerCase();
  path = path.replace(/[\s_]/g, '-');
  path = path.replace(/[^\w-\/]/g, '');
  path = path.replace(/-+/g, '-');
  path = path.replace(/-?\/-?/g, '/');

  return path;
}

function trim(str) {
  if (!str) return '';
  str = str.replace(/^\s+/, '');
  str = str.replace(/\s+$/, '');
  return str;
}

function genQueryString(query) {
  if (!query) return '';
  if (typeof query !== 'object') return '';

  var result = [];
  for (field in query) {
    if (typeof query[field] !== 'function') {
      result.push(trim(field) + '=' + trim(query[field]));
    }
  }

  return '?' + result.join('&');
}

