Showing results for 
Search instead for 
Did you mean: 

Tracking user activity with Google Analytics

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.




To enable tracking for your web sites, you need to embed a small fragment of JavaScript code in every web page. This extension makes this easy, by inspecting all outgoing content and inserting the code into each HTML page, while honoring the users 'Do Not Track' preferences.


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 = [
   "" => "UA-123456-2",
   ""  => "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 
$script = '
<script type="text/javascript">
   var _gaq = _gaq || [];  
   _gaq.push(["_setAccount", "' . $uacct . '"]); 

   (function() {    
      var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;    
      ga.src=("https:" == document.location.protocol ? "https://ssl" : "http://www") + ""; 
      var s = document.getElementsByTagName("script")[0]; 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 ));


For some extensions to this rule, check out Faisal Memon's article Google Analytics revisited

Version history
Revision #:
2 of 2
Last update:
‎12-21-2020 04:42:PM
Updated by:
Labels (1)

cool feature.

unfortunately, i get "trafficscript!memory_warnings" (because we have huge html-files to serve), default is 1 MB.

is it save to just raise this limit? or any other suggestion?