PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.2.9
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.2.9
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / admin / settings / integration / facebook-integration.php

facebook-integration.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.2.9, at includes/admin/settings/integration/facebook-integration.php

265 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Admin\Settings\Integration;
4
5 defined( 'ABSPATH' ) || die();
6
7 /**
8 * Class Facebook Pixel integration.
9 *
10 * @package Sellkit\Admin\Settings\Integration\Settings_Integration
11 * @since 1.1.0
12 */
13 class Facebook extends Settings_Integration {
14 /**
15 * The class instance.
16 *
17 * @var Object Class instance.
18 * @since 1.1.0
19 */
20 public static $instance = null;
21
22 /**
23 * Class Instance.
24 *
25 * @since 1.1.0
26 * @return Sellkit_Funnel|null
27 */
28 public static function get_instance() {
29 if ( null === self::$instance ) {
30 self::$instance = new self();
31 }
32
33 return self::$instance;
34 }
35
36 /**
37 * Class constructor.
38 *
39 * @since 1.1.0
40 */
41 public function __construct() {
42 parent::__construct();
43
44 $this->analytics();
45 }
46
47 /**
48 * Get Facebook analyticts scripts.
49 *
50 * @since 1.1.0
51 */
52 public function analytics() {
53 $fb_tracking_id = esc_attr( sellkit_get_option( 'facebook_pixel_id' ) );
54
55 if ( empty( $fb_tracking_id ) || empty( sellkit_get_option( 'facebook_pixel' ) ) ) {
56 return;
57 }
58
59 // phpcs:disable
60 $facebook_script =
61 "<!-- Facebook Pixel Script By Sellkit -->
62 <script type='text/javascript'>
63 !function(f,b,e,v,n,t,s)
64 {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
65 n.callMethod.apply(n,arguments):n.queue.push(arguments)};
66 if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
67 n.queue=[];t=b.createElement(e);t.async=!0;
68 t.src=v;s=b.getElementsByTagName(e)[0];
69 s.parentNode.insertBefore(t,s)}(window, document,'script',
70 'https://connect.facebook.net/en_US/fbevents.js');
71 </script>
72
73 <noscript>
74 <img height='1' width='1' style='display:none' src='https://www.facebook.com/tr?id=" . esc_js( $fb_tracking_id ) . "&ev=PageView&noscript=1'/>
75 </noscript>
76
77 <script type='text/javascript'>
78 fbq('init', " . esc_js( $fb_tracking_id ) . ");
79 fbq('track', 'PageView', {'plugin': 'Sellkit'});
80 </script>
81 <!-- Facebook Pixel Script By Sellkit -->
82 ";
83 // phpcs:enable
84
85 $facebook_script .= $this->facebook_pixel_events( self::$post, $fb_tracking_id );
86
87 echo $facebook_script;
88 }
89
90 /**
91 * Facebook pixel events scripts.
92 *
93 * @since 1.1.0
94 * @param array $post_meta sellkit pages data.
95 * @param string $tracking_id fb pixel tracking id.
96 */
97 private function facebook_pixel_events( $post_meta, $tracking_id ) {
98 $fb_pixel_events = sellkit_get_option( 'facebook_pixel_events' );
99
100 if ( empty( $fb_pixel_events ) ) {
101 return;
102 }
103
104 $sellkit_page_meta = $post_meta[0]['type']['key'];
105 $fb_events_script = '';
106
107 foreach ( $fb_pixel_events as $event ) {
108 $function_name = "fb_pixel_{$event}";
109
110 $fb_events_script .= $this->$function_name( $sellkit_page_meta, $tracking_id );
111 }
112
113 return $fb_events_script;
114 }
115
116 /**
117 * Get facebook pixel initiate_checkout data.
118 *
119 * @since 1.1.0
120 * @param string $sellkit_page_meta sellkit pages type.
121 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
122 */
123 private function fb_pixel_initiate_checkout( $sellkit_page_meta ) {
124 if ( 'checkout' !== $sellkit_page_meta ) {
125 return;
126 }
127
128 $cart_content = $this->get_cart_data( 'add_to_cart' );
129 $initiate_checkout = $this->get_cart_data( 'initiate_checkout' );
130
131 $fb_events_script =
132 "<script type='text/javascript'>
133 fbq( 'track', 'AddToCart', $cart_content );
134 fbq( 'track', 'InitiateCheckout', $initiate_checkout );
135 </script>";
136
137 return $fb_events_script;
138 }
139
140 /**
141 * Get facebook pixel add_payment_info data.
142 *
143 * @since 1.1.0
144 * @param string $sellkit_page_meta sellkit pages type.
145 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
146 */
147 private function fb_pixel_add_payment_info( $sellkit_page_meta ) {
148 if ( 'checkout' !== $sellkit_page_meta ) {
149 return;
150 }
151
152 $cart_content = $this->get_cart_data( 'add_payment_info' );
153
154 self::$localized_data['fbAddPaymentInfo'] = $cart_content;
155 }
156
157 /**
158 * Get facebook pixel purchage_complete data.
159 *
160 * @since 1.1.0
161 * @param string $sellkit_page_meta sellkit pages type.
162 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
163 */
164 private function fb_pixel_purchace_complete( $sellkit_page_meta ) {
165 if ( 'thankyou' !== $sellkit_page_meta ) {
166 return;
167 }
168
169 $order_content = $this->get_order_data();
170
171 $fb_events_script =
172 "<script type='text/javascript'>
173 fbq( 'track', 'Purchase', $order_content );
174 </script>";
175
176 return $fb_events_script;
177 }
178
179 /**
180 * Get cart data.
181 *
182 * @since 1.1.0
183 * @param string $event Filter fb pixel tracking id.
184 */
185 private function get_cart_data( $event ) {
186 $cart_data = [];
187 $products = [];
188
189 $cart_total = WC()->cart->cart_contents_total + WC()->cart->tax_total;
190
191 $products = $this->get_products_data( WC()->cart->get_cart(), 'fb' );
192 $product_name = isset( $products['products_name'] ) ? substr( $products['products_name'], 2 ) : '';
193 $category_name = isset( $products['categories_name'] ) ? substr( $products['categories_name'], 2 ) : '';
194 $content_name = isset( $products['cart_contents'] ) ? wp_json_encode( $products['cart_contents'] ) : '';
195
196 $cart_data = [
197 'content_type' => 'product',
198 'plugin' => 'Sellkit',
199 'value' => number_format( floatval( $cart_total ), wc_get_price_decimals(), '.', '' ),
200 'content_name' => $product_name,
201 'content_category' => $category_name,
202 'contents' => $content_name,
203 'currency' => get_woocommerce_currency(),
204 'user_roles' => implode( ', ', wp_get_current_user()->roles ),
205 ];
206
207 if ( 'add_to_cart' === $event ) {
208 $cart_items_count = WC()->cart->get_cart_contents_count();
209
210 $cart_data = array_merge(
211 $cart_data,
212 [
213 'num_items' => $cart_items_count,
214 'domain' => get_site_url(),
215 'language' => get_bloginfo( 'language' ),
216 'userAgent' => wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), //phpcs:ignore
217 ]
218 );
219
220 return wp_json_encode( $cart_data );
221 }
222
223 return wp_json_encode( $cart_data );
224 }
225
226 /**
227 * Get purchase data.
228 *
229 * @since 1.1.0
230 */
231 private function get_order_data() {
232 if ( empty( self::$order_key ) ) {
233 return;
234 }
235
236 $order_id = wc_get_order_id_by_order_key( self::$order_key );
237 $order = wc_get_order( $order_id );
238 $products = $this->get_products_data( $order->get_items(), 'fb' );
239
240 $purchase_data = [
241 'transaction_id' => $order_id,
242 'content_type' => 'product',
243 'currency' => $order->get_currency(),
244 'userAgent' => wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), //phpcs:ignore
245 'plugin' => 'Sellkit',
246 'value' => number_format( $order->get_total(), wc_get_price_decimals(), '.', '' ),
247 ];
248
249 if ( ! empty( $products ) ) {
250 $purchase_data = array_merge(
251 $purchase_data,
252 [
253 'content_ids' => $products['content_ids'],
254 'content_names' => $products['products_name'],
255 'content_category' => $products['categories_name'],
256 ]
257 );
258 }
259
260 return wp_json_encode( $purchase_data );
261 }
262 }
263
264 Facebook::get_instance();
265