]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5877 @0.75
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 9 Jun 2023 16:28:49 +0000 (18:28 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 9 Jun 2023 16:28:49 +0000 (18:28 +0200)
app/Fluidbook/Stats.php

index fda57ad221a0a4f936c193b8711c486361710443..29344b40e387d49da9d68cb4b15123ce5c2d38cb 100644 (file)
@@ -233,11 +233,12 @@ class Stats extends Reporting
         // the expanded dataset that includes the subtables of pages. For some reason, Matomo
         // doesn't aggregrate this value when there are sub pages, so we have to do it ourselves
         $expanded_stats = $this->_supportUniqueVisitors() ? 1 : 0; // Only fetch extra data when it will be used
-        $this->_pagesByPeriod = collect($this->getPageUrls(['expanded' => $expanded_stats]))
+        $this->_pagesByPeriod = collect($this->getPageUrls(['expanded' => 1]))
             ->map(function ($item, $date) {
                 return $this->_mapPagesByPeriod($item, $date);
             });
 
+
         //=== SUMMARY BY PERIOD
         // Generate a list of available periods, based on the visits API data.
         // If there are no visits for a certain period, we can assume that there won't be any data for other metrics.
@@ -401,24 +402,19 @@ class Stats extends Reporting
 
     protected function _mapPagesByPeriod($item, $date)
     {
-        //use ($period, $this->fluidbook_id, $hash, $this->_eventsByPeriod) {
         if (empty($item)) {
             return $item; // Some periods might have no data
         }
 
-        // Key results by their label to make it easier to isolate the "page" stats
-        // More specifically, we want to get rid of the "/index" data because these aren't true page views
-        $labelled = collect($item)->keyBy('label');
-
-        if (!isset($labelled['page'])) {
-            // If there's are no page stats, we treat it is if there's no data for this period
-            // This is necessary because it's possible that there will be an '/index' item but no 'page'
-            return [];
+        $data = [];
+        foreach ($item as $k => $v) {
+            if ($v['label'] === 'page') {
+                $data = array_merge($data, $v);
+            } else if ($v['label'] === '/index') {
+                $data['nb_openings'] = $v['nb_hits'];
+            }
         }
 
-        // By returning the page data directly (if available), it makes
-        // the returned data flatter and easier to process later for sums etc.
-        $data = $labelled['page'];
 
         //== Unique Visitors
         if ($this->_supportUniqueVisitors()) {
@@ -457,6 +453,10 @@ class Stats extends Reporting
         $data['nb_downloads'] = data_get($menu_events, 'download.nb_events', 0);
         $data['nb_prints'] = data_get($menu_events, 'print.nb_events', 0);
 
+        if (empty($data['nb_openings']) || empty($data['nb_visits'])) {
+            return [];
+        }
+
         return $data;
     }
 
@@ -523,9 +523,6 @@ class Stats extends Reporting
         $pageUrls = collect($this->getPageUrls(['period' => 'range', 'flat' => 1]))
             ->keyBy('label');
 
-        $this->viewData->openings = $pageUrls['/'];
-        unset($pageUrls['/']);
-
         //=== Group and combine page stats with related data
         $res = [];
         for ($page_number = 0; $page_number <= $this->fluidbook->getPagesNumber(); $page_number++) {