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
3 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
New_Sessions_Module.php
41 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Overview\Modules; |
| 4 | |
| 5 | use IAWP\Date_Range\Relative_Date_Range; |
| 6 | use IAWP\Illuminate_Builder; |
| 7 | use IAWP\Tables; |
| 8 | use IAWP\Utils\Number_Formatter; |
| 9 | use IAWPSCOPED\Illuminate\Database\Query\JoinClause; |
| 10 | /** @internal */ |
| 11 | class New_Sessions_Module extends \IAWP\Overview\Modules\Module |
| 12 | { |
| 13 | public function module_type() : string |
| 14 | { |
| 15 | return 'new-sessions'; |
| 16 | } |
| 17 | public function module_name() : string |
| 18 | { |
| 19 | return \__('New vs. Returning Sessions', 'independent-analytics'); |
| 20 | } |
| 21 | public function calculate_dataset() |
| 22 | { |
| 23 | $date_range = Relative_Date_Range::range_by_id($this->attributes['date_range'] ?? null); |
| 24 | $tables = Tables::class; |
| 25 | // Find all session in range |
| 26 | $session_in_range = Illuminate_Builder::new()->selectRaw('DISTINCT session_id')->from($tables::views())->whereBetween('viewed_at', [$date_range->iso_start(), $date_range->iso_end()]); |
| 27 | $query = Illuminate_Builder::new()->selectRaw('CAST(SUM(IF(is_first_session = 1, 1, 0)) AS SIGNED) AS is_first_session')->selectRaw('CAST(SUM(IF(is_first_session = 0, 1, 0)) AS SIGNED) AS is_returning_session')->from($tables::sessions(), 'sessions')->joinSub($session_in_range, 'sessions_in_range', function (JoinClause $join) { |
| 28 | $join->on('sessions_in_range.session_id', '=', 'sessions.session_id'); |
| 29 | }); |
| 30 | $row = $query->first(); |
| 31 | if ($row->is_first_session === null && $row->is_returning_session === null) { |
| 32 | return []; |
| 33 | } |
| 34 | return [['label' => \__('New Sessions', 'independent-analytics'), 'unit' => \__('Sessions', 'independent-analytics'), 'value' => \intval($row->is_first_session), 'formatted_value' => Number_Formatter::integer($row->is_first_session)], ['label' => \__('Returning Sessions', 'independent-analytics'), 'unit' => \__('Sessions', 'independent-analytics'), 'value' => \intval($row->is_returning_session), 'formatted_value' => Number_Formatter::integer($row->is_returning_session)]]; |
| 35 | } |
| 36 | protected function module_fields() : array |
| 37 | { |
| 38 | return ['date_range']; |
| 39 | } |
| 40 | } |
| 41 |