PluginProbe
Pixelavo – Server Side Tracking & Pixel + AI Ads Tools / 1.2.2
Pixelavo – Server Side Tracking & Pixel + AI Ads Tools v1.2.2
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 1.2.2, at includes/pixel-events-data.php

191 lines 6.9 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 }
41 }
42
43 /**
44 * Search data.
45 * Run when user search for any product and go to `search` page.
46 * @return void
47 */
48 function search_data() {
49 if(pixelavo_pixel_status() && function_exists('is_search') && is_search()) {
50 global $pixelavoEventsLocalizedData;
51
52 $search_query = get_search_query();
53 $args = [
54 's' => $search_query,
55 'status' => 'publish',
56 'visibility' => 'visible'
57 ];
58 $products = wc_get_products($args);
59
60 $ids = [];
61 $category = [];
62 $values = 0;
63 $event_id = 'Search.'.time();
64
65 foreach ($products as $product) {
66 $ids[] = $product->get_id();
67 foreach (get_the_terms($product->get_id(), 'product_cat') as $cat) {
68 if(!in_array($cat->name, $category)) {
69 $category[] = $cat->name;
70 }
71 }
72 $values = $values + floatval($product->get_price());
73 }
74
75 $data = [
76 'content_category' => implode(', ', $category),
77 'content_ids' => $ids,
78 'currency' => get_woocommerce_currency(),
79 'search_string' => $search_query,
80 'content_type' => 'product',
81 'value' => number_format($values, 2)
82 ];
83
84 $data = array_merge($data, apply_filters('pixelavo_additional_user_info', [], $data['content_ids'] ), pixelavo_get_event_common_data());
85
86 if(!empty($data['content_ids'])) {
87 $pixelavoEventsLocalizedData[] = ['track' => 'track', 'event' => 'Search', 'data' => $data, 'eventID' => $event_id];
88 pixelavo_run_conversions_api('Search', $data, $event_id);
89 }
90 }
91 }
92
93 /**
94 * View Category data.
95 * Run when user to go `product category` page.
96 * @return void
97 */
98 function view_category_data() {
99 if(pixelavo_pixel_status() && function_exists('is_product_category') && is_product_category()) {
100 global $pixelavoEventsLocalizedData;
101
102 $category_slug = get_queried_object()->slug;
103 $category_name = get_queried_object()->name;
104 $args = array(
105 'status' => 'publish',
106 'visibility' => 'visible',
107 'category' => [$category_slug]
108 );
109 $products = wc_get_products($args);
110
111 $ids = [];
112 $titles = [];
113 $contents = [];
114 $values = 0;
115 $event_id = 'ViewCategory.'.strtolower($category_name).'.'.time();
116
117 foreach ($products as $product) {
118 $ids[] = $product->get_id();
119 $titles[] = $product->get_title();
120 $contents[] = ['id' => $product->get_id(), 'quantity' => 1];
121 $values = $values + floatval($product->get_price());
122 }
123
124 $data = [
125 'content_ids' => $ids,
126 'content_category' => $category_name,
127 'content_name' => implode(', ', $titles),
128 'content_type' => 'product',
129 'contents' => $contents,
130 'currency' => get_woocommerce_currency(),
131 'value' => number_format($values, 2)
132 ];
133
134 $data = array_merge($data, apply_filters('pixelavo_additional_user_info', [], $data['content_ids'] ), pixelavo_get_event_common_data());
135
136 if(!empty($data['content_ids'])) {
137 $pixelavoEventsLocalizedData[] = ['track' => 'trackCustom', 'event' => 'ViewCategory', 'data' => $data, 'eventID' => $event_id];
138 pixelavo_run_conversions_api('ViewCategory', $data, $event_id);
139 }
140 }
141 }
142
143 /**
144 * View Content data.
145 * Run when user to go `single product` page.
146 * @return void
147 */
148 function view_content_data() {
149 if(pixelavo_pixel_status() && function_exists('is_product') && is_product()) {
150 global $pixelavoEventsLocalizedData;
151
152 $product = wc_get_product(get_the_ID());
153 $category = [];
154 $event_id = 'ViewContent.'.get_the_ID().'.'.time();
155 foreach (get_the_terms($product->get_id(), 'product_cat') as $cat) {
156 if(!in_array($cat->name, $category)) {
157 $category[] = $cat->name;
158 }
159 }
160 if ( $product ) {
161 $content_type = $product->get_type() === 'variable' ? 'product_group' : 'product';
162 $data = [
163 'content_ids' => [$product->get_id()],
164 'content_category' => implode(', ', $category),
165 'content_name' => $product->get_title(),
166 'content_type' => $content_type,
167 'currency' => get_woocommerce_currency(),
168 'value' => wc_get_price_to_display($product)
169 ];
170
171 $data = array_merge($data, apply_filters('pixelavo_additional_user_info', [], $data['content_ids'] ), pixelavo_get_event_common_data());
172
173
174 if(!empty($data['content_ids'])) {
175 if(apply_filters('pixelavo_view_content_delay', false)) {
176 $pixelavoEventsLocalizedData[] = ['track' => 'track', 'event' => 'ViewContent', 'data' => $data, 'eventID' => $event_id, 'isEventDelay' => true, 'eventDelay' => pixelavo_get_option('view_content_delay', 'pixelavo_settings')];
177 } else {
178 $pixelavoEventsLocalizedData[] = ['track' => 'track', 'event' => 'ViewContent', 'data' => $data, 'eventID' => $event_id];
179 pixelavo_run_conversions_api('ViewContent', $data, $event_id);
180 }
181 }
182 }
183 }
184 }
185 }
186
187 /**
188 * Initialize PixelEventsData Class
189 */
190 PixelEventsData::instance();
191