F5 Sites
  • F5.com
  • LearnF5
  • NGINX
  • MyF5
  • Partner Central
Contact
  • Under Attack?
  • F5 Support
  • DevCentral Support
  • F5 Sales
  • NGINX Sales
  • F5 Professional Services
Skip to contentBrand Logo
Forums
CrowdSRC
Articles
Groups
EventsSuggestionsHow Do I...?
RegisterSign In
  1. DevCentral
  2. CrowdSRC
  3. CodeShare

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.0
analytics
application delivery
BIG-IP
cloud
google
google analytics
iRules
piwik
Yann_Desmarest's avatar
Yann_Desmarest
Icon for Cirrus rankCirrus
Joined September 11, 2012
View Profile
Danny_Tix's avatar
Danny_Tix
Icon for Nimbostratus rankNimbostratus
Apr 24, 2019

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
    }
}

ABOUT DEVCENTRAL

DevCentral NewsTechnical ForumTechnical ArticlesTechnical CrowdSRCCommunity GuidelinesDevCentral EULAGet a Developer Lab LicenseBecome a DevCentral MVP

RESOURCES

Product DocumentationWhite PapersGlossaryCustomer StoriesWebinarsFree Online CoursesF5 CertificationLearnF5 Training

SUPPORT

Manage SubscriptionsProfessional ServicesProfessional ServicesCreate a Service RequestSoftware DownloadsSupport Portal

PARTNERS

Find a Reseller PartnerTechnology AlliancesBecome an F5 PartnerLogin to Partner Central

F5 logo©2024 F5, Inc. All rights reserved.
TrademarksPoliciesPrivacyCalifornia PrivacyDo Not Sell My Personal Information