PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.3.2
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.3.2
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 1.3.2, at includes/admin/settings/integration/google-integration.php

262 lines 6.6 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;
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 $sellkit_page_meta = $post_meta[0]['type']['key'];
94 $google_events_script = '';
95
96 foreach ( $google_events as $event ) {
97 $function_name = "google_analytics_{$event}";
98
99 $google_events_script .= $this->$function_name( $sellkit_page_meta, $tracking_id );
100 }
101
102 return $google_events_script;
103 }
104
105 /**
106 * Get google analyticts begin_checkout data.
107 *
108 * @since 1.1.0
109 * @param string $sellkit_page_meta sellkit pages type.
110 * @param string $tracking_id Filter google tracking id.
111 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
112 */
113 private function google_analytics_begin_checkout( $sellkit_page_meta, $tracking_id ) {
114 if ( 'checkout' !== $sellkit_page_meta ) {
115 return;
116 }
117
118 $cart_content = $this->get_cart_data( $tracking_id );
119
120 $google_events_script =
121 "<script type='text/javascript'>
122 gtag( 'event', 'begin_checkout', $cart_content );
123 </script>";
124
125 return $google_events_script;
126 }
127
128 /**
129 * Get google analyticts add_to_cart data.
130 *
131 * @since 1.1.0
132 * @param string $sellkit_page_meta sellkit pages type.
133 * @param string $tracking_id Filter google tracking id.
134 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
135 */
136 private function google_analytics_add_to_cart( $sellkit_page_meta, $tracking_id ) {
137 if ( 'checkout' !== $sellkit_page_meta ) {
138 return;
139 }
140
141 $cart_content = $this->get_cart_data( $tracking_id );
142
143 $google_events_script =
144 "<script type='text/javascript'>
145 gtag( 'event', 'add_to_cart', $cart_content );
146 </script>";
147
148 return $google_events_script;
149 }
150
151 /**
152 * Get google analyticts add_payment_info data.
153 *
154 * @since 1.1.0
155 * @param string $sellkit_page_meta sellkit pages type.
156 * @param string $tracking_id Filter google tracking id.
157 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
158 */
159 private function google_analytics_add_payment_info( $sellkit_page_meta, $tracking_id ) {
160 if ( 'checkout' !== $sellkit_page_meta ) {
161 return;
162 }
163
164 $cart_content = $this->get_cart_data( $tracking_id );
165
166 self::$localized_data['GoogleAddPaymentInfo'] = $cart_content;
167 }
168
169 /**
170 * Get google analyticts purchase data.
171 *
172 * @since 1.1.0
173 * @param string $sellkit_page_meta sellkit pages type.
174 * @param string $tracking_id Filter google tracking id.
175 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
176 */
177 private function google_analytics_purchase( $sellkit_page_meta, $tracking_id ) {
178 if ( 'thankyou' !== $sellkit_page_meta ) {
179 return;
180 }
181
182 $cart_content = $this->get_order_data( $tracking_id );
183
184 $google_events_script =
185 "<script type='text/javascript'>
186 gtag( 'event', 'purchase', $cart_content );
187 </script>";
188
189 return $google_events_script;
190 }
191
192 /**
193 * Get order data.
194 *
195 * @since 1.1.0
196 * @param string $tracking_id Filter google tracking id.
197 */
198 private function get_order_data( $tracking_id ) {
199 $order_data = [];
200 $products = [];
201
202 if ( empty( self::$order_key ) ) {
203 return;
204 }
205
206 $order_id = wc_get_order_id_by_order_key( self::$order_key );
207 $order = wc_get_order( $order_id );
208
209 $products = $this->get_products_data( $order->get_items(), 'google' );
210
211 $order_data = [
212 'send_to' => $tracking_id,
213 'event_category' => 'Enhanced-Ecommerce',
214 'transaction_id' => $order_id,
215 'affiliation' => get_bloginfo( 'name' ),
216 'value' => number_format( $order->get_total(), wc_get_price_decimals(), '.', '' ),
217 'currency' => $order->get_currency(),
218 'tax' => number_format( $order->get_total_tax(), wc_get_price_decimals(), '.', '' ),
219 'shipping' => number_format( $order->get_shipping_total() + $order->get_shipping_tax(), wc_get_price_decimals(), '.', '' ),
220 'coupon' => $order->get_coupon_codes(),
221 'non_interaction' => true,
222 'products' => wp_json_encode( $products ),
223 ];
224
225 return wp_json_encode( $order_data );
226 }
227
228 /**
229 * Get cart content.
230 *
231 * @since 1.1.0
232 * @param string $tracking_id Filter google tracking id.
233 */
234 private function get_cart_data( $tracking_id ) {
235 if ( ! sellkit()->has_valid_dependencies() ) {
236 return wp_json_encode( [] );
237 }
238
239 $products_in_cart = WC()->cart->get_cart();
240 $products = [];
241 $cart_data = [];
242
243 $products = $this->get_products_data( $products_in_cart, 'google' );
244
245 $price = WC()->cart->cart_contents_total + WC()->cart->tax_total;
246
247 $cart_data = [
248 'send_to' => $tracking_id,
249 'event_category' => 'Enhanced-Ecommerce',
250 'currency' => get_woocommerce_currency(),
251 'coupon' => WC()->cart->get_applied_coupons(),
252 'value' => number_format( floatval( $price ), wc_get_price_decimals(), '.', '' ),
253 'products' => wp_json_encode( $products ),
254 'non_interaction' => true,
255 ];
256
257 return wp_json_encode( $cart_data );
258 }
259 }
260
261 Google::get_instance();
262