  
<div id="snippet_292" class="snippet">
  <h2><a href="/snippets/292-Tracking-404-and-500-with-Google-Analytics" title="Tracking 404 and 500 with Google Analytics - JavaScript - 404, 500, google, analytics, track">Tracking 404 and 500 with Google Analytics</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/61-JavaScript">
      JavaScript</a> posted about 1 year ago by christian
          
  </div>

  <div class="body">
    <p>Tracking 404 and 500 errors with Google Analytics is <a href="http://www.google.com/support/analytics/bin/answer.py?hl=en&amp;answer=86927">documented here</a>, but I tend to forget so I&#8217;m putting the information here:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> <span class="LineComment"><span class="LineComment">//</span> 404</span>
<span class="line-numbers">   2 </span> pageTracker._trackPageview(<span class="String"><span class="String">&quot;</span>/404.html?page=<span class="String">&quot;</span></span> <span class="Operator">+</span> <span class="LibraryClassType">document</span>.<span class="NamedConstant">location</span>.<span class="NamedConstant">pathname</span> <span class="Operator">+</span> <span class="LibraryClassType">document</span>.<span class="NamedConstant">location</span>.search <span class="Operator">+</span> <span class="String"><span class="String">&quot;</span>&amp;from=<span class="String">&quot;</span></span> <span class="Operator">+</span> <span class="LibraryClassType">document</span>.<span class="NamedConstant">referrer</span>);
<span class="line-numbers">   3 </span> 
<span class="line-numbers">   4 </span> <span class="LineComment"><span class="LineComment">//</span> 500</span>
<span class="line-numbers">   5 </span> pageTracker._trackPageview(<span class="String"><span class="String">&quot;</span>/500.html?page=<span class="String">&quot;</span></span> <span class="Operator">+</span> <span class="LibraryClassType">document</span>.<span class="NamedConstant">location</span>.<span class="NamedConstant">pathname</span> <span class="Operator">+</span> <span class="LibraryClassType">document</span>.<span class="NamedConstant">location</span>.search <span class="Operator">+</span> <span class="String"><span class="String">&quot;</span>&amp;from=<span class="String">&quot;</span></span> <span class="Operator">+</span> <span class="LibraryClassType">document</span>.<span class="NamedConstant">referrer</span>);
</pre></p>
<p>In Rails I set the response code and use that instead of hardcoding it in the view:<br />
<pre class="active4d"><span class="line-numbers">   1 </span> <span class="Operator">&lt;</span><span class="Operator">%</span> <span class="Keyword">if</span> response.<span class="NamedConstant">status</span> <span class="Operator">!</span><span class="Operator">=</span> <span class="Number">404</span> <span class="Operator">%</span><span class="Operator">&gt;</span>
<span class="line-numbers">   2 </span> pageTracker._trackPageview();
<span class="line-numbers">   3 </span> <span class="Operator">&lt;</span><span class="Operator">%</span> <span class="Keyword">else</span> <span class="Operator">%</span><span class="Operator">&gt;</span>
<span class="line-numbers">   4 </span> pageTracker._trackPageview(<span class="String"><span class="String">&quot;</span>/404.html?page=<span class="String">&quot;</span></span> <span class="Operator">+</span> <span class="LibraryClassType">document</span>.<span class="NamedConstant">location</span>.<span class="NamedConstant">pathname</span> <span class="Operator">+</span> <span class="LibraryClassType">document</span>.<span class="NamedConstant">location</span>.search <span class="Operator">+</span> <span class="String"><span class="String">&quot;</span>&amp;from=<span class="String">&quot;</span></span> <span class="Operator">+</span> <span class="LibraryClassType">document</span>.<span class="NamedConstant">referrer</span>);
<span class="line-numbers">   5 </span> <span class="Operator">&lt;</span><span class="Operator">%</span> end <span class="Operator">%</span><span class="Operator">&gt;</span>
</pre></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/644-404">404</a>, <a href="/tags/434-500">500</a>, <a href="/tags/115-google">google</a>, <a href="/tags/502-analytics">analytics</a>, <a href="/tags/506-track">track</a>
    
    
  </div>
</div>



  
<div id="snippet_173" class="snippet">
  <h2><a href="/snippets/173-PHP-exception-and-error-handling-with-register-shutdown-function-and-set-exception-handler" title="PHP exception and error handling with register_shutdown_function and set_exception_handler - PHP - php, exception, 500, blank, page">PHP exception and error handling with register_shutdown_function and set_exception_handler</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/102-PHP">
      PHP</a> posted about 1 year ago by christian
          
  </div>

  <div class="body">
    <p> PHP  has the crappiest error handling I&#8217;ve ever seen. The default behavior is to show all error messages to the user. If you disable this then you get a blank screen instead. The trick is to register an error handler with register_shutdown_function, which is then called if for example memory is exhausted. You could perhaps also use an ErrorDocument 500, but that didn&#8217;t work for me.</p>


	<p>Here&#8217;s the code I&#8217;ve used to handle both application exceptions and errors, such as memory limit exceeded. The trick is to have a global variable that indicates whether the script was run successfully or not. Add your code here: &#8220;Insert your buggy code here&#8221;.</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> $no_errors_detected = false;
<span class="line-numbers">   2 </span> 
<span class="line-numbers">   3 </span> class DispatchErrors 
<span class="line-numbers">   4 </span> {
<span class="line-numbers">   5 </span> 	static function handleException($exception) 
<span class="line-numbers">   6 </span> 	{
<span class="line-numbers">   7 </span> 	  Logging::error($message);
<span class="line-numbers">   8 </span> 
<span class="line-numbers">   9 </span> 		header( 'Location: /error500.html' );
<span class="line-numbers">  10 </span> 	}
<span class="line-numbers">  11 </span> 	
<span class="line-numbers">  12 </span> 	static function handleShutdown()
<span class="line-numbers">  13 </span> 	{
<span class="line-numbers">  14 </span> 		global $no_errors_detected;
<span class="line-numbers">  15 </span> 		
<span class="line-numbers">  16 </span> 		if (!$no_errors_detected)
<span class="line-numbers">  17 </span> 		{
<span class="line-numbers">  18 </span> 			header( 'Location: /error500.html' );
<span class="line-numbers">  19 </span> 		}		
<span class="line-numbers">  20 </span> 	}
<span class="line-numbers">  21 </span> }
<span class="line-numbers">  22 </span> 
<span class="line-numbers">  23 </span> register_shutdown_function(array('DispatchErrors', 'handleShutdown'));
<span class="line-numbers">  24 </span> set_exception_handler(array('DispatchErrors', 'handleException'));
<span class="line-numbers">  25 </span> 
<span class="line-numbers">  26 </span> # handleShutdown will be called
<span class="line-numbers">  27 </span> #range(0, 10000000000000000000);
<span class="line-numbers">  28 </span> 
<span class="line-numbers">  29 </span> # handleException will be called
<span class="line-numbers">  30 </span> #throw new Exception(&quot;abcd&quot;);
<span class="line-numbers">  31 </span> 
<span class="line-numbers">  32 </span> # Insert your buggy code here
<span class="line-numbers">  33 </span> 
<span class="line-numbers">  34 </span> $no_errors_detected = true;
</pre></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/279-php">php</a>, <a href="/tags/433-exception">exception</a>, <a href="/tags/434-500">500</a>, <a href="/tags/435-blank">blank</a>, <a href="/tags/436-page">page</a>
    
    
  </div>
</div>






