PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
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 / google-integration.php

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

264 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 Google Analytics integration.
9 *
10 * @package Sellkit\Admin\Settings\Integration\Settings_Integration
11 * @since 1.1.0
12 */
13 class Google 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 google analyticts scripts.
49 *
50 * @since 1.1.0
51 */
52 public function analytics() {
53 $google_tracking_id = esc_attr( sellkit_get_option( 'google_analytics_id' ) );
54
55 if ( empty( $google_tracking_id ) || empty( sellkit_get_option( 'google_analytics' ) ) ) {
56 return;
57 }
58
59 // phpcs:disable
60 $google_script =
61 '<!-- Google Analytics Script By Sellkit -->
62 <script async src="https://www.googletagmanager.com/gtag/js?id=' . esc_js( $google_tracking_id ) . '"></script>
63
64 <script>
65 window.dataLayer = window.dataLayer || [];
66 function gtag(){dataLayer.push(arguments);}
67 gtag( "js", new Date() );
68 gtag("config","' . esc_js( $google_tracking_id ) . '");
69 </script>
70 <!-- Google Analytics Script By Sellkit -->
71 ';
72 // phpcs:enable
73
74 $google_script .= $this->google_analytics_events( self::$post, $google_tracking_id );
75
76 echo $google_script; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Inline analytics script; tracking IDs escaped with esc_js.
77 }
78
79 /**
80 * Get google analyticts events scripts.
81 *
82 * @since 1.1.0
83 * @param array $post_meta sellkit pages data.
84 * @param string $tracking_id google tracking id.
85 */
86 private function google_analytics_events( $post_meta, $tracking_id ) {
87 $google_events = sellkit_get_option( 'google_analytics_events' );
88
89 if ( empty( $google_events ) ) {
90 return;
91 }
92
93 $first_index = array_key_first( $post_meta );
94
95 $sellkit_page_meta = $post_meta[ $first_index ]['type']['key'];
96 $google_events_script = '';
97
98 foreach ( $google_events as $event ) {
99 $function_name = "google_analytics_{$event}";
100
101 $google_events_script .= $this->$function_name( $sellkit_page_meta, $tracking_id );
102 }
103
104 return $google_events_script;
105 }
106
107 /**
108 * Get google analyticts begin_checkout data.
109 *
110 * @since 1.1.0
111 * @param string $sellkit_page_meta sellkit pages type.
112 * @param string $tracking_id Filter google tracking id.
113 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
114 */
115 private function google_analytics_begin_checkout( $sellkit_page_meta, $tracking_id ) {
116 if ( 'checkout' !== $sellkit_page_meta ) {
117 return;
118 }
119
120 $cart_content = $this->get_cart_data( $tracking_id );
121
122 $google_events_script =
123 "<script type='text/javascript'>
124 gtag( 'event', 'begin_checkout', $cart_content );
125 </script>";
126
127 return $google_events_script;
128 }
129
130 /**
131 * Get google analyticts add_to_cart data.
132 *
133 * @since 1.1.0
134 * @param string $sellkit_page_meta sellkit pages type.
135 * @param string $tracking_id Filter google tracking id.
136 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
137 */
138 private function google_analytics_add_to_cart( $sellkit_page_meta, $tracking_id ) {
139 if ( 'checkout' !== $sellkit_page_meta ) {
140 return;
141 }
142
143 $cart_content = $this->get_cart_data( $tracking_id );
144
145 $google_events_script =
146 "<script type='text/javascript'>
147 gtag( 'event', 'add_to_cart', $cart_content );
148 </script>";
149
150 return $google_events_script;
151 }
152
153 /**
154 * Get google analyticts add_payment_info data.
155 *
156 * @since 1.1.0
157 * @param string $sellkit_page_meta sellkit pages type.
158 * @param string $tracking_id Filter google tracking id.
159 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
160 */
161 private function google_analytics_add_payment_info( $sellkit_page_meta, $tracking_id ) {
162 if ( 'checkout' !== $sellkit_page_meta ) {
163 return;
164 }
165
166 $cart_content = $this->get_cart_data( $tracking_id );
167
168 self::$localized_data['GoogleAddPaymentInfo'] = $cart_content;
169 }
170
171 /**
172 * Get google analyticts purchase data.
173 *
174 * @since 1.1.0
175 * @param string $sellkit_page_meta sellkit pages type.
176 * @param string $tracking_id Filter google tracking id.
177 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
178 */
179 private function google_analytics_purchase( $sellkit_page_meta, $tracking_id ) {
180 if ( 'thankyou' !== $sellkit_page_meta ) {
181 return;
182 }
183
184 $cart_content = $this->get_order_data( $tracking_id );
185
186 $google_events_script =
187 "<script type='text/javascript'>
188 gtag( 'event', 'purchase', $cart_content );
189 </script>";
190
191 return $google_events_script;
192 }
193
194 /**
195 * Get order data.
196 *
197 * @since 1.1.0
198 * @param string $tracking_id Filter google tracking id.
199 */
200 private function get_order_data( $tracking_id ) {
201 $order_data = [];
202 $products = [];
203
204 if ( empty( self::$order_key ) ) {
205 return;
206 }
207
208 $order_id = wc_get_order_id_by_order_key( self::$order_key );
209 $order = wc_get_order( $order_id );
210
211 $products = $this->get_products_data( $order->get_items(), 'google' );
212
213 $order_data = [
214 'send_to' => $tracking_id,
215 'event_category' => 'Enhanced-Ecommerce',
216 'transaction_id' => $order_id,
217 'affiliation' => get_bloginfo( 'name' ),
218 'value' => number_format( $order->get_total(), wc_get_price_decimals(), '.', '' ),
219 'currency' => $order->get_currency(),
220 'tax' => number_format( $order->get_total_tax(), wc_get_price_decimals(), '.', '' ),
221 'shipping' => number_format( $order->get_shipping_total() + $order->get_shipping_tax(), wc_get_price_decimals(), '.', '' ),
222 'coupon' => $order->get_coupon_codes(),
223 'non_interaction' => true,
224 'products' => wp_json_encode( $products ),
225 ];
226
227 return wp_json_encode( $order_data );
228 }
229
230 /**
231 * Get cart content.
232 *
233 * @since 1.1.0
234 * @param string $tracking_id Filter google tracking id.
235 */
236 private function get_cart_data( $tracking_id ) {
237 if ( ! sellkit()->has_valid_dependencies() ) {
238 return wp_json_encode( [] );
239 }
240
241 $products_in_cart = WC()->cart->get_cart();
242 $products = [];
243 $cart_data = [];
244
245 $products = $this->get_products_data( $products_in_cart, 'google' );
246
247 $price = WC()->cart->cart_contents_total + WC()->cart->tax_total;
248
249 $cart_data = [
250 'send_to' => $tracking_id,
251 'event_category' => 'Enhanced-Ecommerce',
252 'currency' => get_woocommerce_currency(),
253 'coupon' => WC()->cart->get_applied_coupons(),
254 'value' => number_format( floatval( $price ), wc_get_price_decimals(), '.', '' ),
255 'products' => wp_json_encode( $products ),
256 'non_interaction' => true,
257 ];
258
259 return wp_json_encode( $cart_data );
260 }
261 }
262
263 Google::get_instance();
264