Google Analytics event tracking
Google Analytics event tracking

--- a/include/common-template.inc.php
+++ b/include/common-template.inc.php
@@ -129,22 +129,12 @@
 
 </script> ";
 	}
-	if (!isDebugServer()) echo '
+	if (isAnalyticsOn()) echo '
 <script type="text/javascript">'."
 
   var _gaq = _gaq || [];
   _gaq.push(['_setAccount', 'UA-22173039-1']);
   _gaq.push(['_trackPageview']);
-
-  (function() {
-    var ga = document.createElement('script'); ga.type = 
-'text/javascript'; ga.async = true;
-    ga.src = ('https:' == document.location.protocol ? 
-'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
-    var s = document.getElementsByTagName('script')[0]; 
-s.parentNode.insertBefore(ga, s);
-  })();
-
 </script>";
 echo '</head>
 <body>
@@ -177,7 +167,15 @@
 	}
 	echo '<div id="footer"><a href="about.php">About/Contact Us</a>&nbsp;<a href="feedback.php">Feedback/Bug Report</a></a>';
 	echo '</div>';
-        if (!isDebugServer()) {
+        if (isAnalyticsOn()) {
+	echo "<script>  (function() {
+    var ga = document.createElement('script'); ga.type = 
+'text/javascript'; ga.async = true;
+    ga.src = ('https:' == document.location.protocol ? 
+'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+    var s = document.getElementsByTagName('script')[0]; 
+s.parentNode.insertBefore(ga, s);
+  })();";
          $googleAnalyticsImageUrl = googleAnalyticsGetImageUrl();
   echo '<img src="' . $googleAnalyticsImageUrl . '" />';
     }
@@ -220,5 +218,10 @@
                 </form>
             </div></div>';
 }
+function trackEvent($category, $action, $label = "", $value = -1) {
+  if (isAnalyticsOn()) {
+    echo "<script> _gaq.push(['_trackEvent', $category, $action".($label != "" ? ", $label" : "").($value != -1 ? ", $value" : "")."]);";
+  }
+}
 ?>
 

--- a/include/common.inc.php
+++ b/include/common.inc.php
@@ -59,11 +59,18 @@
 			}
 		}
 	}
+	if ($_SESSION['lat'] != "" && isMetricsOn()) {
+		trackEvent("Geolocation","Updated Location", "Geocoded - ".($geocoded ? "Yes" : "No"));
+	}
 }
 debug(print_r($_SESSION, true) , "session");
 function isDebugServer()
 {
 	return $_SERVER['SERVER_NAME'] == "10.0.1.154" || $_SERVER['SERVER_NAME'] == "localhost" || $_SERVER['SERVER_NAME'] == "127.0.0.1" || !$_SERVER['SERVER_NAME'];
+}
+function isAnalyticsOn()
+{
+	return !isDebugServer();
 }
 function isDebug($debugReason = "other")
 {

--- a/routeList.php
+++ b/routeList.php
@@ -36,6 +36,8 @@
 		$suburb = filter_var($_REQUEST['suburb'], FILTER_SANITIZE_STRING);
 		$url = $APIurl . "/json/stopzonesearch?q=" . $suburb;
 		include_header("Routes by Suburb", "routeList");
+		trackEvent("Route Lists","Routes By Suburb", $suburb);
+
 	}
 	if ($_REQUEST['nearby']) {
 		$url = $APIurl . "/json/neareststops?lat={$_SESSION['lat']}&lon={$_SESSION['lon']}&limit=15";

file:a/stop.php -> file:b/stop.php
--- a/stop.php
+++ b/stop.php
@@ -51,9 +51,12 @@
 echo '<div data-role="content" class="ui-content" role="main">        <a name="maincontent" id="maincontent"></a>';
 echo $stopLinks;
 if (sizeof($stops) > 0) {
+    trackEvent("View Stops","View Combined Stops", $stop[1], $stop[0]);
+
 	echo '<p>' . staticmap($stopPositions) . '</p>';
 }
 else {
+        trackEvent("View Stops","View Single Stop", $stop[1], $stop[0]);
 	echo '<p>' . staticmap(Array(
 		0 => Array(
 			$stop[2],

--- a/stopList.php
+++ b/stopList.php
@@ -62,6 +62,7 @@
 		$url = $APIurl . "/json/stopzonesearch?q=" . $suburb;
 		include_header("Stops in " . ucwords($suburb) , "stopList");
 		navbar();
+	       trackEvent("Stop Lists","Stops By Suburb", $suburb);
 	}
 	else {
 		$url = $APIurl . "/json/timingpoints";

file:a/trip.php -> file:b/trip.php
--- a/trip.php
+++ b/trip.php
@@ -24,6 +24,7 @@
 	$routetrips = json_decode(getPage($url));
 }
 include_header("Stops on " . $trips[1]->route_short_name . ' ' . $trips[1]->route_long_name, "trip");
+trackEvent("Route/Trip View","View Route", $trips[1]->route_short_name . ' ' . $trips[1]->route_long_name, $trips[1]->route_id);
 $url = $APIurl . "/json/tripstoptimes?trip=" . $tripid;
 $json = json_decode(getPage($url));
 $stops = $json[0];

--- a/tripPlanner.php
+++ b/tripPlanner.php
@@ -11,6 +11,7 @@
 }
 function tripPlanForm($errorMessage = "")
 {
+
 	global $date, $time, $from, $to;
 	echo "<font color=red>$errorMessage</font>";
 	echo '<form action="tripPlanner.php" method="post">
@@ -111,8 +112,14 @@
 	$fromPlace = (startsWith($from, "-") ? $from : geocode($from, false));
 	if ($toPlace == "" || $fromPlace == "") {
 		$errorMessage = "";
-		if ($toPlace === "") $errorMessage.= urlencode($to) . " not found.<br>\n";
-		if ($fromPlace === "") $errorMessage.= urlencode($from) . " not found.<br>\n";
+		if ($toPlace === "") {
+                  $errorMessage.= urlencode($to) . " not found.<br>\n";
+                  trackEvent("Trip Planner","Geocoder Failed", $to);
+                }
+		if ($fromPlace === "") {
+                  $errorMessage.= urlencode($from) . " not found.<br>\n";
+                  trackEvent("Trip Planner","Geocoder Failed", $from);
+                }
 		tripPlanForm($errorMessage);
 	}
 	else {
@@ -127,9 +134,11 @@
 		$page = curl_exec($ch);
 		if (curl_errno($ch)) {
 			tripPlanForm("Trip planner temporarily unavailable: " . curl_errno($ch) . " " . curl_error($ch) .(isDebug() ? $url : ""));
-		
+                        trackEvent("Trip Planner","Trip Planner Failed", $url);
                 }
 		else {
+                  	trackEvent("Trip Planner","Plan Trip From", $from);
+                        trackEvent("Trip Planner","Plan Trip To", $to);
 			$tripplan = json_decode($page);
 			debug(print_r($triplan, true));
 			echo "<h1> From: {$tripplan->plan->from->name} To: {$tripplan->plan->to->name} </h1>";