RSS

Piwik: Updated MultiSitesPlugin

Saturday, 31 July 2010

UPDATE - I fixed a bug in the code. My copy and paste skills let me down :)

We use the MultiSitesPluging for Piwik to get a view of all the traffic across a collection of sites. It allows us to see all the basic stats for multiple websites on one page. A few weeks ago, I made a few updates to the plugin to aggregate the data for the all the sites, and since someone else asked about it, thought I would document what I did :)

First was a few updates to MultiSitesPlugin/Controller.php

$total_visits = 0;
$total_actions = 0;
$total_unique = 0;

if(count($my_sites) == 1 )
{
    $my_sites[0]['visits'] = $visits_piwik;
    $my_sites[0]['actions'] = $actions_piwik;
    $my_sites[0]['unique'] = $unique_users;
    $my_sites[0]['sparkline'] = $sparklines_piwik_array[$my_sites[0]['idsite']];
    $my_sites[0]['summary'] = $summary['text'];
    $my_sites[0]['summary_value'] = $summary['value'];
    $total_visits = $visits_piwik;
    $total_actions = $actions_piwik;
    $total_unique = $unique_users;
}
else
{
    $visits_piwik_array = $visits_piwik->getArray();
    $actions_piwik_array = $actions_piwik->getArray();
    $unique_users_piwik_array = $unique_users->getArray();
    foreach($my_sites as &$site)
    {
        $site['visits'] = $visits_piwik_array[$site['idsite']]->getColumn(0);
        $site['actions'] = $actions_piwik_array[$site['idsite']]->getColumn(0);
        $site['unique'] = $unique_users_piwik_array[$site['idsite']]->getColumn(0);
        $site['sparkline'] = $sparklines_piwik_array[$site['idsite']];
        $site['summary'] = $summary[$site['idsite']]['text'];
        $site['summary_value'] = $summary[$site['idsite']]['value'];
        $total_visits = $total_visits + $visits_piwik_array[$site['idsite']]->getColumn(0);
        $total_actions = $total_actions + $actions_piwik_array[$site['idsite']]->getColumn(0);
        $total_unique = $total_unique + $unique_users_piwik_array[$site['idsite']]->getColumn(0);
    }
    usort($my_sites, array("Piwik_MultiSitesPlugin_Controller", "sort_by_".$this->order));
}
$this->count = count($my_sites);

as well as the addition of a few lines here:

$view->page = $this->page;
$view->limit = $this->limit;
$view->count = $this->count;
$view->order_by = $this->order_by;
$view->order = $this->order;
$view->page = $this->page;
$view->total_visits = $total_visits;
$view->total_actions = $total_actions;
$view->total_unique = $total_unique;
$view->total_site_count = count($my_sites);
echo $view->render();

I then created a new template file in the templates directory called total.tpl:

<tr align="center"  style="border-right: 1px solid black;">
<td align="center" class="column">
</td>
<td align="left" class="column" id="siteName">
<b>TOTAL ({$total_site_count})</b>
</td>
<td align="right" class="column" id="visits">
    {$total_visits}&nbsp;</td>
</td>
<td align="right" class="column" id="actions">
    {$total_actions}&nbsp;</td>
</td>
<td align="right" class="column" id="unique">
    {$total_unique}&nbsp;</td>
</td>
<td align="center" class="column" id="sparkline"> </td>
<td class="column"></td>
</tr>

Lastly, I modified the templates/index.tpl file and added the following line (in bold)

foreach from=$my_sites key=i item=site}
{include file='MultiSitesPlugin/templates/row.tpl'}
{/foreach}
{include file='MultiSitesPlugin/templates/total.tpl'}
<tr>

I hope that helps those that are interested :)



This entry was written by Karl Kopp, posted on Saturday, 31 July 2010 Bookmark the permalink. Follow any comments here with the RSS feed for this post. You can post a comment.

Post a comment

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact me so we can take care of it!

Visit my friends!

A few highly recommended friends...

About

Some semi-interesting ramblings from a technology geek (me, Karl Kopp) about some future adventures...