Google Analytics is a great tool for monitoring and tracking visitors to your web sites. Perhaps best of all, it's entirely web based - you only need a web browser to access the analysis services it provides.
Installing the Extension
This extension has been tested against Stingray Traffic Manager 9.1, and should function with all versions from 7.0 onwards.
Copy the contents of the User Analytics rule below. Open in an editor, and paste the contents into a new response rule:
Verify that the extension is functioning correctly by accessing a page through the traffic manager and use 'View Source' to verify that the Google Analytics code has been added near the top of the document, just before the closing </head>tag:
User Analytics rule
# Edit the following to set your profile ID;
$defaultProfile = "UA-123456-1";
# You may override the profile ID on a site-by-site basis here
$overrideProfile = [
"support.mysite.com" => "UA-123456-2",
"secure.mysite.com" => "UA-123456-3"
# End of configuration settings
# Only process text/html responses
$contentType = http.getResponseHeader( "Content-Type" );
if( !string.startsWith( $contenttype, "text/html" )) break;
# Honor any Do-Not-Track preference
$dnt = http.getHeader( "DNT" );
if ( $dnt == "1" ) break;
# Determine the correct $uacct profile ID
$uacct = $overrideProfile[ http.getHostHeader() ];
if( !$uacct ) $uacct = $defaultProfile;
# See http://www.google.com/support/googleanalytics/bin/answer.py?answer=174090
$script = '
var _gaq = _gaq || ;
_gaq.push(["_setAccount", "' . $uacct . '"]);
ga.src=("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
var s = document.getElementsByTagName("script"); s.parentNode.insertBefore(ga, s);
$body = http.getResponseBody();
# Find the location of the closing '</head>' tag
$i = string.find( $body, "</head>" ); if( $i ==-1 ) $i = string.findI( $body, "</head>" );
if( $i ==-1 ) break; # Give up
http.setResponseBody( string.left( $body, $i ) . $script . string.skip( $body, $i ));