09-23-2011
12:10 PM
- last edited on
11-15-2011
07:21 AM
by
mgyles
(Originally posted March 23, 2011)
About this extension
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.
This extension has been tested against Zeus Traffic Manager 7.1. It will function with version 7.0, and should function with ealier releases if the use of the $overrideProfile hash is removed.
Download the extension and extract the User Analytics.zts file. 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:
For reference, the source of the User Analytics.zts rule is replicated below:
# 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, or just
# comment out the lines between '[' and '];'
$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"))br eak;
# Honor any Do-Not-Track preference
$dnt =http.getHeader("DNT");
if($dnt =="1")break;
# Determine the correct $uacct profile ID
$host =http.getHeader("Host");
$uacct =$overrideProfile[$host];
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();
# Insert the code just before 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 ));
Download this extension below:
© Copyright 2012 Riverbed Technology. All rights reserved Riverbed.com | Contact Us | Technical Support | Terms & Conditions | Privacy Policy