How to Insert Piwik code into Joomla Template

I am using Joomla 2.5 and recently I found Piwik script, I donwloaded it and installed in one directory in the joomla folder. But I am having difficulties to insert the Piwik Code into the Joomla Template.
Let supose that I have this Piwik Code:

And this template code: (Where should I put the Piwik Code?)

<?php /** * @package Joomla.Site * @subpackage Templates.beez_20 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ // No direct access. defined('_JEXEC') or die; jimport('joomla.filesystem.file'); // check modules $showRightColumn = ($this->countModules('position-3') or $this->countModules('position-6') or $this->countModules('position-8')); $showbottom = ($this->countModules('position-9') or $this->countModules('position-10') or $this->countModules('position-11')); $showleft = ($this->countModules('position-4') or $this->countModules('position-7') or $this->countModules('position-5')); if ($showRightColumn==0 and $showleft==0) { $showno = 0; } JHtml::_('behavior.framework', true); // get params $color = $this->params->get('templatecolor'); $logo = $this->params->get('logo'); $navposition = $this->params->get('navposition'); $app = JFactory::getApplication(); $doc = JFactory::getDocument(); $templateparams = $app->getTemplate(true)->params; $doc->addStyleSheet($this->baseurl.'/templates/system/css/system.css'); $doc->addStyleSheet($this->baseurl.'/templates/'.$this->template.'/css/position.css', $type = 'text/css', $media = 'screen,projection'); $doc->addStyleSheet($this->baseurl.'/templates/'.$this->template.'/css/layout.css', $type = 'text/css', $media = 'screen,projection'); $doc->addStyleSheet($this->baseurl.'/templates/'.$this->template.'/css/print.css', $type = 'text/css', $media = 'print'); $files = JHtml::_('stylesheet', 'templates/'.$this->template.'/css/general.css', null, false, true); if ($files): if (!is_array($files)): $files = array($files); endif; foreach($files as $file): $doc->addStyleSheet($file); endforeach; endif; $doc->addStyleSheet('templates/'.$this->template.'/css/'.htmlspecialchars($color).'.css'); if ($this->direction == 'rtl') { $doc->addStyleSheet($this->baseurl.'/templates/'.$this->template.'/css/template_rtl.css'); if (file_exists(JPATH_SITE . '/templates/' . $this->template . '/css/' . $color . '_rtl.css')) { $doc->addStyleSheet($this->baseurl.'/templates/'.$this->template.'/css/'.htmlspecialchars($color).'_rtl.css'); } } $doc->addScript($this->baseurl.'/templates/'.$this->template.'/javascript/md_stylechanger.js', 'text/javascript'); $doc->addScript($this->baseurl.'/templates/'.$this->template.'/javascript/hide.js', 'text/javascript'); ?>

                                    <?php if ($logo): ?>
                                    <img src="<?php echo $this->baseurl ?>/<?php echo htmlspecialchars($logo); ?>"  alt="<?php echo htmlspecialchars($templateparams->get('sitetitle'));?>" />
                                    <?php endif;?>
                                    <?php if (!$logo ): ?>
                                    <?php echo htmlspecialchars($templateparams->get('sitetitle'));?>
                                    <?php endif; ?>
                                    <span class="header1">
                                    <?php echo htmlspecialchars($templateparams->get('sitedescription'));?>
                            </div><!-- end logoheader -->
                                    <ul class="skiplinks">
                                            <li><a href="#main" class="u2"><?php echo JText::_('TPL_BEEZ2_SKIP_TO_CONTENT'); ?></a></li>
                                            <li><a href="#nav" class="u2"><?php echo JText::_('TPL_BEEZ2_JUMP_TO_NAV'); ?></a></li>
                                        <?php if($showRightColumn ):?>
                                        <li><a href="#additional" class="u2"><?php echo JText::_('TPL_BEEZ2_JUMP_TO_INFO'); ?></a></li>
                                       <?php endif; ?>
                                    <h2 class="unseen"><?php echo JText::_('TPL_BEEZ2_NAV_VIEW_SEARCH'); ?></h2>
                                    <h3 class="unseen"><?php echo JText::_('TPL_BEEZ2_NAVIGATION'); ?></h3>
                                    <jdoc:include type="modules" name="position-1" />
                                    <div id="line">
                                    <div id="fontsize"></div>
                                    <h3 class="unseen"><?php echo JText::_('TPL_BEEZ2_SEARCH'); ?></h3>
                                    <jdoc:include type="modules" name="position-0" />
                                    </div> <!-- end line -->

                    </div><!-- end header -->
                    <div id="<?php echo $showRightColumn ? 'contentarea2' : 'contentarea'; ?>">
                                    <div id="breadcrumbs">

                                                    <jdoc:include type="modules" name="position-2" />


                                    <?php if ($navposition=='left' and $showleft) : ?>

                                                    <div class="left1 <?php if ($showRightColumn==NULL){ echo 'leftbigger';} ?>" id="nav">
                                               <jdoc:include type="modules" name="position-7" style="beezDivision" headerLevel="3" />
                                                            <jdoc:include type="modules" name="position-4" style="beezHide" headerLevel="3" state="0 " />
                                                            <jdoc:include type="modules" name="position-5" style="beezTabs" headerLevel="2"  id="3" />

                                                    </div><!-- end navi -->
           <?php endif; ?>

                                    <div id="<?php echo $showRightColumn ? 'wrapper' : 'wrapper2'; ?>" <?php if (isset($showno)){echo 'class="shownocolumns"';}?>>

                                            <div id="main">

                                            <?php if ($this->countModules('position-12')): ?>
                                                    <div id="top"><jdoc:include type="modules" name="position-12"   />
                                            <?php endif; ?>

                                                    <jdoc:include type="message" />
                                                    <jdoc:include type="component" />

                                            </div><!-- end main -->

                                    </div><!-- end wrapper -->

                            <?php if ($showRightColumn) : ?>
                                    <h2 class="unseen">
                                            <?php echo JText::_('TPL_BEEZ2_ADDITIONAL_INFORMATION'); ?>
                                    <div id="close">
                                            <a href="#" onclick="auf('right')">
                                                    <span id="bild">
                                                            <?php echo JText::_('TPL_BEEZ2_TEXTRIGHTCLOSE'); ?></span></a>

                                    <div id="right">
                                            <a id="additional"></a>
                                            <jdoc:include type="modules" name="position-6" style="beezDivision" headerLevel="3"/>
                                            <jdoc:include type="modules" name="position-8" style="beezDivision" headerLevel="3"  />
                                            <jdoc:include type="modules" name="position-3" style="beezDivision" headerLevel="3"  />
                                    </div><!-- end right -->
                                    <?php endif; ?>

                    <?php if ($navposition=='center' and $showleft) : ?>

                                    <div class="left <?php if ($showRightColumn==NULL){ echo 'leftbigger';} ?>" id="nav" >

                                            <jdoc:include type="modules" name="position-7"  style="beezDivision" headerLevel="3" />
                                            <jdoc:include type="modules" name="position-4" style="beezHide" headerLevel="3" state="0 " />
                                            <jdoc:include type="modules" name="position-5" style="beezTabs" headerLevel="2"  id="3" />

                                    </div><!-- end navi -->
               <?php endif; ?>

                            <div class="wrap"></div>

                            </div> <!-- end contentarea -->

                    </div><!-- back -->

            </div><!-- all -->

            <div id="footer-outer">
                    <?php if ($showbottom) : ?>
                    <div id="footer-inner">

                            <div id="bottom">
                                    <div class="box box1"> <jdoc:include type="modules" name="position-9" style="beezDivision" headerlevel="3" /></div>
                                    <div class="box box2"> <jdoc:include type="modules" name="position-10" style="beezDivision" headerlevel="3" /></div>
                                    <div class="box box3"> <jdoc:include type="modules" name="position-11" style="beezDivision" headerlevel="3" /></div>

                            <?php endif ; ?>

                    <div id="footer-sub">

                            <div id="footer">

                                    <jdoc:include type="modules" name="position-14" />
                                    <p>Builded by: <a href="">Horizont Center®</a></p>
                            </div><!-- end footer -->


			<jdoc:include type="modules" name="debug" />

Copy and paste the Piwik Code as generated in your Piwik backend just before the tag if you want to do this manually.

There should be plugins on the Joomla site to install, which makes things much easier. You can then add your Site ID and your Piwik Installation in the backend into the plugin and the plugin adds the Piwik code for you when it runs through your template file to include all the content and other things.

robertb thnx for your reply, but I still have the same problem :(… I have tried the Joomla Plugin and works, but when I update Piwik the Joomla component doesn’t work anymore…
So I think that the method of inserting a code into the template was a good solution.

In order to establish if the Joomla Plugin or your code on the page works or not you would need to check if it generates the correct code in your source on the page. Look out for the correct piwik base and site ID.

If this is fine, use Firebug or something similar to see what the javascript code posts back to Piwik. It might not even get as far to look at the Piwik code if there are other major JavaScript errors generated by your site. If the piwik.js makes a request to your Piwik install with the right site id, your problem is most likely with your Piwik install and not with the Plugin.

As a next step I would take the URL the javascript calls (e.g. and put it into a browser to see if you get a clear pixel or a error message (e.g. Server Error 500 or 404 not found). Take it from there.

I so the source the page and on the bottom of every page is generated this code :

	<!-- Piwik -->
	<script type="text/javascript">
	var pkBaseURL = (("https:" == document.location.protocol) ? "https://localhost/joomla/apps/piwik/" : "http://localhost/joomla/apps/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);
	} catch( err ) {}
	<!-- End Piwik Code -->
[b]The Piwik instaler gave me this code when I intalled it:[/b]

Could you tell what should I do?

Dont insert the code with “localhost” or your visitors wouldn’t be tracked. Make sure you insert the code with the site url