cancel
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.

 

googleanalytics.png

 

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

 

Requirements

 

This extension has been tested against Stingray Traffic Manager 9.1, and should function with all versions from 7.0 onwards.

 

Installation

  

Copy the contents of the User Analytics rule below. Open in an editor, and paste the contents into a new response rule:

 

348i9FB3E5198DB33A0F.jpg

 

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:

 

350i5D82BAC55643694B.jpg

 

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 = '

<script type="text/javascript">

   var _gaq = _gaq || [];  

   _gaq.push(["_setAccount", "' . $uacct . '"]); 

   _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>';


$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 #:
1 of 1
Last update:
‎03-12-2013 05:54:AM
Updated by:
 
Labels (1)
Comments
jochenmaurer

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?