PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.3.1
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.3.1
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Abilities / Read / GetAnalytics.php

GetAnalytics.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.3.1, at includes/Abilities/Read/GetAnalytics.php

99 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Ability: read NotificationX analytics (views / clicks / CTR).
4 *
5 * @package NotificationX\Abilities\Read
6 */
7
8 namespace NotificationX\Abilities\Read;
9
10 use NotificationX\Abilities\AbilityBase;
11 use NotificationX\Core\PostType;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Returns impression/click analytics for one notification or the whole site.
19 */
20 class GetAnalytics extends AbilityBase {
21
22 protected $id = 'notificationx/get-analytics';
23 protected $label = 'Get analytics';
24 protected $description = 'Get NotificationX analytics: total views (impressions), clicks and click-through rate. Pass an nx_id for one notification, or omit it for a site-wide total plus a per-notification breakdown.';
25
26 public function input_schema() {
27 return array(
28 'type' => 'object',
29 'properties' => array(
30 'nx_id' => array(
31 'type' => 'integer',
32 'description' => 'Optional notification id. Omit for site-wide analytics.',
33 ),
34 ),
35 );
36 }
37
38 public function output_schema() {
39 return array(
40 'type' => 'object',
41 'properties' => array(
42 'totals' => array( 'type' => 'object' ),
43 'items' => array( 'type' => 'array' ),
44 ),
45 );
46 }
47
48 public function execute( $input ) {
49 $wheres = array();
50 if ( ! empty( $input['nx_id'] ) ) {
51 $wheres['a.nx_id'] = (int) $input['nx_id'];
52 }
53
54 $posts = PostType::get_instance()->get_post_with_analytics( $wheres );
55 if ( ! is_array( $posts ) ) {
56 $posts = array();
57 }
58
59 if ( ! empty( $input['nx_id'] ) && empty( $posts ) ) {
60 return new \WP_Error(
61 'nx_mcp_not_found',
62 /* translators: %d: notification id. */
63 sprintf( __( 'No notification found with id %d.', 'notificationx' ), (int) $input['nx_id'] ),
64 array( 'status' => 404 )
65 );
66 }
67
68 $items = array();
69 $total_views = 0;
70 $total_clicks = 0;
71
72 foreach ( $posts as $post ) {
73 $views = isset( $post['views'] ) ? (int) $post['views'] : 0;
74 $clicks = isset( $post['clicks'] ) ? (int) $post['clicks'] : 0;
75
76 $total_views += $views;
77 $total_clicks += $clicks;
78
79 $items[] = array(
80 'nx_id' => isset( $post['nx_id'] ) ? (int) $post['nx_id'] : 0,
81 'title' => isset( $post['title'] ) ? $post['title'] : '',
82 'source' => isset( $post['source'] ) ? $post['source'] : '',
83 'views' => $views,
84 'clicks' => $clicks,
85 'ctr' => $views > 0 ? round( ( $clicks / $views ) * 100, 2 ) : 0,
86 );
87 }
88
89 return array(
90 'totals' => array(
91 'views' => $total_views,
92 'clicks' => $total_clicks,
93 'ctr' => $total_views > 0 ? round( ( $total_clicks / $total_views ) * 100, 2 ) : 0,
94 ),
95 'items' => $items,
96 );
97 }
98 }
99