PluginProbe
Pixelavo – Server Side Tracking & Pixel + AI Ads Tools / trunk
Pixelavo – Server Side Tracking & Pixel + AI Ads Tools vtrunk
1.5.5 1.5.4 trunk 1.0.0 1.0.1 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 All 47 releases
pixelavo / includes / pixel-events-data.php

pixel-events-data.php in Pixelavo – Server Side Tracking & Pixel + AI Ads Tools trunk, at includes/pixel-events-data.php

244 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Pixelavo;
4
5 /* Exit if accessed directly */
6 if (!defined('ABSPATH')) {
7 exit;
8 }
9
10 class PixelEventsData{
11
12 private static $_instance = null;
13
14 /**
15 * Instance.
16 * Initializes a singleton instance.
17 * @return self class
18 */
19 static function instance() {
20 if ( is_null( self::$_instance ) ) {
21 self::$_instance = new self();
22 }
23 return self::$_instance;
24 }
25
26 function __construct() {
27 if(!pixelavo_check_exclude_roles()) {
28 /** Search */
29 if(pixelavo_pixel_event_exists('search')) {
30 add_action('wp_footer', [$this, 'search_data']);
31 }
32 /** View Category */
33 if(pixelavo_pixel_event_exists('view_category')) {
34 add_action('wp_footer', [$this, 'view_category_data']);
35 }
36 /** View Content */
37 if(pixelavo_pixel_event_exists('view_content')) {
38 add_action('wp_footer', [$this, 'view_content_data']);
39 }
40 /** Remove From Cart */
41 if(pixelavo_pixel_event_exists('remove_from_cart')) {
42 add_action('wp_ajax_pixelavo_ajax_remove_from_cart', [$this, 'pixelavo_ajax_remove_from_cart']);
43 add_action('wp_ajax_nopriv_pixelavo_ajax_remove_from_cart', [$this, 'pixelavo_ajax_remove_from_cart']);
44 }
45 }
46 }
47
48 /**
49 * Search data.
50 * Run when user search for any product and go to `search` page.
51 * @return void
52 */
53 function search_data() {
54 if(pixelavo_pixel_status() && function_exists('is_search') && is_search()) {
55 global $pixelavoEventsLocalizedData;
56
57 $search_query = get_search_query();
58 $args = [
59 's' => $search_query,
60 'status' => 'publish',
61 'visibility' => 'visible'
62 ];
63 $products = wc_get_products($args);
64
65 $ids = [];
66 $category = [];
67 $values = 0;
68 $event_id = 'Search.'.time();
69
70 foreach ($products as $product) {
71 $ids[] = $product->get_id();
72 // Safely get product categories with validation
73 $terms = get_the_terms($product->get_id(), 'product_cat');
74 if (is_array($terms) && !is_wp_error($terms)) {
75 foreach ($terms as $cat) {
76 if(!in_array($cat->name, $category)) {
77 $category[] = $cat->name;
78 }
79 }
80 }
81 $values = $values + floatval($product->get_price());
82 }
83
84 $data = [
85 'content_category' => implode(', ', $category),
86 'content_ids' => $ids,
87 'currency' => get_woocommerce_currency(),
88 'search_string' => $search_query,
89 'content_type' => 'product',
90 'value' => round($values, 2) // Use round() instead of number_format() to return numeric value
91 ];
92
93 $data = array_merge($data, apply_filters('pixelavo_additional_user_info', [], $data['content_ids'] ), pixelavo_get_event_common_data());
94
95 if(!empty($data['content_ids'])) {
96 $pixelavoEventsLocalizedData[] = ['track' => 'track', 'event' => 'Search', 'data' => $data, 'eventID' => $event_id];
97 pixelavo_run_conversions_api('Search', $data, $event_id);
98 }
99 }
100 }
101
102 /**
103 * View Category data.
104 * Run when user to go `product category` page.
105 * @return void
106 */
107 function view_category_data() {
108 if(pixelavo_pixel_status() && function_exists('is_product_category') && is_product_category()) {
109 global $pixelavoEventsLocalizedData;
110
111 $category_slug = get_queried_object()->slug;
112 $category_name = get_queried_object()->name;
113 $args = array(
114 'status' => 'publish',
115 'visibility' => 'visible',
116 'category' => [$category_slug]
117 );
118 $products = wc_get_products($args);
119
120 $ids = [];
121 $titles = [];
122 $contents = [];
123 $values = 0;
124 $event_id = 'ViewCategory.'.strtolower($category_name).'.'.time();
125
126 foreach ($products as $product) {
127 $ids[] = $product->get_id();
128 $titles[] = $product->get_title();
129 $contents[] = ['id' => $product->get_id(), 'quantity' => 1];
130 $values = $values + floatval($product->get_price());
131 }
132
133 $data = [
134 'content_ids' => $ids,
135 'content_category' => $category_name,
136 'content_name' => implode(', ', $titles),
137 'content_type' => 'product',
138 'contents' => $contents,
139 'currency' => get_woocommerce_currency(),
140 'value' => round($values, 2) // Use round() instead of number_format() to return numeric value
141 ];
142
143 $data = array_merge($data, apply_filters('pixelavo_additional_user_info', [], $data['content_ids'] ), pixelavo_get_event_common_data());
144
145 if(!empty($data['content_ids'])) {
146 $pixelavoEventsLocalizedData[] = ['track' => 'trackCustom', 'event' => 'ViewCategory', 'data' => $data, 'eventID' => $event_id];
147 pixelavo_run_conversions_api('ViewCategory', $data, $event_id);
148 }
149 }
150 }
151
152 /**
153 * View Content data.
154 * Run when user to go `single product` page.
155 * @return void
156 */
157 function view_content_data() {
158 if(pixelavo_pixel_status() && function_exists('is_product') && is_product()) {
159 global $pixelavoEventsLocalizedData;
160
161 $product = wc_get_product(get_the_ID());
162 $category = [];
163 $event_id = 'ViewContent.'.get_the_ID().'.'.time();
164
165 if ( $product ) {
166 // Safely get product categories with validation
167 $terms = get_the_terms($product->get_id(), 'product_cat');
168 if (is_array($terms) && !is_wp_error($terms)) {
169 foreach ($terms as $cat) {
170 if(!in_array($cat->name, $category)) {
171 $category[] = $cat->name;
172 }
173 }
174 }
175 $content_type = $product->get_type() === 'variable' ? 'product_group' : 'product';
176 $data = [
177 'content_ids' => [$product->get_id()],
178 'content_category' => implode(', ', $category),
179 'content_name' => $product->get_title(),
180 'content_type' => $content_type,
181 'contents' => [['id' => $product->get_id(), 'quantity' => 1]], // Add contents for better match quality
182 'currency' => get_woocommerce_currency(),
183 'value' => round(wc_get_price_to_display($product), 2) // Ensure numeric value
184 ];
185
186 $data = array_merge($data, apply_filters('pixelavo_additional_user_info', [], $data['content_ids'] ), pixelavo_get_event_common_data());
187
188
189 if(!empty($data['content_ids'])) {
190 $view_content_event_settings = pixelavo_get_option('view_content', 'pixelavo_wc_view_content');
191 if(isset($view_content_event_settings['view_content_delay']) && (int) $view_content_event_settings['view_content_delay'] > 0) {
192 $pixelavoEventsLocalizedData[] = ['track' => 'track', 'event' => 'ViewContent', 'data' => $data, 'eventID' => $event_id, 'isEventDelay' => true, 'eventDelay' => $view_content_event_settings['view_content_delay']];
193 } else {
194 $pixelavoEventsLocalizedData[] = ['track' => 'track', 'event' => 'ViewContent', 'data' => $data, 'eventID' => $event_id];
195 pixelavo_run_conversions_api('ViewContent', $data, $event_id);
196 }
197 }
198 }
199 }
200 }
201
202 /**
203 * Remove From Cart ajax data.
204 * @return void
205 */
206 function pixelavo_ajax_remove_from_cart() {
207 check_ajax_referer('pixelavo_ajax_event_nonce', 'nonce');
208 if(pixelavo_pixel_status()) {
209 $product_id = !empty($_POST['product_id']) ? intval(wp_unslash($_POST['product_id'])) : '';
210 $quantity = !empty($_POST['product_quantity']) ? intval(wp_unslash($_POST['product_quantity'])) : '';
211 $product = wc_get_product($product_id);
212 $common_params = !empty($_POST['common_params']) ? array_map('sanitize_text_field', (array)wp_unslash($_POST['common_params'])) : [];
213 $event_id = 'RemoveFromCart.'.$product_id.'.'.time();
214 if ( $product ) {
215 $content_type = $product->get_type() === 'variable' ? 'product_group' : 'product';
216 $data = [
217 'content_ids' => [$product->get_id()],
218 'content_name' => $product->get_title(),
219 'content_type' => $content_type,
220 'contents' => [
221 [
222 'id'=> $product->get_id(),
223 'quantity'=> $quantity
224 ]
225 ],
226 'currency' => get_woocommerce_currency(),
227 'value' => round(wc_get_price_to_display( $product ), 2) // Ensure numeric value
228 ];
229
230 $data = array_merge($data, apply_filters('pixelavo_additional_user_info', [], $data['content_ids'] ), $common_params);
231
232 pixelavo_run_conversions_api('RemoveFromCart', $data, $event_id);
233 wp_send_json_success( ["data" => $data, "event_id" => $event_id] );
234 }
235 wp_die();
236 }
237 }
238 }
239
240 /**
241 * Initialize PixelEventsData Class
242 */
243 PixelEventsData::instance();
244