I have just tried it out and indeed I can’t seem to change the template.
But I think that this isn’t the intended way to change the tracking code. If you look at the code that generates the tracking code, you can see that an event is sent to all plugins that allows them to modify the $codeImpl.
You have probably noticed that the template is
<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
{$options} _paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
{$setTrackerUrl}
{$optionsBeforeTrackerUrl}_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '{$idSite}']);
{% if loadAsync %}var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);{% endif %}
})();
</script>
{% if not loadAsync %}<script type='text/javascript' src="{$protocol}{$piwikUrl}/piwik.js"></script>
{% endif %}
{% if trackNoScript %}<noscript><p><img src="{$protocol}{$piwikUrl}/piwik.php?idsite={$idSite}&rec=1" style="border:0;" alt="" /></p></noscript>
{% endif %}
<!-- End Piwik Code -->
And optionsBeforeTrackerUrl and options are probably the places where you want to insert your code.
So I wrote this plugin, which modifies the tracking code by catching the event.
<?php
namespace Piwik\Plugins\TrackingCodePlugin;
class TrackingCodePlugin extends \Piwik\Plugin
{
public function registerEvents() {
return array(
'Piwik.getJavascriptCode' => 'customFunction'
);
}
public function customFunction(&$codeImpl, $parameters) {
$codeImpl["options"] .= "a=1 //I am an example for a option\n";
$codeImpl["optionsBeforeTrackerUrl"] .= "b=1 //I am an example for a optionsBeforeTrackerUrl\n";
}
}
This causes Piwik to generate the following tracking code.
<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
a=1 //I am an example for a option
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//localhost/piwik/";
b=1 //I am an example for a optionsBeforeTrackerUrl
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Piwik Code -->
I would like to mask paths to piwik.php and piwik.js files. I would use different paths and using mod rewrite I would redirect to real piwik.php and piwik.js paths.
I want user to use and see other paths in his UI for security reasons.
I can modify code directly but I would rather to achieve this via plugin.
Anyway, method described in piwik documentation seems not working at least for template javascriptCode.twig
So if I understand you correctly you want to replace the var u="//piwik.example/"; line with var u="//piwik.example/some_path/";, correct?
I am not sure why it doesn’t work, but I wouldn’t recommend it as it would break if some Piwik update changes the javascriptCode.twig and your file doesn’t have those changes.