1.11 upgrade disaster

Been using piwik for a few years, always upgraded when it tells me to, no problems… until this time.

Former code, up through 1.10 (I trimmed back the part that checks your protocol, because I’m strictly http):


<script type = "text/javascript">
  try
    {
    var piwikTracker = Piwik.getTracker("http://www.example.com/piwik/piwik.php", 1);
    piwikTracker.trackPageView();
    piwikTracker.enableLinkTracking();
    }
  catch( err ) {}
</script>

<noscript>
<p><img src = "/piwik/piwik.php?idsite=1" style = "border: 0" alt = ""></p>
</noscript>

Code I am now told to use (I left the protocol-checking part in place just to ensure I hadn’t misplaced a parenthesis):


<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(["trackPageView"]);
  _paq.push(["enableLinkTracking"]);

  (function() {
    var u=(("https:" == document.location.protocol) ? "https" : "http") + "://www.example.com/";
    _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.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
  })();
</script>

<noscript>
<img src="http://www.example.com/piwik/index.php?idsite=1&rec=1" style="border:0" alt="" />
</noscript>

(Yes, index.php rather than piwik.php. Um, isn’t index.php the view-report page?)

This leads to a flurry of “file does not exist” errors as it looks for www.example.com/piwik.js. Fixed that by adding piwik/ to path in both places… but it STILL can’t find the php file. Or rather, it doesn’t seem to understand that it’s supposed to look for it. Instead my logs show a stream of


GET /directory/filename.html?action_name={et cetera}

That is, it’s appending the piwik query string to the current filename-- whatever it is-- instead of to /piwik/piwik.php

The /piwik/ directory is where it always was. Its assorted files have up-to-the-minute timestamps; there’s no new stuff flopping around loose in the root.

Each file’s tracking code is where it always was, part of an included footer. There’s also a line in each head section

<script type = "text/javascript" src = "/piwik/piwik.js"></script>

This goes back several piwik releases so I’m not clear whether it’s still needed but it can’t be doing any harm.

What on earth is its problem? No new data has come in since I upgraded.

:frowning: :frowning: :frowning:

You can for now use the old tracking code - that still works.

Can you provide a link to your website so I can take a look? If you don’t want to post it here you can also PM me or send an email to fabian [at] piwik.org

OK - fixed this on master branch: Fix image tracker. Falsely showed link to index.php instead of piwik.php · matomo-org/matomo@db60c80 · GitHub and Fix wrong tracking tag when piwik is hosted in a subdirectory. · matomo-org/matomo@e6e1b80 · GitHub

You should use the following tracking code:


<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(["trackPageView"]);
  _paq.push(["enableLinkTracking"]);

  (function() {
    var u=(("https:" == document.location.protocol) ? "https" : "http") + "://www.example.com/piwik/";
    _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.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
  })();
</script>

<noscript>
<img src="http://www.example.com/piwik/piwik.php?idsite=1&rec=1" style="border:0" alt="" />
</noscript>

Thanks Fabian! :slight_smile:

Nope, tweaking the code had no effect. Well, maybe on scriptless visitors looking for the administrative gif, but not on the real thing. Wouldn’t expect it to, since you’re just moving the element “piwik/” from one place to anaother.

The answer turned out to be tucked away in my first post, in a line that I almost didn’t bother to include. It took me HOURS to figure this out, with massive use of Firefox’s Live Headers.

#1 Form of tracking code doesn’t matter-- new or old. If it is loaded properly, visits are recorded.

#2 Location of tracking code doesn’t matter. Currently I have four: large include footer that incorporates piwik code among other stuff; small include footer that contains only piwik code; directory-specific include footer that contains the old tracking code; raw tracking code-- also old-- within the document itself. (I had forgotten about the last two. Coincidentally, sorting out my Include files was already right at the top of my “to do” list, though it turned out not to be especially relevant.)

What does matter is
:: drumroll ::
Pages using the NEW tracking code will NOT work if the page header contains the line


<script type = "text/javascript" src = "/piwik/piwik.js"></script>

Comment it out and all is well. “script src” declarations that don’t involve piwik are fine. (This was a worry for me, since I do have external scripts on a few pages.) So all I have to do is hand-edit a bunch of pages that I thought I’d never need to hand-edit again.

Conversely, the old tracking code will only work WITH this line.

To me it seems utterly counter-intuitive that a script would only work if you don’t have a “src” declaration. If anyone can shed light I would be very interested. Not especially urgent now that the problem is fixed, but may be useful for future reference.