vADC Docs

Google Analytics revisited

by fmemon on ‎08-17-2012 10:17 AM (1,659 Views)

Following up on this earlier article try using the below TrafficScript code snippet to automatically insert the Google Analytics code on all your webpages.  To use it:

  1. Copy the rule onto your Stingray Traffic Manager  by first navigating Catalogs -> Rules
  2. Scroll down to Create new rule, give the rule a name, and select Use TrafficScript Language.  Click Create Rule to create the rule.
  3. Copy and paste the rule below.
  4. Change $account to your Google Analytics account number.
  5. If you are using multiple domains as described here set $multiple_domains to TRUE and set $tld to your Top Level Domain as specified in your Google Analytics account.
  6. Set the rule as a Response Rule in your Virtual Server by navigating to Services -> Virtual Servers -> <your virtual server> -> Rules -> Response Rules and Add rule.

After that you should be good to go.  No need to individually modify your web pages, TrafficScript will take care of it all.


# Replace UA-XXXXXXXX-X with your Google Analytics Account Number


$account = 'UA-XXXXXXXX-X';


# If you are tracking multiple domains, ie,

#, etc. then set $mutliple_domains to TRUE and

# replace with your Top Level Domain as specified

# in your Google Analytics account


$multiple_domains = FALSE;

$tld = '';


# Only modify text/html pages


if( !string.startsWith( http.getResponseHeader( "Content-Type" ), "text/html" )) break;


# This variable contains the code to be inserted in the web page. Do not modify.


$html = "\n<script type=\"text/javascript\"> \n \

  var _gaq = _gaq || []; \n \

  _gaq.push(['_setAccount', " . $account . "]); \n";

if( $multiple_domains == TRUE ) {

  $html .= " _gaq.push(['_setDomainName', " . $tld . "]); \n \

  _gaq.push(['_setAllowLinker', true]); \n";


$html .= " _gaq.push(['_trackPageview']); \n \

  (function() { \n \

  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; \n \

  ga.src=('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + ''; \n \

  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); \n \

  })(); \n \



# Insert the code right before the </head> tag in the page


$body = http.getResponseBody();

$body = string.replace( $body, "</head>", $html . "</head>");

http.setResponseBody( $body );

by michael.g.zuckerman
on ‎07-15-2013 11:17 AM

I am seeing a syntax error when I copy and paste the provided TrafficScript rule to a Stingray Traffic Manager 8.1r1 virtual appliance.  Any help to fix would be greatly appreciated.

Warning: line 23: Unknown escape sequence '\ '

  $html = "\n<script type=\"text/javascript\"> \n \ 


Error: line 23: Unterminated string constant

  $html = "\n<script type=\"text/javascript\"> \n \ 

Error: line 24: syntax error, unexpected function

  var _gaq = _gaq || []; \n \

by fmemon
on ‎07-15-2013 11:28 AM

Hi Mike,

Copy and pasting from that text box adds a few extra white spaces at the end of each line.  To fix, just delete the extra white space on the lines it complains about.


by michael.g.zuckerman
on ‎07-15-2013 11:59 AM

Thank you for the quick response and fix!!

by fmemon
on ‎07-15-2013 12:08 PM

You're welcome.  Hope this works our for you.