Google Analytics script injection
Problem this snippet solves: Add google analytics script in the html content of the HTTP response. Works also for other Analytics providers like Piwik. How to use this snippet: Installation
File...
Updated Jun 06, 2023
Version 2.0Yann_Desmarest
Cirrus
Joined September 11, 2012
Danny_Tix
Apr 24, 2019Nimbostratus
We arrived at a solution using HTML profiles - am I overlooking any major downsides to this approach?
HTML profile (abbreviated):
ltm profile html /Common/html-ga {
app-service none
content-detection disabled
content-selection { application/xhtml+xml text/xhtml text/html }
defaults-from /Common/html
description none
rules {
/Common/GT-Private
/Common/GA-Private
}
}
ltm html-rule tag-append-html /Common/GT-Private {
action {
text ""
}
description "Google Tag Manager"
match {
tag-name body
}
}
ltm html-rule tag-prepend-html /Common/GA-Private {
action {
text ""
}
description "Google Analytics"
match {
tag-name /head
}
}
Sample iRule (optional and abbreviated):
when HTTP_REQUEST {
set disable_html 0
switch -glob [string tolower [HTTP::uri]] {
/secure_app* {
special case for app that leaks data in URLs
pool secure_app
set disable_html 1
} /legacy_app/pagenotfound/* {
... special case for app that redirects 404s ...
pool legacy_app
set disable_html 1
} default {
... normal logic ...
pool main_app
}
}
}
when HTTP_RESPONSE {
if { [HTTP::status] == 404 || [HTTP::status] == 403 || $disable_html == 1 }{
HTML::disable
}
}