-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
404 lines (362 loc) · 13.3 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
* @file
* Contains \Netzstrategen\ShopAnalytics\Plugin.
*/
namespace Netzstrategen\ShopAnalytics;
/**
* Main front-end functionality.
*/
class Plugin {
/**
* Prefix for naming.
*
* @var string
*/
const PREFIX = 'shop_analytics';
/**
* Gettext localization domain.
*
* @var string
*/
const L10N = self::PREFIX;
/**
* @var string
*/
private static $baseUrl;
/**
* @implements init
*/
public static function init() {
if (is_admin()) {
// @see Admin::init()
return;
}
if (static::isContainerEnabled()) {
add_action('wp_head', __CLASS__ . '::wp_head', 1);
add_action('wp_footer', __CLASS__ . '::wp_footer', 1);
}
// Adds global site and user context attributes to HTML tag.
add_filter('language_attributes', __CLASS__ . '::language_attributes', 99);
if (static::isEcommerceTrackingEnabled()) {
// Add products details as data attributes to remove item from cart link.
add_filter('woocommerce_cart_item_remove_link', __NAMESPACE__ . '\WooCommerce::woocommerce_cart_item_remove_link', 10, 2);
// Add a hidden HTML div element with product details as data attributes.
add_action('woocommerce_shop_loop_item_title', __NAMESPACE__ . '\WooCommerce::addImpressionsProductDetailsHtmlDataAttr');
add_action('woocommerce_single_product_summary', __NAMESPACE__ . '\WooCommerce::addSingleProductDetailsHtmlDataAttr');
add_action('woocommerce_thankyou', __NAMESPACE__ . '\WooCommerce::addOrderDetailsHtmlDataAttr');
// Adds hidden fields with data related to product variations with a custom product name set.
add_action('woocommerce_after_single_variation', __NAMESPACE__ . '\WooCommerce::woocommerce_after_single_variation');
// Enqueue Google Analytics Data Layer related scripts.
add_action('wp_enqueue_scripts', __CLASS__ . '::enqueueGaDataLayerScripts');
}
}
/**
* Returns whether basic GTM tracking is enabled or not.
*/
public static function isContainerEnabled() {
return get_option('shop_analytics_gtm_embed') && get_option('shop_analytics_gtm_id');
}
/**
* @implements wp_head
*/
public static function wp_head() {
if (!$gtm_id = get_option('shop_analytics_gtm_id')) {
return;
}
?>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','<?= $gtm_id ?>');</script>
<?php
}
/**
* @implements wp_footer
*/
public static function wp_footer() {
if (!$gtm_id = get_option('shop_analytics_gtm_id')) {
return;
}
?>
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?= $gtm_id ?>" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<?php
}
/**
* @implements language_attributes
*/
public static function language_attributes($html_attributes) {
global $wp_query;
$attributes = [];
// Adds language code data attribute.
$attributes['language'] = static::getLanguageCode();
// Adds user id data attribute.
if (get_option('shop_analytics_track_user_id') && is_user_logged_in()) {
$attributes['user-id'] = static::getCurrentUserID();
}
// Adds user role data attribute.
if (get_option('shop_analytics_track_user_role')) {
$attributes['user-type'] = static::getCurrentUserRole();
}
// Adds user tracking status attribute. Exclude the current user role from tracking if option is set.
$attributes['user-track'] = (int) !in_array(static::getCurrentUserRole(), static::getDisabledUserRoles(), TRUE);
// Adds page type data attribute.
if ($wp_query->queried_object) {
$postId = $wp_query->queried_object->ID ?? 0;
$attributes['page-type'] = static::getPageType($postId);
}
// Adds shop market code. Set to the the plugin settings default value if undefined.
$attributes['market'] = get_option('shop_analytics_market_default') ?: 'GLOBAL';
if (static::isEcommerceTrackingEnabled()) {
// Adds currency code data attribute.
$attributes['currency'] = get_woocommerce_currency();
$is_tax = FALSE;
if (is_product_category() && $category_page_path = WooCommerce::getProductCategoryParents($wp_query->queried_object->term_id, ' > ')) {
$is_tax = TRUE;
// Adds product category page path attribute.
$attributes['product-category'] = $category_page_path;
}
elseif (is_product_tag() && $wp_query->queried_object->name) {
$is_tax = TRUE;
// Adds product tag attribute.
$attributes['product-tag'] = $wp_query->queried_object->name;
}
elseif (WooCommerce::isAttribute() && $wp_query->queried_object->name) {
$is_tax = TRUE;
// Adds product attribute page attribute.
$attributes['product-attribute'] = $wp_query->queried_object->name;
}
elseif (is_product() && $product_details = WooCommerce::getProductDetails(get_the_ID(), FALSE)) {
// Adds product details and attributes.
foreach ($product_details as $key => $detail) {
$attributes['product-' . str_replace('_', '-', $key)] = $detail;
}
foreach (WooCommerce::getProductAttributes(wc_get_product($product_details['id'])) as $attribute) {
$attributes['product-' . $attribute['key']] = $attribute['value'] ?: '';
}
}
// Adds products count in taxonomy attribute.
if ($is_tax) {
$attributes['product-count'] = $wp_query->queried_object->count;
}
}
return $html_attributes . ' ' . static::buildAttributesDataTags($attributes);
}
/**
* Returns site frontend language code. If WPML plugin is active, its language setting gets precedence.
*
* @return string
*/
public static function getLanguageCode() {
$language = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : get_bloginfo('language');
return substr($language, 0, 2);
}
/**
* Returns the first role of the current user. 'Visitor' otherwise.
*/
public static function getCurrentUserRole() {
// Probably you should check against all of the users roles?
$user = wp_get_current_user();
return $user->roles ? $user->roles[0] : 'visitor';
}
/**
* Returns the wordpress user ID or hashed e-mail address of the current user.
*/
public static function getCurrentUserID() {
// Probably change the hash method to get shorter hashes.
$user = wp_get_current_user();
return get_option('shop_analytics_user_id_email') ? sha1($user->user_email) : $user->ID;
}
/**
* Returns the user roles which should not be tracked.
*/
public static function getDisabledUserRoles() {
return get_option('shop_analytics_disable_user_tracking') ? get_option('shop_analytics_disable_user_tracking') : [];
}
/**
* Returns current page type.
*
* @param int $post_id
* Current page post ID.
*
* @return string
*/
public static function getPageType($post_id) {
if (Plugin::isEcommerceTrackingEnabled() && $page_type = WooCommerce::getPageType($post_id)) {
return $page_type;
}
elseif (is_front_page()) {
return 'Home';
}
elseif (is_page()) {
return 'Page';
}
elseif (is_single()) {
return 'Post';
}
elseif (is_tag()) {
return 'Tag';
}
elseif (is_category()) {
return 'Category';
}
elseif (is_search()) {
return 'Search';
}
else {
return 'Other';
}
}
/**
* Returns TRUE if WooCommerce plugin is installed and activated.
*
* @return bool
*/
public static function isWooCommerceActive() {
return class_exists('WooCommerce');
}
/**
* Returns TRUE if WooCommerce plugin is active and ecommerce tracking enabled.
*
* @return bool
*/
public static function isEcommerceTrackingEnabled() {
return static::isWooCommerceActive() && get_option('shop_analytics_track_ecommerce');
}
/**
* The base URL path to this plugin's folder.
*
* Uses plugins_url() instead of plugin_dir_url() to avoid a trailing slash.
*/
public static function getBaseUrl() {
if (!isset(static::$baseUrl)) {
static::$baseUrl = plugins_url('', static::getBasePath() . '/plugin.php');
}
return static::$baseUrl;
}
/**
* Loads Google Analytics Data Layer related scripts.
*/
public static function enqueueGaDataLayerScripts() {
global $wp;
$handle = Plugin::PREFIX . '_datalayer';
$source = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '/assets' : '/dist';
$scripts = static::getBaseUrl() . $source . '/scripts/datalayer';
$checkout_step_prefix = 'shop_analytics_checkout_step_';
$checkout_step = 0;
$wc_checkout_steps = apply_filters('shop_analytics_checkout_steps', [
'view_cart' => 10,
'checkout_page' => 20,
'checkout_pay_page' => 30,
]);
$wc_endpoints = apply_filters('shop_analytics_wc_endpoints', [
'view-order' => 10,
'edit-account' => 20,
'edit-address' => 30,
'lost-password' => 40,
'customer-logout' => 50,
'add-payment-method' => 60,
'order-pay' => 70,
'order-received' => 80,
]);
wp_enqueue_script($handle . '_common', "$scripts/common.js", ['jquery'], FALSE, TRUE);
wp_localize_script($handle . '_common', Plugin::PREFIX . '_settings', [
'tc_enabled' => (bool) get_option('shop_analytics_tc_enabled'),
'datalayer_console_log' => (int) get_option('shop_analytics_datalayer_logging') ? 'on' : 'off',
]);
wp_enqueue_script($handle . '_cart_checkout', "$scripts/cart-checkout.js", [$handle . '_common'], FALSE, TRUE);
if (is_cart() || is_checkout()) {
if (is_cart()) {
$checkout_step = apply_filters($checkout_step_prefix . '_view_cart', $wc_checkout_steps['view_cart']);
}
if (is_checkout_pay_page() && !empty($wc_checkout_steps['checkout_pay'])) {
$checkout_step = apply_filters($checkout_step_prefix . '_checkout_pay', $wc_checkout_steps['checkout_pay']);
}
elseif (is_checkout() && !empty($wc_checkout_steps['checkout_page'])) {
$checkout_step = apply_filters($checkout_step_prefix . '_checkout_page', $wc_checkout_steps['checkout_page']);
}
}
elseif (isset($wp->query_vars['pagename'])) {
$checkout_step = $wc_checkout_steps[$wp->query_vars['pagename']] ?? 0;
}
if ($checkout_step) {
wp_localize_script($handle . '_cart_checkout', Plugin::PREFIX . '_checkout_steps', [
'order' => apply_filters($checkout_step_prefix . 'current', $checkout_step),
]);
}
if (is_wc_endpoint_url()) {
wp_enqueue_script($handle . '_endpoints', "$scripts/endpoints.js", [$handle . '_common'], FALSE, TRUE);
// Inject woocoomerce endpoint identifier into frontend.
foreach ($wc_endpoints as $endpoint => $order) {
if (is_wc_endpoint_url($endpoint)) {
break;
}
}
wp_localize_script($handle . '_endpoints', Plugin::PREFIX . '_endpoint_data', [
'step' => $endpoint,
'order' => $order,
]);
}
if (is_product()) {
wp_enqueue_script($handle . '_product', "$scripts/product.js", [$handle . '_common'], FALSE, TRUE);
}
}
/**
* Builds a string containing HTML data tags with the product attributes.
*
* @param array $attributes
* Product attributes.
*
* @return string
* Product attributes as HTML data tags.
*/
public static function buildAttributesDataTags(array $attributes) {
array_walk($attributes, function (&$value, $key) {
if (is_array($value)) {
$value = implode(',', $value);
}
if (!is_null($value) && '' !== trim($value)) {
$value = 'data-' . $key . '="' . esc_attr($value) . '"';
}
});
return implode(' ', $attributes);
}
/**
* The absolute filesystem base path of this plugin.
*
* @return string
*/
public static function getBasePath() {
return dirname(__DIR__);
}
/**
* Renders a given plugin template, optionally overridden by the theme.
*
* WordPress offers no built-in function to allow plugins to render templates
* with custom variables, respecting possibly existing theme template overrides.
* Inspired by Drupal (5-7).
*
* @param array $template_subpathnames
* An prioritized list of template (sub)pathnames within the plugin/theme to
* discover; the first existing wins.
* @param array $variables
* An associative array of template variables to provide to the template.
*
* @throws \InvalidArgumentException
* If none of the $template_subpathnames files exist in the plugin itself.
*/
public static function renderTemplate(array $template_subpathnames, array $variables = []) {
$template_pathname = locate_template($template_subpathnames, FALSE, FALSE);
extract($variables, EXTR_SKIP | EXTR_REFS);
if ($template_pathname !== '') {
include $template_pathname;
}
else {
while ($template_pathname = current($template_subpathnames)) {
if (file_exists($template_pathname = static::getBasePath() . '/' . $template_pathname)) {
include $template_pathname;
return;
}
next($template_subpathnames);
}
throw new \InvalidArgumentException("Missing template '$template_pathname'");
}
}
}