Google Analytics event tracking
Google Analytics event tracking

--- a/include/common-net.inc.php
+++ b/include/common-net.inc.php
@@ -7,7 +7,14 @@
 	curl_setopt($ch, CURLOPT_HEADER, 0);
 	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
 	$page = curl_exec($ch);
-	if (curl_errno($ch)) echo "<font color=red> Database temporarily unavailable: " . curl_errno($ch) . " " . curl_error($ch) . "</font><br>";
+	if (curl_errno($ch)) {
+		echo "<font color=red> Database temporarily unavailable: ";
+		echo curl_errno($ch) . " " . curl_error($ch);
+		if (isDebug()) {
+			echo $url;
+		}
+		echo "</font><br>";
+	}
 	curl_close($ch);
 	debug(print_r($page,true),"json");
 	return $page;

--- a/include/common-template.inc.php
+++ b/include/common-template.inc.php
@@ -129,23 +129,14 @@
 
 </script> ";
 	}
-	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></head>".'
+</script>";
+echo '</head>
 <body>
     <div id="skip">
     <a href="#maincontent">Skip to content</a>
@@ -176,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 (!isDebug()) {
+        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 . '" />';
     }
@@ -219,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
@@ -16,7 +16,7 @@
 $googleMapsAPIkey = "ABQIAAAA95XYXN0cki3Yj_Sb71CFvBTPaLd08ONybQDjcH_VdYtHHLgZvRTw2INzI_m17_IoOUqH3RNNmlTk1Q";
 $otpAPIurl = 'http://localhost:8080/opentripplanner-api-webapp/';
 if (isDebug("awsotp") || php_uname('n') == "maxious.xen.prgmr.com") {
-	'http://bus-main.lambdacomplex.org:8080/opentripplanner-api-webapp/';
+	$otpAPIurl = 'http://bus-main.lambdacomplex.org:8080/opentripplanner-api-webapp/';
 }
 if (isDebug("phperror")) error_reporting(E_ALL ^ E_NOTICE);
 include_once ("common-geo.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];
@@ -40,7 +41,7 @@
 	echo '<a href="trip.php?tripid=' . $othertrip[1] . "&routeid=" . $routeid . '">' . midnight_seconds_to_time($othertrip[0]) . '</a> ';
 }
 echo '</p><p><h2>Other directions/timing periods:</h2> ';
-$url = $APIurl . "/json/routesearch?routeshortname=" . $trips[1]->route_short_name;
+$url = $APIurl . "/json/routesearch?routeshortname=" . rawurlencode($trips[1]->route_short_name);
 $json = json_decode(getPage($url));
 foreach ($json as $row) {
 	if ($row[0] != $routeid) echo '<a href="trip.php?routeid=' . $row[0] . '">' . $row[2] . ' (' . ucwords($row[3]) . ')</a> ';

--- 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">
@@ -92,7 +93,17 @@
 		}
 		echo "" . staticmap($walkStepMarkers, 0, "icong", false) . "<br>\n";
 		foreach ($leg->steps->walkSteps as $stepNumber => $step) {
-			echo "Walking step " . ($stepNumber + 1) . " $step->absoluteDirection / $step->relativeDirection on $step->streetName for " . floor($step->distance) . " meters<br>\n";
+			echo "Walking step " . ($stepNumber + 1) . ": ";
+                        if ($step->relativeDirection == "CONTINUE") {
+                          echo "Continue, ";
+                        } else if ($step->relativeDirection) echo "Turn ".ucwords(strtolower(str_replace("_"," ",$step->relativeDirection))).", ";
+                        echo "Go ".ucwords(strtolower($step->absoluteDirection))." on ";
+                        if (strpos($step->streetName,"from") !== false && strpos($step->streetName,"way") !== false) {
+                          echo "footpath";
+                        } else {
+                          echo $step->streetName;
+                        }
+                        echo " for " . floor($step->distance) . " meters<br>\n";
 		}
 	}
 }
@@ -101,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 {
@@ -116,9 +133,12 @@
 		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
 		$page = curl_exec($ch);
 		if (curl_errno($ch)) {
-			tripPlanForm("Trip planner temporarily unavailable: " . curl_errno($ch) . " " . curl_error($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>";