Forum Discussion
HELITG_35878
Nimbostratus
Aug 18, 2008Google 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
- Nicolas_Menant
Employee
Hi, - HELITG_35878
Nimbostratus
Thanks for the reply. We have an LTM 1500 - Nicolas_Menant
Employee
Hi, - HELITG_35878
Nimbostratus
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. - Nicolas_Menant
Employee
Can you remove all the space in your [] for code ? i did it simply so that you could see which mark to use - HELITG_35878
Nimbostratus
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 } } }}
- Nicolas_Menant
Employee
Hi,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 } }
- HELITG_35878
Nimbostratus
Thanks for your very prompt response. I'll give that a go.
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects