How to use Piwik for your website delivered by Silverstripe cms

Abstract: Silverstripe is a common content management system which is known for its flexibility. Piwik describes itself as "open source web analytics". Installation of both is easy. While you can configure almost everything behind the stage you want to go according to web standards, propably. The sites Silverstripe creates are valid xhtml+xml and therefore the content type is "text/html". This is not compatible with Piwik's  default tracking code if you're using Firefox.

Installation directory

Silverstripe and Piwik don't get in the way of each other. I recommend using the default folder "piwik" inside your Silverstripe root directory. Now it should be aside with "assets", "mysite", "themes" etc.

Tracking

To track your visitors you have to add piwik specifik Javascript to your site. The default is:

<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ?
"https://www.in.the.net/piwik/" : "http://www.in.the.net/piwik/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script>
<noscript><p><img src="http://www.in.the.net/piwik/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tag -->

Mozilla Firefox trouble shooting

You may observe that Piwik isn't tracking any users if you test using Firefox. Open error console and see if loading of your site's tracking code results to an:

[Exception... "Operation is not supported"  code: "9" nsresult: "0x80530009 (NS_ERROR_DOM_NOT_SUPPORTED_ERR)"  location: "http://www.in.the.net/?flush=1 Line: ..."]

The Line number corresponds to the line where the "document.write" statement is. The reason is simple: Since your templates come as xml by default ("<?xml version="1.0" encoding="UTF-8"?>") you can't use "document.wirte" to work with DOM (document object model). At least with Firefox. You have to modify the XML-Dom instead. Try this code:

<!-- Piwik -->
<script type="text/javascript">
 var pkBaseURL = (("https:" == document.location.protocol) ? "https://www.in.the.net/piwik/" : "http://www.in.the.net/piwik/");
 // XHTML tracking code
 try {
   document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
 } catch ( err ) { alert(err);
   var xscript = document.createElementNS("http://www.w3.org/1999/xhtml","script");
     xscript.setAttribute("type", "text/javascript");
     xscript.setAttribute("src", pkBaseURL + "piwik.js");
   document.getElementsByTagName("body")[0].appendChild(xscript);
 }
 //document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
 try {
  var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
  piwikTracker.trackPageView();
  piwikTracker.enableLinkTracking();
 } catch( err ) {}
</script>
<noscript>
 <p><img src="http://www.in.the.net/piwik/piwik.php?idsite=1" style="border:0" alt="" /></p>
</noscript>
<!-- End Piwik Tag -->

Basically this it what it does: It tries to write into the document. If it fails it will create an appropriate <script> tag and includes it into the DOM.

If I would have known this before it would have saved me two hours.

Geben Sie einen Kommentar ab

Kommentare

Bisher hat niemand diese Seite kommentiert.

RSS Feed für die Kommentare auf dieser Seite | RSS feed für alle Kommentare