PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.12.2
Independent Analytics – WordPress Analytics Plugin v2.12.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 1 year ago Busiest_Time_Of_Day_Module.php 1 year ago Line_Chart_Module.php 1 year ago Map_Module.php 1 year ago Module.php 11 months ago New_Sessions_Module.php 1 year ago Pie_Chart_Module.php 1 year ago Quick_Stats_Module.php 1 year ago Recent_Conversions_Module.php 1 year ago Recent_Views_Module.php 1 year ago Top_Ten_Module.php 1 year ago
Recent_Conversions_Module.php
86 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\Timezone;
11 use IAWP\Utils\WordPress_Site_Date_Format_Pattern;
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::link_rules()} AS link_rules", "clicked_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)->setTimezone(Timezone::site_timezone());
41 $time_format = \IAWPSCOPED\iawp()->get_option('time_format', 'g:i a');
42 $long_date_string = $date->format(WordPress_Site_Date_Format_Pattern::for_php() . ' ' . $time_format);
43 if ($date->isToday()) {
44 $short_date_string = $date->format($time_format);
45 } elseif ($date->isYesterday()) {
46 $short_date_string = \__('Yesterday', 'independent-analytics');
47 } else {
48 $short_date_string = $date->startOfDay()->diffForHumans();
49 }
50 if ($row->conversion_type == 'order') {
51 $name = Currency::format($row->name, \false);
52 } else {
53 $name = $row->name;
54 }
55 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)];
56 })->all();
57 }
58 public function add_icons_to_dataset(array $dataset) : array
59 {
60 $flags = Icon_Directory_Factory::flags();
61 $device_types = Icon_Directory_Factory::device_types();
62 $browsers = Icon_Directory_Factory::browsers();
63 return \array_map(function (array $row) use($flags, $device_types, $browsers) {
64 $row['flag'] = $flags->find($row['country_code'] ?? '');
65 $row['device_type_icon'] = $device_types->find($row['device_type'] ?? '');
66 $row['browser_icon'] = $browsers->find($row['browser'] ?? '');
67 return $row;
68 }, $dataset);
69 }
70 protected function module_fields() : array
71 {
72 return ['recent_conversion_types'];
73 }
74 private function get_conversion_label(string $conversion_type) : string
75 {
76 switch ($conversion_type) {
77 case 'order':
78 return \__('Order', 'independent-analytics');
79 case 'form_submission':
80 return \__('Form', 'independent-analytics');
81 default:
82 return \__('Click', 'independent-analytics');
83 }
84 }
85 }
86