cancel
Showing results for 
Search instead for 
Did you mean: 

Google Analytics revisited

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 yourdomain.com,


# yourdomain.net, etc. then set $mutliple_domains to TRUE and


# replace yourdomain.com with your Top Level Domain as specified


# in your Google Analytics account


#


$multiple_domains = FALSE;


$tld = 'yourdomain.com';



#


# 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') + '.google-analytics.com/ga.js'; \n \


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


  })(); \n \


</script>\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 );





Version history
Revision #:
1 of 1
Last update:
‎08-17-2012 10:17:AM
Updated by:
 
Labels (1)
Comments
michael.g.zuckerman

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 \

fmemon

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.

Faisal

michael.g.zuckerman

Thank you for the quick response and fix!!

fmemon

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

Faisal