Forum Discussion

warbie_136205's avatar
warbie_136205
Icon for Nimbostratus rankNimbostratus
Mar 23, 2016

Google Analytics different code for Multiple Sites

Hi All,

 

I am trying to figure out how to make different Google Analytic codes work for multiple sites. I found this on the good old interewebz/DevCentral listed below. However, when I create a second iRule and change out the gacode for another code I still see one GA code dominate both sites. I see now that Rule_INIT is a global call and I'm just not sure how to switch it up to make this work. Could any of you awesome people help me here? I've tried messing with streams to change it out and also thinking maybe my answer might be in the client_accept call, but I'm just kind of green here. Thanks in advance.

 

Code
    when RULE_INIT {
              set ::find ""
              set gacode "UA-Code-x"
              set ::replace "  "
     }
  when HTTP_RESPONSE {
  STREAM::disable
  if {[HTTP::header value Content-Type] contains "text"}{
  STREAM::expression "@$::find@$::replace@" 
  STREAM::enable
    }
  }
  • Hi Warbie,

    don't use the iRule you've posted. Its using a very outdated syntax that would cause you system to disable CMP processing (aka. massive performance impact)

    I've used the iRule below to inject GA-Code into HTMLs. The HTTP_REQUEST event can be customized to use a different $Insert_GA_Code for independent website or even subsites.

     

    when HTTP_REQUEST {
        STREAM::disable
        if { [string tolower [HTTP::path]] ends_with ".aspx" } then {
            set Insert_GA_Code "XYZ-YOUR_GA_ID-XYZ"
    
             Note: Instead of removing the "Accept-Encoding" header, you could also use a compression profile on your virtual server
                   and make sure the "Keep Accept Encoding" is disabled. This would allow you to still compress the content before 
                   sending it to the client.
            HTTP::header remove "Accept-Encoding"
        }
    }
    when HTTP_RESPONSE {
        if { [info exists Insert_GA_Code] } then {
             if { $debug } { log -noname local0. "$log_prefix Need to inject  GA Tracking Code for the requested ressource." }
            if { [HTTP::header value Content-Type] eq "text/html" {
                 if { $debug } { log -noname local0. "$log_prefix The \"Content-Type\" Headers containing \"text/html\". Preparing the XYZ specific GA Tracking Code for HTTP Response Injection." }
                 if { $debug } { log -noname local0. "$log_prefix Generating the XYZ specific GA Tracking Code." }
                set ga_code_snipped "
    
    "
                 if { $debug } { log -noname local0. "$log_prefix Setting up the SEARCH/REPLACE expression for GA-Code injection." }
                STREAM::expression "@@$ga_code_snipped@"
                 if { $debug } { log -noname local0. "$log_prefix Reenabling Content Streaming Profile." }
                STREAM::enable
                unset -nocomplain Insert_GA_Code
                unset -nocomplain ga_code_snipped
            }
        }
    }
    

     

    Cheers, Kai

  • Hi Warbie,

     

    Very strange.. the provided iRule had somehow a "} then" missing. So try this updated iRule...

     

    when HTTP_REQUEST {
        STREAM::disable
        if { [string tolower [HTTP::path]] ends_with ".aspx" } then {
            set Insert_GA_Code "XYZ-YOUR_GA_ID-XYZ"
    
             Note: Instead of removing the "Accept-Encoding" header, you could also use a compression profile on your virtual server
                   and make sure the "Keep Accept Encoding" is disabled. This would allow you to still compress the content before 
                   sending it to the client.
            HTTP::header remove "Accept-Encoding"
        }
    }
    when HTTP_RESPONSE {
        if { [info exists Insert_GA_Code] } then {
             if { $debug } { log -noname local0. "$log_prefix Need to inject  GA Tracking Code for the requested ressource." }
            if { [HTTP::header value Content-Type] eq "text/html" } then {
                 if { $debug } { log -noname local0. "$log_prefix The \"Content-Type\" Headers containing \"text/html\". Preparing the XYZ specific GA Tracking Code for HTTP Response Injection." }
                 if { $debug } { log -noname local0. "$log_prefix Generating the XYZ specific GA Tracking Code." }
                set ga_code_snipped "
    
    "
                 if { $debug } { log -noname local0. "$log_prefix Setting up the SEARCH/REPLACE expression for GA-Code injection." }
                STREAM::expression "@@$ga_code_snipped@"
                 if { $debug } { log -noname local0. "$log_prefix Reenabling Content Streaming Profile." }
                STREAM::enable
                unset -nocomplain Insert_GA_Code
                unset -nocomplain ga_code_snipped
            }
        }
    }

    Cheers, Kai