Forum Discussion

HELITG_35878's avatar
HELITG_35878
Icon for Nimbostratus rankNimbostratus
Aug 18, 2008

Google Analytics

I've found an article has a iRule to handle Google Analytics

 

 

http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=174

 

 

I wanted to modify the iRule for my environment.

 

 

We have multiple websites that point to single IP address. We only want to analyse one domain.

 

 

I've tried to wrap the code in the article with

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] eq "www.domain.com"} {

 

 

insert code from the article>

 

}}

 

 

The gives me an error indicating the commands when RULE_INIT, when HTTP_REQUEST, and when HTTP_RESPONSE is not valid in the current scope.

 

 

 

I was wondering if anyone can help

 

8 Replies

  • Hi,

     

     

    Your product is a licensed LTM or something else like GTM/LC? if this is not a LTM you won't have those functionalities

     

     

    To be able to use HTTP_REQUEST/HTTP_RESPONSE, you'll need to assign a HTTP profile to your vs but RULE_INIT should be accepted. The only reason for me if it doesn't work is that you have a LC.

     

     

    If you have a LTM, can you show us the exact output you have when you try to save the iRule ?
  • Thanks for the reply. We have an LTM 1500

     

     

    Here is the error when I try to save the iRule.

     

     

    01070151:3: Rule [ir_Google_Analytics] error: line 4: [command is not valid in the current scope] [when RULE_INIT { replace the value of the _uacct variable with your Google Analytics account. set analytics !-- Begin Analytics --script src=http://www.google-analytics.com/urchin.js type=text/javascript/script script type=text/javascript uacct = UA-edited-1 urchinTracker /script !-- End Analytics -- set exist_search urchinTracker }] line 16: [command is not valid in the current scope] [when HTTP_REQUEST { Dont 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 } }] line 25: [command is not valid in the current scope] [when HTTP_RESPONSE { if { [HTTP::header Content-Type] starts_with text/html } { if { [HTTP::header exists Content-Length] } { log local0. content length: [HTTP::header {Content-Length}] set content_length [HTTP::header Content-Length] } else { set content_length 1000000000 } log local0. Collecting $content_length bytes if { $content_length 0 } { HTTP::collect $content_length } } }] line 39: [command is not valid in the current scope] [when HTTP_RESPONSE_DATA { log local0. Content Type: [HTTP::header Content-Type] if { ! [HTTP::payload] contains $::exist_search } { log local0. Payload didnt contain $::exist_search! set idx [string last /body [HTTP::payload]] if { -1 == $idx } { set idx [string last /BODY [HTTP::payload]] } log local0. html end tag found at $idx if { -1 == $idx } { set offset [HTTP::payload length] } else { set offset $idx } HTTP::payload replace $offset 0 $::analytics } }
  • Hi,

     

     

    We'll need your iRule to understand your error, can you try posting your code here within [ code ] [ / code ] ?

     

     

    This way it should print correctly

     

     

    Thanks

     

     

    N.
  • Here's the code. The only parts I've added were the first two lines, and the last two close braces. So it's basically the Google Analytics script from here on Devcentral.

     

     

     

    [ code ]

     

     

    when HTTP_REQUEST {

     

    if { [HTTP::host] eq "www.domain.com" } {

     

     

    when RULE_INIT {

     

    replace the value of the _uacct variable with your Google Analytics account.

     

    set analytics "

     

     

     

    "

     

     

    set exist_search "urchinTracker()"

     

    }

     

    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"] } {

     

    log local0. "content length: [HTTP::header {Content-Length}]"

     

    set content_length [HTTP::header "Content-Length"]

     

    } else {

     

    set content_length 1000000000

     

    }

     

    log local0. "Collecting $content_length bytes"

     

    if { $content_length > 0 } {

     

    HTTP::collect $content_length

     

    }

     

    }

     

    }

     

    when HTTP_RESPONSE_DATA {

     

    log local0. "Content Type: [HTTP::header Content-Type]"

     

    if { ! ([HTTP::payload] contains $::exist_search) } {

     

    log local0. "Payload didn't contain $::exist_search!"

     

    set idx [string last "" [HTTP::payload]]

     

    if { -1 == $idx } {

     

    set idx [string last "" [HTTP::payload]]

     

    }

     

    log local0. "html end tag found at $idx"

     

    if { -1 == $idx } {

     

    set offset [HTTP::payload length]

     

    } else {

     

    set offset $idx

     

    }

     

    HTTP::payload replace $offset 0 $::analytics

     

    }

     

    }

     

     

    }}

     

     

     

     

    [ / code ]
  • Can you remove all the space in your [] for code ? i did it simply so that you could see which mark to use
  • I'm not sure what you mean by removing the space. Is this OK now?

    when HTTP_REQUEST { 
     if { [HTTP::host] eq "www.domain.com"} { 
     when RULE_INIT {   
      replace the value of the _uacct variable with your Google Analytics account.   
     set analytics " 
      
      
     " 
     set exist_search "urchinTracker()" 
     } 
     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"] } {       
     log local0. "content length: [HTTP::header {Content-Length}]"       
     set content_length [HTTP::header "Content-Length"]     
     } else {       
     set content_length 1000000000     
     }     
     log local0. "Collecting $content_length bytes"     
     if { $content_length > 0 } {        
     HTTP::collect $content_length     
     }   
     } 
     } 
     when HTTP_RESPONSE_DATA {   
     log local0. "Content Type: [HTTP::header Content-Type]"   
     if { ! ([HTTP::payload] contains $::exist_search) } {     
     log local0. "Payload didn't contain $::exist_search!"     
     set idx [string last "" [HTTP::payload]]     
     if { -1 == $idx } {       
     set idx [string last "" [HTTP::payload]] 
     }     
     log local0. "html end tag found at $idx"     
     if { -1 == $idx } {       
     set offset [HTTP::payload length]     
     } else {      
     set offset $idx    
     }     
     HTTP::payload replace $offset 0 $::analytics   
     } 
     } 
     }}

  • Hi,

     

     

    Ok you did it wrong, you should try something like this instead

     

     

     
     when RULE_INIT { 
        replace the value of the _uacct variable with your Google Analytics account. 
       set analytics " 
      
      
      
     " 
       set exist_search "urchinTracker()" 
     } 
     when HTTP_REQUEST { 
       set activate_analytics 0 
       if {[HTTP::host] equals "www.domain.com"} { 
             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" 
            } 
            set activate_analytics 1 
       } 
     } 
     when HTTP_RESPONSE { 
       if {$activate_analytics equals "1"} { 
            if { [HTTP::header Content-Type] starts_with "text/html" } { 
               if { [HTTP::header exists "Content-Length"] } { 
                  log local0. "content length: [HTTP::header {Content-Length}]" 
                  set content_length [HTTP::header "Content-Length"] 
               } else { 
               set content_length 1000000000 
             } 
             log local0. "Collecting $content_length bytes" 
             if { $content_length > 0 } { 
                HTTP::collect $content_length 
             } 
           } 
        } 
     } 
     when HTTP_RESPONSE_DATA { 
       log local0. "Content Type: [HTTP::header Content-Type]" 
       if { ! ([HTTP::payload] contains $::exist_search) } { 
         log local0. "Payload didn't contain $::exist_search!" 
         set idx [string last "" [HTTP::payload]] 
         if { -1 == $idx } { 
           set idx [string last "" [HTTP::payload]] 
         } 
         log local0. "html end tag found at $idx" 
         if { -1 == $idx } { 
           set offset [HTTP::payload length] 
         } else { 
           set offset $idx 
         } 
         HTTP::payload replace $offset 0 $::analytics 
       } 
     } 
     

     

     

    Here with the variable activate_analytics we will trigger the code only if the domain matched