PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.5
Independent Analytics – WordPress Analytics Plugin v1.5
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / inc / queries / views.php
independent-analytics / inc / queries Last commit date
range_query.php 4 years ago referrers.php 4 years ago resources.php 4 years ago views.php 4 years ago
views.php
140 lines
1 <?php
2
3 namespace IAWP;
4
5 class Views extends Range_Query
6 {
7 const REFERRERS = 'REFERRERS';
8 const RESOURCES = 'RESOURCES';
9 private $views;
10 private $prev_period_views;
11 private $daily_views;
12
13 public function __construct($type, $ids = null, $start = null, $end = null)
14 {
15 parent::__construct($start, $end);
16 if ($type == self::RESOURCES || $type == self::REFERRERS) {
17 $this->query($type, $ids);
18 } else {
19 throw new \Exception('IAWP_Views: Unsupported type');
20 }
21 }
22
23 public function views()
24 {
25 return $this->views;
26 }
27
28 public function prev_period_views()
29 {
30 return $this->prev_period_views;
31 }
32
33 public function daily_views()
34 {
35 return $this->daily_views;
36 }
37
38 public function views_percentage_growth()
39 {
40 if ($this->views() == 0 || $this->prev_period_views() == 0) {
41 return 0;
42 } else {
43 return round((($this->views() / $this->prev_period_views()) - 1) * 100);
44 }
45 }
46
47 private function query($type, $ids)
48 {
49 global $wpdb;
50 $views_table = DB::views_table();
51
52 if (is_null($ids)) {
53 $skip_in = 1;
54 $ids = [0];
55 $in = '%s';
56 } elseif (count($ids) == 0) {
57 $skip_in = 0;
58 $ids = [0];
59 $in = '%s';
60 } else {
61 $skip_in = 0;
62 $in = implode(',', array_fill(0, count($ids), '%s'));
63 }
64
65 $column = $type == self::RESOURCES ? 'resource_id' : 'referrer_id';
66 $has_null_id = false;
67 foreach ($ids as $id) {
68 if (is_null($id)) {
69 $has_null_id = true;
70 break;
71 }
72 }
73 $null_check = '';
74
75 if ($type == self::REFERRERS && $has_null_id) {
76 $null_check = "OR $column IS NULL";
77 }
78
79 $utc = Timezone::utc_offset();
80 $local_offset = Timezone::local_offset();
81 $query = $wpdb->prepare(
82 "SELECT DATE(CONVERT_TZ(viewed_at, '$utc', '$local_offset')) as date, count(*) as views FROM $views_table WHERE viewed_at BETWEEN %s AND %s AND (%d = 1 OR $column IN ($in) $null_check) GROUP BY DATE(CONVERT_TZ(viewed_at, '$utc', '$local_offset'))",
83 $this->formatted_start(),
84 $this->formatted_end(),
85 $skip_in,
86 ...$ids
87 );
88 $rows = $wpdb->get_results($query);
89
90 $prev_period_query = $wpdb->prepare(
91 "SELECT count(*) as views FROM $views_table WHERE viewed_at BETWEEN %s AND %s AND (%d = 1 OR $column IN ($in))",
92 $this->prev_period_formatted_start(),
93 $this->prev_period_formatted_end(),
94 $skip_in,
95 ...$ids
96 );
97 $prev_period_views = intval($wpdb->get_var($prev_period_query));
98
99 $views = 0;
100 $daily_views = [];
101
102 foreach ($rows as $row) {
103 $views += $row->views;
104 $daily_views[] = [$row->date, intval($row->views)];
105 }
106
107 $this->views = $views;
108 $this->prev_period_views = $prev_period_views;
109 $this->daily_views = $this->fill_in_data_range($daily_views);
110 }
111
112 private function fill_in_data_range($partial_data)
113 {
114 $interval = new \DateInterval('P1D');
115 $start = clone $this->start();
116 $end = clone $this->end();
117 $user_timezone = new \DateTimeZone(Timezone::local_offset());
118 $start->setTimezone($user_timezone);
119 $end->setTimezone($user_timezone);
120
121 $date_range = new \DatePeriod($start, $interval, $end);
122 $new_data = [];
123
124 foreach ($date_range as $date) {
125 $views = 0;
126
127 foreach ($partial_data as $data_point) {
128 if ($date == (new \DateTime($data_point[0], $user_timezone))) {
129 $views = intval($data_point[1]);
130 break;
131 }
132 }
133
134 $new_data[] = [$date, $views];
135 }
136
137 return $new_data;
138 }
139 }
140