Campaigns.php
2 years ago
Cities.php
2 years ago
Countries.php
2 years ago
Device_Browsers.php
2 years ago
Device_OSS.php
2 years ago
Device_Types.php
2 years ago
Filter.php
2 years ago
Pages.php
2 years ago
Referrers.php
2 years ago
Rows.php
2 years ago
Pages.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP\Rows; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Illuminate_Builder; |
| 6 | use IAWP_SCOPED\IAWP\Models\Page; |
| 7 | use IAWP_SCOPED\IAWP\Query; |
| 8 | use IAWP_SCOPED\Illuminate\Database\Query\Builder; |
| 9 | use IAWP_SCOPED\Illuminate\Database\Query\JoinClause; |
| 10 | /** @internal */ |
| 11 | class Pages extends Rows |
| 12 | { |
| 13 | public function attach_filters(Builder $query) : void |
| 14 | { |
| 15 | $query->joinSub($this->query(\true), 'page_rows', function (JoinClause $join) { |
| 16 | $join->on('page_rows.id', '=', 'views.resource_id'); |
| 17 | }); |
| 18 | } |
| 19 | protected function fetch_rows() : array |
| 20 | { |
| 21 | $rows = $this->query()->get()->all(); |
| 22 | return \array_map(function (object $row) { |
| 23 | return Page::from_row($row); |
| 24 | }, $rows); |
| 25 | } |
| 26 | private function query(?bool $skip_pagination = \false) : Builder |
| 27 | { |
| 28 | global $wpdb; |
| 29 | if ($skip_pagination) { |
| 30 | $this->number_of_rows = null; |
| 31 | } |
| 32 | $views_table = Query::get_table_name(Query::VIEWS); |
| 33 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 34 | $resources_table = Query::get_table_name(Query::RESOURCES); |
| 35 | $comments_table = $wpdb->prefix . 'comments'; |
| 36 | $current_period_array = [$this->date_range->iso_start(), $this->date_range->iso_end()]; |
| 37 | $previous_period_array = [$this->date_range->previous_period()->iso_start(), $this->date_range->previous_period()->iso_end()]; |
| 38 | $total_period_array = [$this->date_range->previous_period()->iso_start(), $this->date_range->iso_end()]; |
| 39 | $calculated_columns = ['views_growth', 'visitors_growth', 'bounce_rate', 'exit_percent']; |
| 40 | $has_calculate_column_filter = !empty(\array_filter($this->filters, function ($filter) use($calculated_columns) { |
| 41 | return \in_array($filter['column'], $calculated_columns); |
| 42 | })); |
| 43 | if (\in_array($this->sort_configuration->column(), $calculated_columns)) { |
| 44 | $has_calculate_column_filter = \true; |
| 45 | } |
| 46 | $database_sort_columns = ['title' => 'cached_title', 'url' => 'cached_url', 'author' => 'cached_author', 'type' => 'cached_type_label', 'date' => 'cached_date', 'category' => 'cached_category']; |
| 47 | $sort_column = $this->sort_configuration->column(); |
| 48 | foreach ($database_sort_columns as $key => $value) { |
| 49 | if ($sort_column === $key) { |
| 50 | $sort_column = $value; |
| 51 | } |
| 52 | } |
| 53 | $pages_query = Illuminate_Builder::get_builder(); |
| 54 | $pages_query->select('resources.*')->selectRaw('COUNT(DISTINCT IF(views.viewed_at BETWEEN ? AND ?, views.id, NULL)) AS views', $current_period_array)->selectRaw('COUNT(DISTINCT IF(views.viewed_at BETWEEN ? AND ?, views.id, NULL)) AS previous_period_views', $previous_period_array)->selectRaw('COUNT(DISTINCT IF(views.viewed_at BETWEEN ? AND ?, sessions.visitor_id, NULL)) AS visitors', $current_period_array)->selectRaw('COUNT(DISTINCT IF(views.viewed_at BETWEEN ? AND ?, sessions.visitor_id, NULL)) AS previous_period_visitors', $previous_period_array)->selectRaw('COUNT(DISTINCT IF(views.viewed_at BETWEEN ? AND ?, sessions.session_id, NULL)) AS sessions', $current_period_array)->selectRaw('COUNT(DISTINCT IF(views.viewed_at BETWEEN ? AND ? AND sessions.final_view_id IS NULL, sessions.session_id, NULL)) AS bounces', $current_period_array)->selectRaw('IF(resources.singular_id IS NOT NULL, COUNT(DISTINCT IF(comments.comment_date_gmt BETWEEN ? AND ? AND comment_approved = "1", comments.comment_ID, null)), NULL) as comments', $current_period_array)->selectRaw('AVG(IF(views.viewed_at BETWEEN ? AND ?, TIMESTAMPDIFF(SECOND, views.viewed_at, views.next_viewed_at), NULL)) AS average_view_duration', $current_period_array)->selectRaw('COUNT(DISTINCT IF(views.viewed_at BETWEEN ? AND ? AND resources.id = initial_view.resource_id, sessions.session_id, NULL)) AS entrances', $current_period_array)->selectRaw('COUNT(DISTINCT IF(views.viewed_at BETWEEN ? AND ? AND (resources.id = final_view.resource_id OR (resources.id = initial_view.resource_id AND sessions.final_view_id IS NULL)), sessions.session_id, NULL)) AS exits', $current_period_array)->from($views_table, 'views')->leftJoin($pages_query->raw($sessions_table . ' AS sessions'), function (JoinClause $join) { |
| 55 | $join->on('views.session_id', '=', 'sessions.session_id'); |
| 56 | })->leftJoin($pages_query->raw($resources_table . ' AS resources'), function (JoinClause $join) { |
| 57 | $join->on('views.resource_id', '=', 'resources.id'); |
| 58 | })->leftJoin($pages_query->raw($views_table . ' AS initial_view'), function (JoinClause $join) { |
| 59 | $join->on('sessions.initial_view_id', '=', 'initial_view.id'); |
| 60 | })->leftJoin($pages_query->raw($views_table . ' AS final_view'), function (JoinClause $join) { |
| 61 | $join->on('sessions.final_view_id', '=', 'final_view.id'); |
| 62 | })->leftJoin($pages_query->raw($comments_table . ' AS comments'), function (JoinClause $join) { |
| 63 | $join->on('resources.singular_id', '=', 'comments.comment_post_ID'); |
| 64 | })->whereBetween('views.viewed_at', $total_period_array)->whereBetween('sessions.created_at', $total_period_array)->where(function (Builder $query) use($total_period_array) { |
| 65 | $query->whereNull('sessions.ended_at')->orWhereBetween('sessions.ended_at', $total_period_array); |
| 66 | })->where(function (Builder $query) use($total_period_array) { |
| 67 | $query->whereNull('initial_view.viewed_at')->orWhereBetween('initial_view.viewed_at', $total_period_array); |
| 68 | })->where(function (Builder $query) use($total_period_array) { |
| 69 | $query->whereNull('final_view.viewed_at')->orWhereBetween('final_view.viewed_at', $total_period_array); |
| 70 | })->when(\count($this->filters) > 0, function (Builder $query) use($calculated_columns) { |
| 71 | foreach ($this->filters as $filter) { |
| 72 | if (\in_array($filter['column'], $calculated_columns)) { |
| 73 | continue; |
| 74 | } |
| 75 | $filter = new Filter($filter); |
| 76 | $method = $filter->method(); |
| 77 | $query->{$method}($filter->column(), $filter->operator(), $filter->value()); |
| 78 | } |
| 79 | })->groupBy('resources.id')->having('views', '>', 0)->when(!$has_calculate_column_filter, function (Builder $query) use($sort_column) { |
| 80 | $query->when($this->sort_configuration->is_nullable(), function (Builder $query) use($sort_column) { |
| 81 | $query->orderByRaw("CASE WHEN {$sort_column} IS NULL THEN 1 ELSE 0 END"); |
| 82 | })->orderBy($sort_column, $this->sort_configuration->direction())->orderBy('cached_title')->when(\is_int($this->number_of_rows), function (Builder $query) { |
| 83 | $query->limit($this->number_of_rows); |
| 84 | }); |
| 85 | }); |
| 86 | $outer_query = Illuminate_Builder::get_builder(); |
| 87 | $outer_query->selectRaw('pages.*')->selectRaw('IFNULL((views - previous_period_views) / previous_period_views * 100, 0) AS views_growth')->selectRaw('IFNULL((visitors - previous_period_visitors) / previous_period_visitors * 100, 0) AS visitors_growth')->selectRaw('IFNULL(bounces / sessions * 100, 0) AS bounce_rate')->selectRaw('IFNULL((exits / views) * 100, 0) AS exit_percent')->when(\count($this->filters) > 0, function (Builder $query) use($calculated_columns) { |
| 88 | foreach ($this->filters as $filter) { |
| 89 | if (!\in_array($filter['column'], $calculated_columns)) { |
| 90 | continue; |
| 91 | } |
| 92 | $filter = new Filter($filter); |
| 93 | $method = $filter->method(); |
| 94 | $query->{$method}($filter->column(), $filter->operator(), $filter->value()); |
| 95 | } |
| 96 | })->fromSub($pages_query, 'pages')->when($has_calculate_column_filter, function (Builder $query) use($sort_column) { |
| 97 | $query->when($this->sort_configuration->is_nullable(), function (Builder $query) use($sort_column) { |
| 98 | $query->orderByRaw("CASE WHEN {$sort_column} IS NULL THEN 1 ELSE 0 END"); |
| 99 | })->orderBy($sort_column, $this->sort_configuration->direction())->orderBy('cached_title')->when(\is_int($this->number_of_rows), function (Builder $query) { |
| 100 | $query->limit($this->number_of_rows); |
| 101 | }); |
| 102 | }); |
| 103 | return $outer_query; |
| 104 | } |
| 105 | } |
| 106 |