Google Analytics Injection iRules for Multiple Virtual Servers

Morning,

I have written iRules for 2 separate virtual servers that inject the GA code snippet with a different google id. When I hit the first virtual server (A) the code is injected properly. Then, without closing my browser, I connect to the next virtual server (B), the code from the irule assigned to A is getting injected into the payload for site B.

I am not testing for the existence of the GA code in the payload; the analysis people want this instantiated even if the GA script is already hardcoded on the page.

1. How can I make sure that the unique irule runs for each virtual server?

2. How can I make this more efficient, since I want it to go into every page? Would it be ok to move the setting of the variable for code injection to HTTP_RESPONSE or HTTP_RESPONSE_DATA?

Thanks!!!

-katherine

Here is a sample of the code I am currently using.

when RULE_INIT { 
 set txt_texttoinsert " 
  
  
 " 
 } 
 when HTTP_REQUEST { 
  Don't allow data to be chunked 
 if { [HTTP::version] eq "1.1" } { 
 if { [HTTP::header is_keepalive] } { 
 HTTP::header replace "Connection" "Keep-Alive" 
 } 
 HTTP::version "1.0" 
 } 
 } 
 when HTTP_RESPONSE { 
 if { [HTTP::header Content-Type] starts_with "text/html" } { 
 if { [HTTP::header exists "Content-Length"] } { 
 set content_length [HTTP::header "Content-Length"] 
 } else { 
 set content_length 1000000 
 } 
 if { $content_length > 0 } { 
 HTTP::collect $content_length 
 } 
 } 
 } 
 when HTTP_RESPONSE_DATA { 
 set idx [string last "" [HTTP::payload]] 
 if { -1 == $idx } { 
 set idx [string last "" [HTTP::payload]] 
 } 
 if { -1 == $idx } { 
 set offset [HTTP::payload length] 
 } else { 
 set offset $idx 
 } 
 HTTP::payload replace $offset 0 $::txt_texttoinsert 
 }

That’s correct. You could either move the variable declaration to another event other than RULE_INIT or use two different names.

In theory I think there can only be one body tag in well formed HTML, so you could use a stream profile and STREAM::expression based iRule to replace with your HTML and . A stream filter would be more efficient than collecting the payload and rewriting it. You can check the STREAM::expression wiki page for some examples:

STREAM::expression

http://devcentral.f5.com/wiki/default.aspx/iRules/stream\_\_expression

Aaron

can anybody share the steps/guidelines to configure the same…?

Many Thanks!

Mayur