PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.14.2
Independent Analytics – WordPress Analytics Plugin v2.14.2
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 / IAWP / Overview / Modules / Recent_Conversions_Module.php
independent-analytics / IAWP / Overview / Modules Last commit date
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
Recent_Conversions_Module.php
85 lines
1 <?php
2
3 namespace IAWP\Overview\Modules;
4
5 use IAWPSCOPED\Carbon\CarbonImmutable;
6 use IAWP\Icon_Directory_Factory;
7 use IAWP\Illuminate_Builder;
8 use IAWP\Tables;
9 use IAWP\Utils\Currency;
10 use IAWP\Utils\Format;
11 use IAWP\Utils\Timezone;
12 /** @internal */
13 class Recent_Conversions_Module extends \IAWP\Overview\Modules\Module
14 {
15 public function module_type() : string
16 {
17 return 'recent-conversions';
18 }
19 public function module_name() : string
20 {
21 return \__('Recent Conversions', 'independent-analytics');
22 }
23 public function subtitle() : string
24 {
25 return \__('Most Recent', 'independent-analytics');
26 }
27 public function calculate_dataset()
28 {
29 $tables = Tables::class;
30 // Get recent clicks
31 $recent_clicks_query = Illuminate_Builder::new()->select(['clicks.view_id', 'clicks.created_at', 'link_rules.name'])->selectRaw("'click' as conversion_type")->from($tables::clicks(), 'clicks')->join("{$tables::clicked_links()} AS clicked_links", 'clicks.click_id', '=', 'clicked_links.click_id')->join("{$tables::links()} AS links", 'clicked_links.link_id', '=', 'links.id')->join("{$tables::link_rules()} AS link_rules", 'links.link_rule_id', '=', 'link_rules.link_rule_id');
32 // Get form submission conversions
33 $recent_submissions_query = Illuminate_Builder::new()->select(['form_submissions.view_id', 'form_submissions.created_at'])->selectRaw("IF(forms.cached_form_title = '', '(Unnamed form)', forms.cached_form_title) AS name")->selectRaw("'form_submission' as conversion_type")->from($tables::form_submissions(), 'form_submissions')->join("{$tables::forms()} AS forms", "form_submissions.form_id", '=', "forms.form_id");
34 // Get order conversions
35 $recent_orders_query = Illuminate_Builder::new()->select(['orders.view_id', 'orders.created_at', 'orders.total AS name'])->selectRaw("'order' as conversion_type")->from($tables::orders(), 'orders')->where('orders.is_included_in_analytics', '=', \true);
36 // Create a single conversion query for all conversion types
37 $conversion_query = $recent_clicks_query->unionAll($recent_orders_query)->unionAll($recent_submissions_query);
38 $query = Illuminate_Builder::new()->select(['conversions.view_id', 'conversions.created_at', 'conversions.conversion_type', 'conversions.name', 'countries.country_code', 'countries.country', 'device_types.device_type', 'device_browsers.device_browser AS browser'])->fromSub($conversion_query, 'conversions')->leftJoin("{$tables::views()} as views", 'conversions.view_id', '=', 'views.id')->leftJoin("{$tables::resources()} as resources", 'views.resource_id', '=', 'resources.id')->leftJoin("{$tables::sessions()} as sessions", 'views.session_id', '=', 'sessions.session_id')->leftJoin("{$tables::countries()} as countries", 'sessions.country_id', '=', 'countries.country_id')->leftJoin("{$tables::device_types()} as device_types", 'sessions.device_type_id', '=', 'device_types.device_type_id')->leftJoin("{$tables::device_browsers()} as device_browsers", 'sessions.device_browser_id', '=', 'device_browsers.device_browser_id')->whereIn('conversions.conversion_type', $this->attributes['recent_conversion_types'] ?? ['order', 'form_submission', 'click'])->orderByDesc('conversions.created_at')->limit(40);
39 return $query->get()->map(function ($row) {
40 $date = CarbonImmutable::parse($row->created_at, 'utc')->setTimezone(Timezone::site_timezone());
41 $long_date_string = $date->format(Format::date_time());
42 if ($date->isToday()) {
43 $short_date_string = $date->format(Format::time());
44 } elseif ($date->isYesterday()) {
45 $short_date_string = \__('Yesterday', 'independent-analytics');
46 } else {
47 $short_date_string = $date->startOfDay()->diffForHumans();
48 }
49 if ($row->conversion_type == 'order') {
50 $name = Currency::format($row->name, \false);
51 } else {
52 $name = $row->name;
53 }
54 return ['viewed_at' => $short_date_string, 'viewed_at_the_long_way' => $long_date_string, 'name' => $name, 'country_code' => $row->country_code, 'country' => $row->country ?? \__('Unknown Country', 'independent-analytics'), 'device_type' => $row->device_type ?? \__('Unknown Device Type', 'independent-analytics'), 'browser' => $row->browser ?? \__('Unknown Browser', 'independent-analytics'), 'conversion_type' => $row->conversion_type, 'conversion_label' => $this->get_conversion_label($row->conversion_type)];
55 })->all();
56 }
57 public function add_icons_to_dataset(array $dataset) : array
58 {
59 $flags = Icon_Directory_Factory::flags();
60 $device_types = Icon_Directory_Factory::device_types();
61 $browsers = Icon_Directory_Factory::browsers();
62 return \array_map(function (array $row) use($flags, $device_types, $browsers) {
63 $row['flag'] = $flags->find($row['country_code'] ?? '');
64 $row['device_type_icon'] = $device_types->find($row['device_type'] ?? '');
65 $row['browser_icon'] = $browsers->find($row['browser'] ?? '');
66 return $row;
67 }, $dataset);
68 }
69 protected function module_fields() : array
70 {
71 return ['recent_conversion_types'];
72 }
73 private function get_conversion_label(string $conversion_type) : string
74 {
75 switch ($conversion_type) {
76 case 'order':
77 return \__('Order', 'independent-analytics');
78 case 'form_submission':
79 return \__('Form', 'independent-analytics');
80 default:
81 return \__('Click', 'independent-analytics');
82 }
83 }
84 }
85