PAGE_TRACKER = null;

// Function to track the page view of a user and send it to Google Analytics.
function trackPageview(url) {
  if (typeof(url) == "undefined") {
    url = location.pathname;
  }

  params = {
    'url': url
  };
  $.postJSON('/users/track_pageview', params);
  
  loadPageTracker();
  try {
    PAGE_TRACKER._trackPageview(url); 
  } catch(err) {}
}

// Function to track an event taken by a user and POST it to the datastore
// as well as send it to Google Analytics.
function trackEvent(category, action, label, value) {
  params = {
    'category': category,
    'action': action,
    'label': label,
    'value': value
  };
  $.postJSON('/users/track_event', params);

  loadPageTracker();
  try {
    PAGE_TRACKER._trackEvent(category, action, label, value);
  } catch(err) {}
}

function loadPageTracker() {
  if (PAGE_TRACKER == null) {
    try {
      PAGE_TRACKER = _gat._getTracker("UA-8427258-1");      
    } catch(err) {}
  }
}
/*
// Function to track the user based on whether they are new or returning.
function trackUser() {
  if (USER.username == null && USER.points == 0) {
    trackEvent("Account", "New User", "Anonymous", USER.points);
  }
  else {
    if (USER.username == null) {
      trackEvent("Account", "Returning User", "Anonymous", USER.points);      
    }
    else {
      trackEvent("Account", "Returning User", USER.username, USER.points); 
    }
  }
}*/


