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:
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 );
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 \
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
Thank you for the quick response and fix!!
You're welcome. Hope this works our for you.
Faisal