enrich
2 years ago
events
2 years ago
formEvents
2 years ago
logger
2 years ago
views
2 years ago
class-custom-event-factory.php
7 years ago
class-custom-event.php
2 years ago
class-event-id-generator.php
5 years ago
class-events-manager-ajax_hook.php
2 years ago
class-events-manager.php
2 years ago
class-fixed-notices.php
2 years ago
class-pixel.php
7 years ago
class-plugin-updater.php
2 years ago
class-plugin.php
7 years ago
class-pys.php
2 years ago
class-settings.php
2 years ago
functions-admin.php
2 years ago
functions-common.php
2 years ago
functions-custom-event.php
2 years ago
functions-edd.php
2 years ago
functions-gdpr.php
2 years ago
functions-license.php
2 years ago
functions-migrate.php
2 years ago
functions-optin.php
2 years ago
functions-promo-notices.php
2 years ago
functions-system-report.php
7 years ago
functions-update-plugin.php
6 years ago
functions-woo.php
2 years ago
options_defaults.json
2 years ago
options_fields.json
2 years ago
class-custom-event.php
755 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PixelYourSite; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; // Exit if accessed directly. |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * @property int post_id |
| 11 | * @property string title |
| 12 | * @property bool enabled |
| 13 | * |
| 14 | * @property int delay |
| 15 | * @property array triggers |
| 16 | * @property string trigger_type |
| 17 | * |
| 18 | * @property bool facebook_enabled |
| 19 | * @property string facebook_event_type |
| 20 | * @property string facebook_custom_event_type |
| 21 | * @property bool facebook_params_enabled |
| 22 | * @property array facebook_params |
| 23 | * @property array facebook_custom_params |
| 24 | * |
| 25 | * @property bool pinterest_enabled |
| 26 | * @property string pinterest_event_type |
| 27 | * @property string pinterest_custom_event_type |
| 28 | * @property bool pinterest_params_enabled |
| 29 | * @property array pinterest_custom_params |
| 30 | * @property array ga_custom_params |
| 31 | * @property array ga_params |
| 32 | * |
| 33 | * @property bool ga_enabled |
| 34 | * @property string ga_event_action |
| 35 | * @property string ga_custom_event_action |
| 36 | * @property string ga_event_category |
| 37 | * @property string ga_event_label |
| 38 | * @property string ga_event_value |
| 39 | * |
| 40 | * @property bool ga_ads_enabled |
| 41 | * @property string ga_ads_pixel_id |
| 42 | * @property string ga_ads_event_action |
| 43 | * @property string ga_ads_custom_event_action |
| 44 | * @property array ga_ads_custom_params |
| 45 | * @property array ga_ads_params |
| 46 | * @property string ga_ads_version |
| 47 | * @property string ga_ads_event_category |
| 48 | * @property string ga_ads_event_label |
| 49 | * |
| 50 | * |
| 51 | * @property bool bing_enabled |
| 52 | * @property string bing_event_action |
| 53 | * @property string bing_event_category |
| 54 | * @property string bing_event_label |
| 55 | * @property string bing_event_value |
| 56 | */ |
| 57 | class CustomEvent { |
| 58 | |
| 59 | private $post_id; |
| 60 | |
| 61 | private $title = 'Untitled'; |
| 62 | |
| 63 | private $enabled = true; |
| 64 | |
| 65 | public $GAEvents = array( |
| 66 | "" => array("CustomEvent"=>array()), |
| 67 | "All Properties" => array( |
| 68 | "earn_virtual_currency" => array("virtual_currency_name","value"), |
| 69 | "join_group" => array("group_id"), |
| 70 | "login" => array("method"), |
| 71 | "purchase" => array("transaction_id",'value','currency','tax','shipping','items','coupon'), |
| 72 | "refund" => array("transaction_id",'value','currency','tax','shipping','items'), |
| 73 | "search" => array("search_term"), |
| 74 | "select_content" => array("content_type",'item_id'), |
| 75 | "share" => array("content_type",'item_id'), |
| 76 | "sign_up" => array("method"), |
| 77 | "spend_virtual_currency" => array("item_name",'virtual_currency_name','value'), |
| 78 | "tutorial_begin" => array(), |
| 79 | "tutorial_complete" => array(), |
| 80 | ), |
| 81 | "Retail/Ecommerce" => array( |
| 82 | 'add_payment_info' => array('coupon','currency','items','payment_type','value'), |
| 83 | 'add_shipping_info' => array('coupon','currency','items','shipping_tier','value'), |
| 84 | 'add_to_cart' => array('currency', 'items', 'value'), |
| 85 | 'add_to_wishlist' => array('currency', 'items', 'value'), |
| 86 | 'begin_checkout' => array('coupon','currency', 'items', 'value'), |
| 87 | 'generate_lead' => array('value', 'currency'), |
| 88 | 'purchase' => array('affiliation', 'coupon', 'currency', 'items', 'transaction_id', 'shipping', 'tax', 'value'), |
| 89 | 'refund' => array('affiliation', 'coupon', 'currency', 'items', 'transaction_id', 'shipping', 'tax', 'value'), |
| 90 | 'remove_from_cart' => array('currency', 'items', 'value'), |
| 91 | 'select_item' => array('items', 'item_list_name', 'item_list_id'), |
| 92 | 'select_promotion' => array('items', 'promotion_id', 'promotion_name', 'creative_name', 'creative_slot', 'location_id'), |
| 93 | 'view_cart' => array('currency', 'items', 'value'), |
| 94 | 'view_item' => array('currency', 'items', 'value'), |
| 95 | 'view_item_list' => array('items', 'item_list_name', 'item_list_id'), |
| 96 | 'view_promotion' => array('items', 'promotion_id', 'promotion_name', 'creative_name', 'creative_slot', 'location_id') |
| 97 | ), |
| 98 | "Jobs, Education, Local Deals, Real Estate" => array( |
| 99 | 'add_payment_info' => array("coupon", 'currency', 'items', 'payment_type', 'value'), |
| 100 | 'add_shipping_info' => array('coupon', 'currency', 'items', 'shipping_tier', 'value'), |
| 101 | 'add_to_cart' => array('currency', 'items', 'value'), |
| 102 | 'add_to_wishlist' => array('currency', 'items', 'value'), |
| 103 | 'begin_checkout' => array('coupon','currency', 'items', 'value'), |
| 104 | 'purchase' => array('affiliation', 'coupon', 'currency', 'items', 'transaction_id', 'shipping', 'tax', 'value'), |
| 105 | 'refund' => array('affiliation', 'coupon', 'currency', 'items', 'transaction_id', 'shipping', 'tax', 'value'), |
| 106 | 'remove_from_cart' => array('currency', 'items', 'value'), |
| 107 | 'select_item' => array('items', 'item_list_name', 'item_list_id'), |
| 108 | 'select_promotion' => array('items', 'promotion_id', 'promotion_name', 'creative_name', 'creative_slot', 'location_id'), |
| 109 | 'view_cart' => array('currency', 'items', 'value'), |
| 110 | 'view_item' => array('currency', 'items', 'value'), |
| 111 | 'view_item_list' => array('items', 'item_list_name', 'item_list_id'), |
| 112 | 'view_promotion' => array('items', 'promotion_id', 'promotion_name', 'creative_name', 'creative_slot', 'location_id') |
| 113 | ), |
| 114 | "Travel (Hotel/Air)" => array( |
| 115 | 'add_payment_info' => array("coupon", 'currency', 'items', 'payment_type', 'value'), |
| 116 | 'add_shipping_info' => array('coupon', 'currency', 'items', 'shipping_tier', 'value'), |
| 117 | 'add_to_cart' => array('currency', 'items', 'value'), |
| 118 | 'add_to_wishlist' => array('currency', 'items', 'value'), |
| 119 | 'begin_checkout' => array('coupon','currency', 'items', 'value'), |
| 120 | 'generate_lead' => array('value', 'currency'), |
| 121 | 'purchase' => array('affiliation', 'coupon', 'currency', 'items', 'transaction_id', 'shipping', 'tax', 'value'), |
| 122 | 'refund' => array('affiliation', 'coupon', 'currency', 'items', 'transaction_id', 'shipping', 'tax', 'value'), |
| 123 | 'remove_from_cart' => array('currency', 'items', 'value'), |
| 124 | 'select_item' => array('items', 'item_list_name', 'item_list_id'), |
| 125 | 'select_promotion' => array('items', 'promotion_id', 'promotion_name', 'creative_name', 'creative_slot', 'location_id'), |
| 126 | 'view_cart' => array('currency', 'items', 'value'), |
| 127 | 'view_item' => array('currency', 'items', 'value'), |
| 128 | 'view_item_list' => array('items', 'item_list_name', 'item_list_id'), |
| 129 | 'view_promotion' => array('items', 'promotion_id', 'promotion_name', 'creative_name', 'creative_slot', 'location_id') |
| 130 | ), |
| 131 | "Games" => array( |
| 132 | 'earn_virtual_currency' => array('virtual_currency_name', 'value'), |
| 133 | 'join_group' => array('group_id'), |
| 134 | 'level_end' => array('level_name', 'success'), |
| 135 | 'level_start' => array('level_name'), |
| 136 | 'level_up' => array('character', 'level'), |
| 137 | 'post_score' => array('level', 'character', 'score'), |
| 138 | 'select_content' => array('content_type', 'item_id'), |
| 139 | 'spend_virtual_currency' => array('item_name', 'virtual_currency_name', 'value'), |
| 140 | 'tutorial_begin' => array(), |
| 141 | 'tutorial_complete' => array(), |
| 142 | 'unlock_achievement' => array('achievement_id'), |
| 143 | ) |
| 144 | ); |
| 145 | |
| 146 | private $data = array( |
| 147 | 'delay' => null, |
| 148 | 'trigger_type' => 'page_visit', |
| 149 | 'triggers' => array(), |
| 150 | |
| 151 | 'facebook_enabled' => false, |
| 152 | 'facebook_event_type' => 'ViewContent', |
| 153 | 'facebook_custom_event_type' => null, |
| 154 | 'facebook_params_enabled' => false, |
| 155 | 'facebook_params' => array(), |
| 156 | 'facebook_custom_params' => array(), |
| 157 | |
| 158 | 'pinterest_enabled' => false, |
| 159 | 'pinterest_event_type' => 'ViewContent', |
| 160 | 'pinterest_custom_event_type' => null, |
| 161 | 'pinterest_params_enabled' => false, |
| 162 | 'pinterest_custom_params' => array(), |
| 163 | |
| 164 | 'ga_enabled' => false, |
| 165 | 'ga_event_action' => '_custom', |
| 166 | 'ga_custom_event_action' => null, |
| 167 | 'ga_event_category' => null, |
| 168 | 'ga_event_label' => null, |
| 169 | 'ga_event_value' => null, |
| 170 | |
| 171 | //ver 4 |
| 172 | 'ga_params' => array(), |
| 173 | 'ga_custom_params' => array(), |
| 174 | 'ga_custom_params_enabled' => false, |
| 175 | |
| 176 | 'ga_ads_enabled' => false, |
| 177 | 'ga_ads_event_action' => '_custom', |
| 178 | 'ga_ads_custom_event_action' => null, |
| 179 | //ver 4 |
| 180 | 'ga_ads_params' => array(), |
| 181 | 'ga_ads_custom_params' => array(), |
| 182 | 'ga_ads_custom_params_enabled' => false, |
| 183 | |
| 184 | |
| 185 | 'bing_enabled' => false, |
| 186 | 'bing_event_action' => null, |
| 187 | 'bing_event_category' => null, |
| 188 | 'bing_event_label' => null, |
| 189 | 'bing_event_value' => null, |
| 190 | ); |
| 191 | |
| 192 | public function __construct( $post_id = null ) { |
| 193 | $this->initialize( $post_id ); |
| 194 | } |
| 195 | |
| 196 | public function __get( $key ) { |
| 197 | |
| 198 | if ( $key == 'post_id' ) { |
| 199 | return $this->post_id; |
| 200 | } |
| 201 | |
| 202 | if ( $key == 'title' ) { |
| 203 | return $this->title; |
| 204 | } |
| 205 | |
| 206 | if ( $key == 'enabled' ) { |
| 207 | return $this->enabled; |
| 208 | } |
| 209 | |
| 210 | if ( isset( $this->data[ $key ] ) ) { |
| 211 | return $this->data[ $key ]; |
| 212 | } else { |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 | private function initialize( $post_id ) { |
| 219 | |
| 220 | if ( $post_id ) { |
| 221 | |
| 222 | $this->post_id = $post_id; |
| 223 | $this->title = get_the_title( $post_id ); |
| 224 | |
| 225 | $data = get_post_meta( $post_id, '_pys_event_data', true ); |
| 226 | $this->data = is_array( $data ) ? $data : array(); |
| 227 | |
| 228 | $state = get_post_meta( $post_id, '_pys_event_state', true ); |
| 229 | $this->enabled = $state == 'active' ? true : false; |
| 230 | |
| 231 | |
| 232 | if(count(GA()->getPixelIDs()) == 0) { |
| 233 | $this->data['ga_enabled'] = false; |
| 234 | $this->clearGa(); |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
| 241 | public function update( $args = null ) { |
| 242 | |
| 243 | if ( ! is_array( $args ) ) { |
| 244 | $args = $this->data; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * GENERAL |
| 249 | */ |
| 250 | |
| 251 | // title |
| 252 | wp_update_post( array( |
| 253 | 'ID' => $this->post_id, |
| 254 | 'post_title' => empty( $args['title'] ) ? $this->title : sanitize_text_field( $args['title'] ) |
| 255 | ) ); |
| 256 | |
| 257 | // state |
| 258 | $state = isset( $args['enabled'] ) && $args['enabled'] ? 'active' : 'paused'; |
| 259 | $this->enabled = $state == 'active' ? true : false; |
| 260 | update_post_meta( $this->post_id, '_pys_event_state', $state ); |
| 261 | |
| 262 | // trigger type |
| 263 | $this->data['trigger_type'] = 'page_visit'; |
| 264 | |
| 265 | // delay |
| 266 | $this->data['delay'] = isset( $args['delay'] ) && $args['delay'] ? (int) $args['delay'] : null; |
| 267 | |
| 268 | /** |
| 269 | * TRIGGERS |
| 270 | */ |
| 271 | |
| 272 | // reset old triggers |
| 273 | $this->data['triggers'] = array(); |
| 274 | |
| 275 | // page visit triggers |
| 276 | if ( $this->trigger_type == 'page_visit' && isset( $args['page_visit_triggers'] ) |
| 277 | && is_array( $args['page_visit_triggers'] ) ) { |
| 278 | |
| 279 | foreach ( $args['page_visit_triggers'] as $trigger ) { |
| 280 | |
| 281 | if ( ! empty( $trigger['value'] ) ) { |
| 282 | |
| 283 | $this->data['triggers'][] = array( |
| 284 | 'rule' => $trigger['rule'] == 'contains' ? 'contains' : 'match', |
| 285 | 'value' => $trigger['value'], |
| 286 | ); |
| 287 | |
| 288 | } |
| 289 | |
| 290 | } |
| 291 | |
| 292 | } |
| 293 | |
| 294 | // reset old url filters |
| 295 | $this->data['url_filters'] = array(); |
| 296 | |
| 297 | /** |
| 298 | |
| 299 | */ |
| 300 | |
| 301 | $facebook_event_types = array( |
| 302 | 'ViewContent', |
| 303 | 'AddToCart', |
| 304 | 'AddToWishlist', |
| 305 | 'InitiateCheckout', |
| 306 | 'AddPaymentInfo', |
| 307 | 'Purchase', |
| 308 | 'Lead', |
| 309 | 'CompleteRegistration', |
| 310 | |
| 311 | 'Subscribe', |
| 312 | 'CustomizeProduct', |
| 313 | 'FindLocation', |
| 314 | 'StartTrial', |
| 315 | 'SubmitApplication', |
| 316 | 'Schedule', |
| 317 | 'Contact', |
| 318 | 'Donate', |
| 319 | |
| 320 | 'CustomEvent' |
| 321 | ); |
| 322 | |
| 323 | // enabled |
| 324 | $this->data['facebook_enabled'] = isset( $args['facebook_enabled'] ) && $args['facebook_enabled'] ? true : false; |
| 325 | |
| 326 | // event type |
| 327 | $this->data['facebook_event_type'] = isset( $args['facebook_event_type'] ) && in_array( $args['facebook_event_type'], $facebook_event_types ) |
| 328 | ? sanitize_text_field( $args['facebook_event_type'] ) |
| 329 | : 'ViewContent'; |
| 330 | |
| 331 | // custom event type |
| 332 | $this->data['facebook_custom_event_type'] = $this->facebook_event_type == 'CustomEvent' && ! empty( $args['facebook_custom_event_type'] ) |
| 333 | ? sanitizeKey( $args['facebook_custom_event_type'] ) |
| 334 | : null; |
| 335 | |
| 336 | // params enabled |
| 337 | $this->data['facebook_params_enabled'] = isset( $args['facebook_params_enabled'] ) && $args['facebook_params_enabled'] ? true : false; |
| 338 | |
| 339 | // params |
| 340 | if ( $this->facebook_params_enabled && isset( $args['facebook_params'] ) && $this->facebook_event_type !== 'CustomEvent' ) { |
| 341 | |
| 342 | $this->data['facebook_params'] = array( |
| 343 | 'value' => ! empty( $args['facebook_params']['value'] ) ? sanitize_text_field( $args['facebook_params']['value'] ) : null, |
| 344 | 'currency' => ! empty( $args['facebook_params']['currency'] ) ? sanitize_text_field( $args['facebook_params']['currency'] ) : null, |
| 345 | 'content_name' => ! empty( $args['facebook_params']['content_name'] ) ? sanitize_text_field( $args['facebook_params']['content_name'] ) : null, |
| 346 | 'content_ids' => ! empty( $args['facebook_params']['content_ids'] ) ? sanitize_text_field( $args['facebook_params']['content_ids'] ) : null, |
| 347 | 'content_type' => ! empty( $args['facebook_params']['content_type'] ) ? sanitize_text_field( $args['facebook_params']['content_type'] ) : null, |
| 348 | 'content_category' => ! empty( $args['facebook_params']['content_category'] ) ? sanitize_text_field( $args['facebook_params']['content_category'] ) : null, |
| 349 | 'num_items' => ! empty( $args['facebook_params']['num_items'] ) ? (int) $args['facebook_params']['num_items'] : null, |
| 350 | 'order_id' => ! empty( $args['facebook_params']['order_id'] ) ? sanitize_text_field( $args['facebook_params']['order_id'] ) : null, |
| 351 | 'search_string' => ! empty( $args['facebook_params']['search_string'] ) ? sanitize_text_field( $args['facebook_params']['search_string'] ) : null, |
| 352 | 'status' => ! empty( $args['facebook_params']['status'] ) ? sanitize_text_field( $args['facebook_params']['status'] ) : null, |
| 353 | 'predicted_ltv' => ! empty( $args['facebook_params']['predicted_ltv'] ) ? sanitize_text_field( $args['facebook_params']['predicted_ltv'] ) : null, |
| 354 | ); |
| 355 | |
| 356 | // custom currency |
| 357 | if ( $this->data['facebook_params']['currency'] == 'custom' && ! empty( $args['facebook_params']['custom_currency'] )) { |
| 358 | $this->data['facebook_params']['custom_currency'] = sanitize_text_field( $args['facebook_params']['custom_currency'] ); |
| 359 | } else { |
| 360 | $this->data['facebook_params']['custom_currency'] = null; |
| 361 | } |
| 362 | |
| 363 | } else { |
| 364 | |
| 365 | $this->data['facebook_params'] = array( |
| 366 | 'value' => null, |
| 367 | 'currency' => null, |
| 368 | 'custom_currency' => null, |
| 369 | 'content_name' => null, |
| 370 | 'content_ids' => null, |
| 371 | 'content_type' => null, |
| 372 | 'content_category' => null, |
| 373 | 'num_items' => null, |
| 374 | 'order_id' => null, |
| 375 | 'search_string' => null, |
| 376 | 'status' => null, |
| 377 | 'predicted_ltv' => null, |
| 378 | ); |
| 379 | |
| 380 | } |
| 381 | |
| 382 | // reset old custom params |
| 383 | $this->data['facebook_custom_params'] = array(); |
| 384 | |
| 385 | // custom params |
| 386 | if ( $this->facebook_params_enabled && isset( $args['facebook_custom_params'] ) ) { |
| 387 | |
| 388 | foreach ( $args['facebook_custom_params'] as $custom_param ) { |
| 389 | |
| 390 | if ( ! empty( $custom_param['name'] ) && ! empty( $custom_param['value'] ) ) { |
| 391 | |
| 392 | $this->data['facebook_custom_params'][] = array( |
| 393 | 'name' => sanitize_text_field( $custom_param['name'] ), |
| 394 | 'value' => sanitize_text_field( $custom_param['value'] ), |
| 395 | ); |
| 396 | |
| 397 | } |
| 398 | |
| 399 | } |
| 400 | |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | |
| 405 | */ |
| 406 | |
| 407 | $pinterest_event_types = array( |
| 408 | 'pagevisit', |
| 409 | 'viewcategory', |
| 410 | 'search', |
| 411 | 'addtocart', |
| 412 | 'checkout', |
| 413 | 'watchvideo', |
| 414 | 'signup', |
| 415 | 'lead', |
| 416 | 'custom', |
| 417 | 'CustomEvent', |
| 418 | ); |
| 419 | |
| 420 | // enabled |
| 421 | $this->data['pinterest_enabled'] = isset( $args['pinterest_enabled'] ) && $args['pinterest_enabled'] ? true |
| 422 | : false; |
| 423 | |
| 424 | // event type |
| 425 | $this->data['pinterest_event_type'] = isset( $args['pinterest_event_type'] ) && in_array( $args['pinterest_event_type'], |
| 426 | $pinterest_event_types ) |
| 427 | ? sanitize_text_field( $args['pinterest_event_type'] ) |
| 428 | : 'pagevisit'; |
| 429 | |
| 430 | // custom event type |
| 431 | $this->data['pinterest_custom_event_type'] = $this->pinterest_event_type == 'CustomEvent' && ! empty( $args['pinterest_custom_event_type'] ) |
| 432 | ? sanitizeKey( $args['pinterest_custom_event_type'] ) |
| 433 | : null; |
| 434 | |
| 435 | // params enabled |
| 436 | $this->data['pinterest_params_enabled'] = isset( $args['pinterest_params_enabled'] ) && $args['pinterest_params_enabled'] |
| 437 | ? true : false; |
| 438 | |
| 439 | // reset old custom params |
| 440 | $this->data['pinterest_custom_params'] = array(); |
| 441 | |
| 442 | // custom params |
| 443 | if ( $this->pinterest_params_enabled && isset( $args['pinterest_custom_params'] ) ) { |
| 444 | |
| 445 | foreach ( $args['pinterest_custom_params'] as $custom_param ) { |
| 446 | |
| 447 | if ( ! empty( $custom_param['name'] ) && ! empty( $custom_param['value'] ) ) { |
| 448 | |
| 449 | $this->data['pinterest_custom_params'][] = array( |
| 450 | 'name' => sanitize_text_field( $custom_param['name'] ), |
| 451 | 'value' => sanitize_text_field( $custom_param['value'] ), |
| 452 | ); |
| 453 | |
| 454 | } |
| 455 | |
| 456 | } |
| 457 | |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * GOOGLE ANALYTICS |
| 462 | */ |
| 463 | $this->updateGA($args); |
| 464 | |
| 465 | /** |
| 466 | * BING |
| 467 | */ |
| 468 | |
| 469 | $this->data['bing_enabled'] = isset($args['bing_enabled']) && $args['bing_enabled'] ? true : false; |
| 470 | $this->data['bing_event_action'] = !empty($args['bing_event_action']) ? sanitize_text_field($args['bing_event_action']) : null; |
| 471 | $this->data['bing_event_category'] = !empty($args['bing_event_category']) ? sanitize_text_field($args['bing_event_category']) : null; |
| 472 | $this->data['bing_event_label'] = !empty($args['bing_event_label']) ? sanitize_text_field($args['bing_event_label']) : null; |
| 473 | $this->data['bing_event_value'] = !empty($args['bing_event_value']) ? sanitize_text_field($args['bing_event_value']) : null; |
| 474 | |
| 475 | update_post_meta( $this->post_id, '_pys_event_data', $this->data ); |
| 476 | |
| 477 | } |
| 478 | |
| 479 | public function enable() { |
| 480 | |
| 481 | $this->enabled = true; |
| 482 | update_post_meta( $this->post_id, '_pys_event_state', 'active' ); |
| 483 | |
| 484 | } |
| 485 | |
| 486 | public function disable() { |
| 487 | |
| 488 | $this->enabled = false; |
| 489 | update_post_meta( $this->post_id, '_pys_event_state', 'paused' ); |
| 490 | |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * @return int |
| 495 | */ |
| 496 | public function getPostId() { |
| 497 | return $this->post_id; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * @return string |
| 502 | */ |
| 503 | public function getTitle() { |
| 504 | return $this->title; |
| 505 | } |
| 506 | |
| 507 | public function isEnabled() { |
| 508 | return $this->enabled; |
| 509 | } |
| 510 | |
| 511 | public function getTriggerType() { |
| 512 | return $this->trigger_type; |
| 513 | } |
| 514 | |
| 515 | public function getDelay() { |
| 516 | return $this->delay; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * @return array |
| 521 | */ |
| 522 | public function getPageVisitTriggers() { |
| 523 | return $this->trigger_type == 'page_visit' ? $this->triggers : array(); |
| 524 | } |
| 525 | |
| 526 | public function isFacebookEnabled() { |
| 527 | return (bool) $this->facebook_enabled; |
| 528 | } |
| 529 | |
| 530 | public function getFacebookEventType() { |
| 531 | return $this->facebook_event_type == 'CustomEvent' ? $this->facebook_custom_event_type : $this->facebook_event_type; |
| 532 | } |
| 533 | |
| 534 | public function isFacebookParamsEnabled() { |
| 535 | return (bool) $this->facebook_params_enabled; |
| 536 | } |
| 537 | |
| 538 | public function getFacebookParam( $key ) { |
| 539 | return isset( $this->facebook_params[ $key ] ) ? $this->facebook_params[ $key ] : null; |
| 540 | } |
| 541 | |
| 542 | public function getFacebookParams() { |
| 543 | return $this->facebook_params_enabled ? $this->facebook_params : array(); |
| 544 | } |
| 545 | |
| 546 | public function getFacebookCustomParams() { |
| 547 | return $this->facebook_params_enabled ? $this->facebook_custom_params : array(); |
| 548 | } |
| 549 | |
| 550 | public function isPinterestEnabled() { |
| 551 | return (bool) $this->pinterest_enabled; |
| 552 | } |
| 553 | |
| 554 | public function getPinterestEventType() { |
| 555 | return $this->pinterest_event_type == 'CustomEvent' |
| 556 | ? $this->pinterest_custom_event_type |
| 557 | : $this->pinterest_event_type; |
| 558 | } |
| 559 | |
| 560 | public function isPinterestParamsEnabled() { |
| 561 | return (bool) $this->pinterest_params_enabled; |
| 562 | } |
| 563 | |
| 564 | public function getPinterestCustomParams() { |
| 565 | return $this->pinterest_params_enabled ? $this->pinterest_custom_params : array(); |
| 566 | } |
| 567 | |
| 568 | public function isGoogleAnalyticsEnabled() { |
| 569 | return (bool) $this->ga_enabled; |
| 570 | } |
| 571 | public function isUnifyAnalyticsEnabled(){ |
| 572 | return (bool) $this->ga_ads_enabled; |
| 573 | } |
| 574 | public function getMergedGaParams() { |
| 575 | if(is_array($this->ga_ads_params)) { |
| 576 | return $this->ga_ads_params; |
| 577 | } else { |
| 578 | return []; |
| 579 | } |
| 580 | } |
| 581 | public function getGoogleAnalyticsAction() { |
| 582 | return $this->ga_event_action == '_custom' || |
| 583 | $this->ga_event_action == 'CustomEvent' ? $this->ga_custom_event_action : $this->ga_event_action; |
| 584 | } |
| 585 | public function getMergedAction(){ |
| 586 | return $this->ga_ads_event_action == '_custom' || $this->ga_ads_event_action == 'CustomEvent' ? $this->ga_ads_custom_event_action : $this->ga_ads_event_action; |
| 587 | } |
| 588 | public function isBingEnabled() { |
| 589 | return (bool) $this->bing_enabled; |
| 590 | } |
| 591 | public function isGaV4() { |
| 592 | $all = GA()->getPixelIDs(); |
| 593 | if(count($all) == 0) { |
| 594 | return false; |
| 595 | } |
| 596 | return strpos($all[0], 'G') === 0; |
| 597 | } |
| 598 | private function clearGa() { |
| 599 | $this->data['ga_params'] = array(); |
| 600 | $this->data['ga_custom_params'] = array(); |
| 601 | $this->data['ga_event_action'] = 'CustomEvent'; |
| 602 | $this->data['ga_custom_event_action']=null; |
| 603 | // old |
| 604 | $this->data['ga_event_category'] = null; |
| 605 | $this->data['ga_event_label'] = null; |
| 606 | $this->data['ga_event_value'] = null; |
| 607 | } |
| 608 | function migrateUnifyGA() { |
| 609 | $all = GA()->getPixelIDs(); |
| 610 | $this->data['ga_ads_enabled'] = $this->isGoogleAnalyticsEnabled(); |
| 611 | $pixel_ids = GA()->getPixelIDs(); |
| 612 | |
| 613 | if(!empty($this->data['ga_ads_pixel_id'])){ return; } |
| 614 | $this->data['ga_ads_pixel_id'] = array_map(function($pixelId) use ($all) { |
| 615 | if (in_array($pixelId, $all) || $pixelId == 'all') { |
| 616 | return $pixelId; |
| 617 | } else { |
| 618 | return ''; |
| 619 | } |
| 620 | }, $pixel_ids); |
| 621 | |
| 622 | $this->data['ga_ads_pixel_id'] = array_filter($this->data['ga_ads_pixel_id']); |
| 623 | |
| 624 | $this->data['ga_ads_event_action'] = $this->ga_event_action; |
| 625 | $this->data['ga_ads_custom_event_action'] = $this->ga_event_action == '_custom' || $this->ga_event_action == 'CustomEvent' ? $this->ga_custom_event_action : ''; |
| 626 | $this->data['ga_ads_params'] = $this->getGaParams(); |
| 627 | $this->data['ga_ads_custom_params'] = $this->getGACustomParams(); |
| 628 | |
| 629 | $outputArray = []; |
| 630 | |
| 631 | foreach ($this->data['ga_ads_custom_params'] as $item) { |
| 632 | $key = $item["name"]; |
| 633 | if (!isset($outputArray[$key])) { |
| 634 | $outputArray[$key] = $item; |
| 635 | } |
| 636 | } |
| 637 | $this->data['ga_ads_custom_params'] = array_values($outputArray); |
| 638 | update_post_meta( $this->post_id, '_pys_event_data', $this->data ); |
| 639 | } |
| 640 | private function updateGA( $args) { |
| 641 | |
| 642 | $all = GA()->getPixelIDs(); |
| 643 | |
| 644 | $this->data['ga_ads_enabled'] = count($all) > 0 |
| 645 | && isset( $args['ga_ads_enabled'] ) |
| 646 | && $args['ga_ads_enabled']; |
| 647 | |
| 648 | if(!$this->data['ga_ads_enabled']) { |
| 649 | $this->clearGa(); |
| 650 | } else { |
| 651 | if($this->isGaV4()) { |
| 652 | |
| 653 | $this->data['ga_ads_event_action'] = isset( $args['ga_ads_event_action'] ) |
| 654 | ? sanitize_text_field( $args['ga_ads_event_action'] ) |
| 655 | : 'view_item'; |
| 656 | |
| 657 | $this->data['ga_ads_custom_event_action'] = $this->ga_ads_event_action == '_custom' || $this->ga_ads_event_action == 'CustomEvent' && !empty( $args['ga_ads_custom_event_action'] ) |
| 658 | ? sanitizeKey( $args['ga_ads_custom_event_action'] ) |
| 659 | : null; |
| 660 | |
| 661 | $this->data['ga_ads_params'] = array(); |
| 662 | |
| 663 | |
| 664 | foreach ($this->GAEvents as $group) { |
| 665 | foreach ($group as $name => $fields) { |
| 666 | if($name == $this->data['ga_ads_event_action']) { |
| 667 | foreach ($fields as $field) { |
| 668 | $this->data['ga_ads_params'][$field] = isset($args['ga_ads_params'][$field]) ? $args['ga_ads_params'][$field] : ""; |
| 669 | } |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if ( isset( $args['ga_ads_params'] ) ) { |
| 676 | foreach ($args['ga_ads_params'] as $key => $val) { |
| 677 | $this->data['ga_ads_params'][$key] = sanitize_text_field( $val ); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // reset old custom params |
| 682 | $this->data['ga_ads_custom_params'] = array(); |
| 683 | |
| 684 | // custom params |
| 685 | if ( isset( $args['ga_ads_custom_params'] ) ) { |
| 686 | |
| 687 | foreach ( $args['ga_ads_custom_params'] as $custom_param ) { |
| 688 | |
| 689 | if ( ! empty( $custom_param['name'] ) && ! empty( $custom_param['value'] ) ) { |
| 690 | |
| 691 | $this->data['ga_ads_custom_params'][] = array( |
| 692 | 'name' => sanitize_text_field( $custom_param['name'] ), |
| 693 | 'value' => sanitize_text_field( $custom_param['value'] ), |
| 694 | ); |
| 695 | |
| 696 | } |
| 697 | |
| 698 | } |
| 699 | |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | public function getGAMergedCustomParams() { |
| 705 | if(is_array($this->ga_ads_custom_params)) { |
| 706 | return $this->ga_ads_custom_params; |
| 707 | } |
| 708 | return []; |
| 709 | } |
| 710 | public function getGACustomParams() { |
| 711 | if($this->isGaV4()) { |
| 712 | if(is_array($this->ga_custom_params)) { |
| 713 | return $this->ga_custom_params; |
| 714 | } |
| 715 | return []; |
| 716 | } |
| 717 | $custom = array(); |
| 718 | if($this->ga_event_category) { |
| 719 | $custom[] = array('name'=>"event_category",'value' => $this->ga_event_category); |
| 720 | } |
| 721 | if($this->ga_event_value){ |
| 722 | $custom[] = array('name'=>"value",'value' => $this->ga_event_value); |
| 723 | } |
| 724 | if($this->ga_event_label){ |
| 725 | $custom[] = array('name'=>"event_label",'value' => $this->ga_event_label); |
| 726 | } |
| 727 | |
| 728 | return $custom; |
| 729 | } |
| 730 | |
| 731 | public function getGaParams() { |
| 732 | if($this->isGaV4()) |
| 733 | if(is_array($this->ga_params)) { |
| 734 | return $this->ga_params; |
| 735 | } else { |
| 736 | return []; |
| 737 | } |
| 738 | $list = array(); |
| 739 | foreach ($this->GAEvents as $group) { |
| 740 | foreach ($group as $name => $fields) { |
| 741 | if($name == $this->data['ga_event_action']) { |
| 742 | foreach ($fields as $field) { |
| 743 | $list[$field] = ""; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | return $list; |
| 750 | } |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | } |