Support for Piwik Analytics as an alternative to Google Analytics

(Lee_Ars) #1

I do my own analytics with Piwik, a FOSS self-hosted Google Analytics-like platform. It relies on a similar bit of javascript in the <head> section of each page.

Obviously this is a low-priority question, but might it be possible to have Discourse support Piwik as an alternative to GA in the site settings? Actually, all it would take is the ability to inject the javascript code into each page’s <head> (or in the <body>—that works fine, too).

Thanks :smile:

Can't edit a specific wiki topic
(Jeff Atwood) #2

Don’t we have the ability, for admins, to insert arbitrary markup of any kind in the header?

I thought we did – if not we should add that.

(Sam Saffron) #3

we probably want analytics in the footer though.

(Jeff Atwood) #4

I don’t think it matters in this case and he is asking for the header anyway.

(Richard - #5

Piwik code should be inserted just before the </body> tag.
I think it might be a good idea to provide for free markup blocks in three places: in the <head>, right after <body> and just before </body>

(Lee_Ars) #6

I’m using Octopress for my blog, which is Jekyll-based, and it does analytics in the body just before the footer. On my non-blog site, which is hand-coded instead of generated, I’ve got the code in the head. Looks like Piwik doesn’t particularly care if it’s in the head or body, so any of the above works for me :smile:

(Lee_Ars) #7

Quick update on this—I had totally overlooked the “Header” section under Customizations. I’ve cut-n-pasted my piwik script into the header section and applied it, and it appears to be working—Piwik is registering visits.

I need a little larger sample size to see if it’s correctly showing pages and things, though. Right now, though I hit several pages from my iphone, all I show is a single visit to /index (I’ve got LAN clients excluded from tracking and I don’t want to go screw around with changing that). Will let folks visit and click around and then report back.

(Andrew Hunn) #8

It’s my understanding that the header section is actually just the global header that is still at the top of <body>.

This might work for the use case this thread is discussing, but it’s not a long-term solution for most admins who are going to want the ability to place arbitrary bits of code (javascript tracking, ad codes, meta definitions, etc.) in <head> and just before </body> in the footer.

Adding these two extra sections to customize would be a very welcome addition.

(Lee_Ars) #9

Yeah, so far all I’m seeing are multiple visits to the front page. I suspect that the highly dynamic nature of Discourse means that the tracking code needs to be further down in the page, and that its presence in the header is causing it to not be invoked correctly when users click on topics and posts and things.

(Lee_Ars) #10

Closing the loop on this: Piwik works great & records topic visits. The only wrinkle is due to Discourse’s display of threads as directory objects instead of distinct file objects; there’s more analytic activity than probably needs to happen, because as the URL dynamically updates during scrolling, each distinct URL is recorded. I don’t know enough about piwik (or google analytics) to know if there’s a workaround, or if one’s even needed:

But all is well. If at some point you guys want to support piwik tracking code injection in <body>, I’ll use that, but things are looking good for now.

(Sam Saffron) #11

Keep in mind, we do this trick for google analytics:

It ensures that as you navigate around, page views are reported back. Being a JavaScript app, our concept of “page view” is a bit non traditional. Even though you click on a topic, the url changes and so on, a page view in the traditional sense is not triggered or reported (we simply grab a bit of json from the server).

You can work around this with something like the following in the footer (pending a footer hook):

Discourse.ApplicationController .reopen({

  routeChangedPwik: function(){

    if(this.afterFirstHitPwik) {'afterRender', function() {
    } else {
      this.afterFirstHitPwik = true;

(Lee_Ars) #12

Good stuff; bookmarked. Thanks, @sam!

(Andrew Hunn) #13

Does this mean we’re getting footer hooks? :pray:

(Michael Jonker) #14


Is there any development on this?

I have inserted the Piwik code in the head and added:

to the bottom of the page in script tags. Page tracking triggers unreliably. Am I missing something or is this still in development?

(Sam Saffron) #15

@zogstrip added this

Does it not solve this issue?

(Michael Jonker) #16

This is done:

Problem is that I do not understand the details of the code and I am blindly cutting and pasting :blush:

Piwik is tracking, but it would appear not to be tracking every page hit. I am trying to figure out if I have missed something.

(Sam Saffron) #17

@eviltrout any ideas here, the code seems correct

(Robin Ward) #18

The code looks okay but it’s hard to debug this way.

One thing you could do is add a: console.log('tracked!') just before line 8. Then open a developer console and see if the javascript is running properly.

(jon r) #19

I think as Piwik more commonly used as a Google Analytics substitute it would be nice to provide direct Piwik-API support similiar to the textboxes for Google Analytics. It would equal the heavy solution out and be a statement towards the reliability of Open Source Software in general.

(Lee_Ars) #20

I’d love to see API support. @sam and @eviltrout, I can facilitate an intro to the piwik guys if you want—I’ve written about them before and they’re typically super-responsive. Because I know you guys are totally hunting for new stuff to do :wink: