:S Understanding 'SitesManager.getJavascriptTag' behaviour

I am trying to programmatically obtain the javascript tracking code. I am able to obtain what I thought was the tracking code using getJavascriptTag, and insert it into my sites, but I don’t see any visits in the piwik dashboard. What I did notice was that the tracking code I am using has a pkBaseURL value equal to the URL from where the getJavascriptTag method was called in my original php script (the one that obtained and stored the tracking code), NOT the location of piwiki. To reproduce:

If I call SitesManager.getJavascriptTag from within piwik (example.com/piwik), it returns this:


<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://www.example.com/piwik/" : "http://www.example.com/piwik/");
...
<!-- End Piwik Tracking Code -->

When I call the same function, but within php code from another page, say from example.com/myfunction, i get this:


<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://www.example.com/myfunction/" : "http://www.example.com/myfunction/");
...
<!-- End Piwik Tracking Code -->

According to piwik, I should paste the code it has given (the one with example.com/piwik) “in all the pages you want to track with Piwik”, so basically I’ve been inserting the wrong code… How can I get the code that piwik wants me to put in all pages using the API, if getJavascriptTag won’t do it?

This may not be ideal, but now I simply add the ‘url’ parameter and set it to the piwik folder. It’s strange that I should have to do this though - is there no query that will get the tracking code for a site based on the id?

This is because Piwik has to guess where it is located relative to the web document root. In your second example, it guessed based on the REQUEST_URI of your php script.

You could use file_get_contents(‘http//example.com/piwik/?module=API&method=…’) instead of using the ‘url’ parameter explicitly, but this adds overhead.