News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

adding Piwik analytics to SMF

Started by jonesH, June 10, 2014, 11:36:57 AM

Previous topic - Next topic

jonesH

Hello,

I am somewhat familiar with php and js, and I'm trying to add Piwik analytics to my forum, but can't quite figure out how. So far, piwik's advice and some opinions I found searching around haven't helped much. Basically, it boils down to adding a js script somewhere before the </body> of the index.template.php file of the default theme (so that all pages are tracked by piwik).

I tried the echo' option, include and require_once (separately) but in each case what shows is only the content of the page's <title>. No errors.

I'd appreciate any help in pointing me in the right direction.

(Running SMF 2.0.6 and Piwik 2.3.0)


mashby

Which hosting option are you trying to implement for Piwik?
Always be a little kinder than necessary.
- James M. Barrie

jonesH

Well... My forum is hosted on GoDaddy and they have a Piwik add-on available; I am also aware of the SMF add-on. The reason I opted for a manual install and setup was that in the first two instances Piwik is listed as version 1.x, whereas their latest downloadable version is 2.x - thus the confusion on my end.

So I created a dedicated db, downloaded and expanded 2.x, uploaded, installed and added my forum to the tracked sites (so far, all good), now I need to add the actual tracking code to the forms templates.

I wouldn't mind undoing all this and going with the smf mod, if you think that's a better option...

mashby

What tracking code was supplied to you?
Always be a little kinder than necessary.
- James M. Barrie

jonesH

I posted a reply here and upon re-reading it I believe I spotted some error on my end. Please allow me some time to work on this. Meanwhile, thanks for your help.

jonesH

Well, things seem to be working for now. The supplied code is a <script> within a pair of html comments, so I just added the whole thing at the bottom of index.template.php, after the closing php bracket, which I don't like at all as a permanent solution, so I am going with it as a bandaid.

I will update here as I get further.

mashby

Interesting placement...did that actually really work? Are you seeing usage data in Piwik? I'd be a little shocked if you are.
Always be a little kinder than necessary.
- James M. Barrie

jonesH

Yes, I logged in the forum, hit a few pages, logged out and checked in piwik - all my travels were logged. The forum not being yet ready to go live, I am the only user, so I would have to add some more accounts and see how things work out.

Anyway, there seems to be two ways to add html to php: either echo line by line, or include the html bit outside of the php brackets. It is though inelegant, to say the least. I would prefer a link of some sort.

mashby

You should be able to insert the code into the HTML portion of the PHP code. Find </body>. Insert the code provided. Just keep in mind ' characters would have be escaped with the \ dealio. You don't need to echo line by line. One echo statement or inserted into an already existing echo should work just fine. Another idea that came to mind. Global Headers and Footers mod.
Always be a little kinder than necessary.
- James M. Barrie

jonesH

#10
I now think that escaping is where my problem is, so I will work on that next. As for mods, I feel iffy about adding yet another mod to the forum, especially considering that installing the Piwik part should be trivial.

Thanks for the help, I'll be back with updates.

Update: btw, the piwik site explaining the tracking code can be found here.

Kindred


echo '
<!-- Piwik -->
<script type="text/javascript"> var _paq = _paq || []; (function(){ var u=(("https:" == document.location.protocol) ? "https://{$PIWIK_URL}/" : "http://{$PIWIK_URL}/"); _paq.push([\'setSiteId\', {$IDSITE}]); _paq.push([\'setTrackerUrl\', u+\'piwik.php\']); _paq.push([\'trackPageView\']); _paq.push([\'enableLinkTracking\']); 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>
<!-- End Piwik Code -->';



try that
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

jonesH

I tried that, too, and I get a PHP parse error (expecting `'," or `';") on the first _paq.push line.

But I will mark this topic as solved because regardless how I proceed from here, Piwik does work with SMF and I did a way to add it.

Thank you all for your help.

jonesH

UPDATE:

It would seem that the tracking code I got from Piwik was my problem all along, since after adding the code found at the bottom of this topic (and edited for my particular URL):

http://www.simplemachines.org/community/index.php?topic=381722.0

everything suddenly worked just fine.

By the way, I inserted the code just before the </body></html> tags, circa line number 365.

Another useful link is this:

http://piwik.org/integrate/


Advertisement: