Busiest_Day_Of_Week_Module.php
5 months ago
Busiest_Time_Of_Day_Module.php
1 year ago
Line_Chart_Module.php
9 months ago
Map_Module.php
9 months ago
Module.php
5 months ago
New_Sessions_Module.php
1 year ago
Pie_Chart_Module.php
9 months ago
Quick_Stats_Module.php
9 months ago
Recent_Conversions_Module.php
5 months ago
Recent_Views_Module.php
5 months ago
Top_Ten_Module.php
9 months ago
Busiest_Day_Of_Week_Module.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Overview\Modules; |
| 4 | |
| 5 | use IAWPSCOPED\Carbon\CarbonImmutable; |
| 6 | use IAWPSCOPED\Carbon\CarbonInterface; |
| 7 | use IAWP\Illuminate_Builder; |
| 8 | use IAWP\Tables; |
| 9 | use IAWP\Utils\Timezone; |
| 10 | use IAWPSCOPED\Illuminate\Support\Collection; |
| 11 | /** @internal */ |
| 12 | class Busiest_Day_Of_Week_Module extends \IAWP\Overview\Modules\Module |
| 13 | { |
| 14 | public function module_type() : string |
| 15 | { |
| 16 | return 'busiest-day-of-week'; |
| 17 | } |
| 18 | public function module_name() : string |
| 19 | { |
| 20 | return \__('Busiest Day of Week', 'independent-analytics'); |
| 21 | } |
| 22 | public function calculate_dataset() |
| 23 | { |
| 24 | $tables = Tables::class; |
| 25 | $offset = Timezone::site_offset(); |
| 26 | $sessions_by_day_of_week = Illuminate_Builder::new()->selectRaw("DAYOFWEEK(CONVERT_TZ(sessions.created_at, '+00:00', '{$offset}')) - 1 AS day")->selectRaw("WEEK(CONVERT_TZ(sessions.created_at, '+00:00', '{$offset}')) AS week")->selectRaw('COUNT(*) AS sessions')->from($tables::sessions(), 'sessions')->whereRaw("CONVERT_TZ(sessions.created_at, '+00:00', '{$offset}') > CONVERT_TZ(CURDATE(), '+00:00', '{$offset}') - INTERVAL 90 DAY")->groupBy(['week', 'day']); |
| 27 | $query = Illuminate_Builder::new()->select(['day'])->selectRaw('AVG(sessions) as sessions')->fromSub($sessions_by_day_of_week, 'sessions')->groupBy('day'); |
| 28 | return $query->get()->map(function ($row) { |
| 29 | return ['day' => \intval($row->day), 'sessions' => \intval($row->sessions)]; |
| 30 | })->all(); |
| 31 | } |
| 32 | public function get_labels(array $dataset) : array |
| 33 | { |
| 34 | $days = $this->shift_to_start_of_week(\range(0, 6)); |
| 35 | return \array_map(function ($day) { |
| 36 | $date = CarbonImmutable::now('utc')->startOfWeek(CarbonInterface::SUNDAY)->addDays($day); |
| 37 | return \json_encode(['tick' => $date->format('l'), 'tooltipLabel' => $date->format('l')]); |
| 38 | }, $days); |
| 39 | } |
| 40 | public function get_sessions(array $dataset) : array |
| 41 | { |
| 42 | $days = $this->shift_to_start_of_week(\range(0, 6)); |
| 43 | $dataset_collection = Collection::make($dataset); |
| 44 | return \array_map(function ($day) use($dataset_collection) { |
| 45 | $matching_row = $dataset_collection->first(function ($row) use($day) { |
| 46 | return $row['day'] === $day; |
| 47 | }); |
| 48 | if ($matching_row === null) { |
| 49 | return 0; |
| 50 | } |
| 51 | return $matching_row['sessions']; |
| 52 | }, $days); |
| 53 | } |
| 54 | public function shift_to_start_of_week(array $dataset) : array |
| 55 | { |
| 56 | $day_of_week = \IAWPSCOPED\iawp()->get_option('iawp_dow', 0); |
| 57 | $collection = Collection::make($dataset); |
| 58 | return $collection->splice($day_of_week)->merge($collection)->values()->all(); |
| 59 | } |
| 60 | protected function module_fields() : array |
| 61 | { |
| 62 | return ['busiest_date_range']; |
| 63 | } |
| 64 | } |
| 65 |