Adding subtable to Custom Report doesn't work

I am currently developing a custom report that extends the report Content Names with data from another database. I would like to add the subtable that shows which content pieces were clicked.

This is my Code:

public function getMyReport($idSite, $period, $date, $segment = false)
    {
        $data = \Piwik\API\Request::processRequest('Contents.getContentNames', array(
            'idSite' => $idSite,
            'period' => $period,
            'date' => $date,
            'segment' => $segment,
            'filter_limit'=> -1,
            'expanded'=>1,
        ));
        $data->applyQueuedFilters();
        $result = $data->getEmptyClone($keepFilters = false);

        foreach ($data->getRows() as $visitRow) {
            //GET ADDITIONAL DATA FROM ANOTHER DATABASE

            $row = new \Piwik_DataTable_Row(array(array(
                'ID' => $visitRow['label'],
                'Seen' => $visitRow['nb_impressions'],
                'Clicked' => $visitRow['nb_interactions'],
                'Click Rate' => $visitRow['interaction_rate'],
//ADDITIONAL DATA
            )));

            $subtabledata=\Piwik\API\Request::processRequest('Contents.getContentNames', array(
                'idSite' => $idSite,
                'period' => $period,
                'date' => $date,
                'segment' => $segment,
                'filter_limit'=> -1,
                'idSubtable'=>1,
            ));
            $subtabledata->applyQueuedFilters();
            $subresult = new DataTable();
            foreach ($subtabledata->getRows() as $subtabledataRow){
                $subRow =new \Piwik_DataTable_Row(array(Row::COLUMNS => array(
                    'label'=>$subtabledataRow['label'],
                    'Seen' => $subtabledataRow['nb_impressions'],
                    'Clicked' => $subtabledataRow['nb_interactions'],
                    'Click Rate' => $subtabledataRow['interaction_rate'],
                )));
                $subresult->addRow($subRow);
            };
            $row->setSubtable($subresult);
            $result->addRow($row);
        }
        return $result;
    }

The subtable in the report displays the following error message: There is no data for this report.
Does anyone know where my mistake is?

Second question: My first request does not return idsubdatatable - do I need an extra note in the request?

1 Like

I am still struggling with this. Do I have to specify the subtable somewhere in GetMyReport.php?

I am quite confused. When I output my $subRow via var_dump I get the following information:

object(Piwik_DataTable_Row)#3145 (5) { ["maxVisitsSummed"]=> int(0) ["metadata": "Piwik\DataTable\Row":private]=> array(0) { } ["isSubtableLoaded": "Piwik\DataTable\Row":private]=> bool(false) ["subtableId"]=> NULL ["storage": "ArrayObject":private]=> array(4) { ["label"]=> string(5) "list" ["lakes"]=> int(1) ["Clicked"]=> int(0) ["Click Rate"]=> string(2) "0%" } }

But when I try to open the subtable in the report I still get ‘there is no data for this report.’ or a never ending ‘loading’.

Does anyone have an idea?