Unable to modify default tracking code (v:3.2.0)

Hello,
I need to modify default tracking code and let user to view modified code in his account settings.

I read this article: Themes: Develop - Piwik Analytics - Developer Docs - v3

I have my custom API plugin. It wok with REST calls normally. But with this plugin I also want to add template with modified tracking code.

I created modified file:

plugins/[MyAPIplugin]/templates/javascriptCode.twig

Modification hasn’t been applied so I tried also:
plugins/[MyAPIplugin]/templates/plugins/Morpheus/javascriptCode.twig

and
plugins/[MyAPIplugin]/templates/plugins/Morpheus/templates/javascriptCode.twig

Modification still wasnt there.
I also set “theme” to “true” in plugin.js

When I modify orininal plugins/Morpheus/templates/javascriptCode.twig, modification is there.

Working with Piwik 3.2.0 on PHP 5.6.18

Maybe this is a bug, because Themes: Develop - Piwik Analytics - Developer Docs - v3 says it is possible to do this way as I tried.

Can somebody check if this is a real bug or I am doing womething wrong?

Thank you

Hi,

If I find some time and don’t forget, I’ll try to reproduce your issue in the next days.

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

Thank you Lukas, but actually I wanted to slightly change HTML parts of code.

Hi,

What do you mean by changing HTML parts?

Can you give an example?

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.

Correct, but there also some paths inside noscript tags in javascriptCode.twig

I’ll see if I am able to find a way, when I have some time.

Ok, thank you, but I am still wondering why I am unable to override this template.

Is there are some intended exception for this particular template or just bug?