Forum Discussion

kghahremani's avatar
kghahremani
Icon for Nimbostratus rankNimbostratus
Aug 19, 2019

out of bound error in piwik iRule

I've used iRule for piwik like sample below :

https://devcentral.f5.com/s/articles/google-analytics-script-injection-911?t=1566187088612

 

I've created iRule and iList but whet check ltm log I've received error and piwik doesn't received any data .

Error :

out of bounds( line 5) invoked from within ".... "

 

iRule content :

 

when RULE_INIT {

set static::siteid "XXXXX"

set static::piwik_url "https://www.piwik.url/piwik/piwik"

}

 

when HTTP_REQUEST {

HTTP::header remove "Accept-Encoding"

}

 

when HTTP_RESPONSE {

if { [HTTP::header Content-Type] contains "text/html" } {

if { [HTTP::header exists "Content-Length"] } {

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

} else {

set content_length 1000000

}

if { $content_length > 0 } {

HTTP::collect $content_length

}

}

}

when HTTP_RESPONSE_DATA {

   set search "</head>"

  HTTP::payload replace 0 $content_length [string map [list $search "[subst -nocommands -nobackslashes [ifile get matomo.js]]</head>"] [HTTP::payload]]

log local0. HTTP::payload replace 0 $content_length [string map [list $search "[subst -nocommands -nobackslashes [ifile get matomo.js]]</head>"] [HTTP::payload]]

   HTTP::release

}

 

  • Hi, I'm just curious.

    Is the log output showing that you have inserted script on header as twice or it's all ok? Did you write that line code covered by double quotes or curly braces?

    Regards.

  • I have never used Piwik or Matomo. I think you use Matomo. I updated piwik codes for Matomo. But I 'm not sure if it will work.

     

    google.js (Import to iFile):

    <!-- Google Analytics -->
    <script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', '$static::tracking_id', 'auto');
    ga('send', 'pageview');
    </script>
    <script async src='https://www.google-analytics.com/analytics.js'></script>
    <!-- End Google Analytics -->

    matomo.js (Import to iFile):

    <!-- Matomo -->
    <script type="text/javascript">
    var _paq = _paq || [];
    _paq.push(['trackPageView']);
    _paq.push(['enableLinkTracking']);
    (function() {
    	var u="$static::matomo_url";
    	_paq.push(['setTrackerUrl', u+'matomo.php']);
    	_paq.push(['setSiteId', {$static::siteid}]);
    	var d=document,
    		g=d.createElement('script'),
    		s=d.getElementsByTagName('script')[0];
    	g.;
    	g.type='text/javascript';
    	g.async=true;
    	g.defer=true;
    	g.src=u+'matomo.js';
    	s.parentNode.insertBefore(g,s);
    })();
    </script>
    <!-- End Matomo Code -->

    iRule:

    when RULE_INIT {
    	set static::tracking_id "Your Google Analytics ID UA-XXXXX-Y"
    	set static::siteid "XXXXX"
    	set static::matomo_url "https://matomo.url/ (with slash)"
    }
     
    when HTTP_REQUEST {
    	HTTP::header remove "Accept-Encoding"
    }
     
    when HTTP_RESPONSE {
    	if { [HTTP::header Content-Type] contains "text/html" } {
    		if { [HTTP::header exists "Content-Length"] } {
    			set content_length [HTTP::header "Content-Length"]
    		} else {
    			set content_length 1000000
    		}
    		
    		if { $content_length > 0 } {
    			HTTP::collect $content_length
    		}
    	}
    }
     
    when HTTP_RESPONSE_DATA { 
        set search "</head>"
        HTTP::payload replace 0 $content_length [string map [list $search "[subst -nocommands -nobackslashes [ifile get google.js]]</head>"] [HTTP::payload]]
        HTTP::release
    }