inc/analytics
class-merchant-analytics-data-ajax.php
12.5 KB
1 month ago
class-merchant-analytics-data-hooks.php
1.5 KB
1 month ago
class-merchant-analytics-data-provider.php
21.0 KB
1 month ago
class-merchant-analytics-data-reports.php
33.6 KB
1 month ago
class-merchant-analytics-db-orm.php
15.5 KB
1 month ago
class-merchant-analytics-logger.php
4.7 KB
1 month ago
class-merchant-analytics-logger.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.1, at inc/analytics/class-merchant-analytics-logger.php
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly. |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Class Merchant_Analytics |
| 10 | * |
| 11 | * This class is responsible for logging analytics events. |
| 12 | */ |
| 13 | class Merchant_Analytics_Logger { |
| 14 | |
| 15 | /** |
| 16 | * @var Merchant_Analytics_DB_ORM |
| 17 | */ |
| 18 | private $database; |
| 19 | |
| 20 | /** |
| 21 | * @var mixed |
| 22 | */ |
| 23 | private $user_id = null; |
| 24 | |
| 25 | public function __construct() { |
| 26 | $this->database = new Merchant_Analytics_DB_ORM(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Set the user ID. |
| 31 | * |
| 32 | * @param $user_id |
| 33 | */ |
| 34 | public function set_user_id( $user_id ) { |
| 35 | $this->user_id = $user_id; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Log an event. |
| 40 | * |
| 41 | * @param $args array The arguments for the event to be logged. |
| 42 | * |
| 43 | * @return false|int The ID of the inserted row on success, false on failure. |
| 44 | */ |
| 45 | public function log_event( $args ) { |
| 46 | if ( |
| 47 | /** |
| 48 | * Filter the analytics toggle. |
| 49 | * |
| 50 | * @param bool $analytics_toggle The analytics toggle. |
| 51 | * |
| 52 | * @since 2.0.1 |
| 53 | */ |
| 54 | ! apply_filters( |
| 55 | 'merchant_analytics_toggle', |
| 56 | Merchant_Admin_Options::get( 'global-settings', 'analytics_toggle', true ) |
| 57 | ) |
| 58 | ) { |
| 59 | return false; |
| 60 | } |
| 61 | if ( ! $this->is_db_table_exists() ) { |
| 62 | return false; |
| 63 | } |
| 64 | $defaults = array( |
| 65 | 'source_product_id' => 0, |
| 66 | 'event_type' => '', |
| 67 | 'customer_id' => $this->user_id, |
| 68 | 'related_event_id' => '', |
| 69 | 'module_id' => '', |
| 70 | 'campaign_id' => 0, |
| 71 | 'campaign_cost' => 0, |
| 72 | 'order_id' => 0, |
| 73 | 'order_subtotal' => 0, |
| 74 | 'meta_data' => '', |
| 75 | ); |
| 76 | |
| 77 | $args = wp_parse_args( $args, $defaults ); |
| 78 | |
| 79 | /** |
| 80 | * Filter the arguments for the event to be logged. |
| 81 | * |
| 82 | * @param array $args The arguments for the event to be logged. |
| 83 | * |
| 84 | * @since 2.0 |
| 85 | */ |
| 86 | $args = apply_filters( |
| 87 | 'merchant_analytics_log_event_args', |
| 88 | $args |
| 89 | ); |
| 90 | |
| 91 | return $this->database->create( $args ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Check if there is an impression event for a module and product in the last hour. |
| 96 | * |
| 97 | * @param $module_id string The module ID. |
| 98 | * @param $product_id int The product ID. |
| 99 | * @param $extra_attrs array Extra attributes to search for. |
| 100 | * |
| 101 | * @return array|null |
| 102 | */ |
| 103 | public function get_product_module_last_impression( $module_id, $product_id, $extra_attrs = array() ) { |
| 104 | if ( ! $this->is_db_table_exists() ) { |
| 105 | return null; |
| 106 | } |
| 107 | $now = current_time( 'mysql' ); |
| 108 | $last_hour = gmdate( 'Y-m-d H:i:s', strtotime( '-1 hour', strtotime( $now ) ) ); |
| 109 | |
| 110 | $args = array( |
| 111 | 'event_type' => 'impression', |
| 112 | 'source_product_id' => $product_id, |
| 113 | 'customer_id' => $this->user_id, |
| 114 | 'module_id' => $module_id, |
| 115 | ); |
| 116 | |
| 117 | if ( ! empty( $extra_attrs ) ) { |
| 118 | $args = array_merge( $args, $extra_attrs ); |
| 119 | } |
| 120 | |
| 121 | $result = $this->database |
| 122 | ->where_between_dates( $last_hour, $now ) |
| 123 | ->where( $args ) |
| 124 | ->first(); |
| 125 | |
| 126 | $this->database->reset_query(); |
| 127 | |
| 128 | return $result; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get event data |
| 133 | * |
| 134 | * @param $event_id int The event ID. |
| 135 | * |
| 136 | * @return ARRAY|NULL The event data. |
| 137 | */ |
| 138 | public function get_event( $event_id ) { |
| 139 | if ( ! $this->is_db_table_exists() ) { |
| 140 | return null; |
| 141 | } |
| 142 | $result = $this->database->where( 'id = %d', $event_id )->first(); |
| 143 | $this->database->reset_query(); |
| 144 | |
| 145 | return $result; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get events by attributes. |
| 150 | * |
| 151 | * @param $attributes array The attributes to search for. |
| 152 | * |
| 153 | * @return array|null The events found. |
| 154 | */ |
| 155 | public function get_event_by( $attributes ) { |
| 156 | if ( ! $this->is_db_table_exists() ) { |
| 157 | return null; |
| 158 | } |
| 159 | $result = $this->database->where( $attributes )->first(); |
| 160 | $this->database->reset_query(); |
| 161 | |
| 162 | return $result; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Check if an event exists. |
| 167 | * |
| 168 | * @param $event_id int The event ID. |
| 169 | * |
| 170 | * @return bool True if the event exists, false otherwise. |
| 171 | */ |
| 172 | public function event_exists( $event_id ) { |
| 173 | if ( ! $this->is_db_table_exists() ) { |
| 174 | return false; |
| 175 | } |
| 176 | $result = ! empty( $this->database->where( 'id = %d', $event_id )->first() ); |
| 177 | $this->database->reset_query(); |
| 178 | |
| 179 | return $result; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Update an event. |
| 184 | * |
| 185 | * @param $event_id int The event ID. |
| 186 | * @param $data array The data to update. |
| 187 | * |
| 188 | * @return false|int The ID of the updated row on success, false on failure. |
| 189 | */ |
| 190 | public function update_event( $event_id, $data ) { |
| 191 | if ( ! $this->is_db_table_exists() ) { |
| 192 | return false; |
| 193 | } |
| 194 | $result = $this->database->update( $event_id, $data ); |
| 195 | $this->database->reset_query(); |
| 196 | |
| 197 | return $result; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Delete an event. |
| 202 | * |
| 203 | * @param $event_id int The event ID. |
| 204 | * |
| 205 | * @return bool True if the event was deleted, false otherwise. |
| 206 | */ |
| 207 | public function delete_event( $event_id ) { |
| 208 | if ( ! $this->is_db_table_exists() ) { |
| 209 | return false; |
| 210 | } |
| 211 | $result = $this->database->delete( $event_id ); |
| 212 | $this->database->reset_query(); |
| 213 | |
| 214 | return $result; |
| 215 | } |
| 216 | |
| 217 | private function is_db_table_exists() { |
| 218 | return $this->database->table_exists(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 |