Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
26.42% covered (danger)
26.42%
14 / 53
28.57% covered (danger)
28.57%
2 / 7
CRAP
n/a
0 / 0
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Wpadmin_Page_View\wpcom_nosara_track_admin_page_views
29.17% covered (danger)
29.17%
7 / 24
0.00% covered (danger)
0.00%
0 / 1
37.79
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Wpadmin_Page_View\wpcom_maybe_track_customizer
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Wpadmin_Page_View\wpcom_track_customizer_from_frontend
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Wpadmin_Page_View\do_not_track_a11ns
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Wpadmin_Page_View\wpcom_atomic_maybe_is_a11n
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Wpadmin_Page_View\wpcom_atomic_get_user_types
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Wpadmin_Page_View\get_account_age_in_days
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * WPCom Tracking for WP Admin Page Views.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Wpadmin_Page_View;
9
10use WPCOMSH_Support_Session_Detect;
11
12/**
13 * Bump a Tracks stat for every wp-admin page view not generated by an Automattician.
14 * This is possibly interesting on its own but will also be used as part of an
15 * initiative to understand how much supply we're offering for live chat demand.
16 * See https://nosaraproject.wordpress.com/2015/03/06/live-chat-stats/
17 */
18function wpcom_nosara_track_admin_page_views() {
19    if ( do_not_track_a11ns() ) {
20        return;
21    }
22
23    $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM;
24    $is_atomic_site = ! $is_simple_site;
25
26    global $current_user, $current_blog, $current_screen;
27
28    if (
29        ! $current_user instanceof \WP_User ||
30        ! $current_screen instanceof \WP_Screen
31    ) {
32        return;
33    }
34
35    $blog_id    = null;
36    $user_types = array();
37
38    if ( $is_simple_site ) {
39        if ( ! $current_blog instanceof \WP_Site ) {
40            return;
41        }
42
43        $blog_id    = $current_blog->blog_id;
44        $user_types = \WPCOM_User::get_types();
45    }
46
47    if ( $is_atomic_site ) {
48        $blog_id    = _wpcom_get_current_blog_id();
49        $user_types = wpcom_atomic_get_user_types();
50    }
51    ?>
52    <script type="text/javascript">
53        var _admin_pv_props = {
54            from_page: '<?php echo esc_js( $current_screen->id ); ?>',
55            is_block_editor: '<?php echo $current_screen->is_block_editor ? 'true' : 'false'; ?>',
56            source: 'wp-admin',
57            blog_id: '<?php echo esc_js( (string) $blog_id ); ?>',
58            user_type: '<?php echo esc_js( implode( ',', $user_types ) ); ?>'
59        };
60        _tkq = window._tkq || [];
61        _tkq.push( [ 'identifyUser', <?php echo (int) $current_user->ID; ?>, '<?php echo esc_js( $current_user->user_login ); ?>' ] );
62        _tkq.push( [ 'recordEvent', 'wpcom_admin_page_view', _admin_pv_props ] );
63    </script>
64    <?php
65}
66add_action( 'admin_footer', __NAMESPACE__ . '\wpcom_nosara_track_admin_page_views' );
67
68/**
69 * Track non-calypso Customizer views if the user is linked from the frontend.
70 */
71function wpcom_maybe_track_customizer() {
72    // not when the Customizer is loaded in Calypso
73    if ( isset( $_GET['calypso'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
74        return;
75    }
76
77    // links from frontend
78    if ( isset( $_GET['url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
79        return wpcom_track_customizer_from_frontend();
80    }
81
82    // standard wp-admin
83    wpcom_nosara_track_admin_page_views();
84}
85
86/**
87 * Since links to the Customizer from the frontend aren't properly wp-admin (no wp-admin chrome,
88 * you return to the frontend after closing), they shouldn't be counted as wp-admin views.
89 */
90function wpcom_track_customizer_from_frontend() {
91    global $current_user;
92
93    // Since we view so many screens to offer support and do feature dev, I think it's best not to count us.
94    if ( do_not_track_a11ns() ) {
95        return;
96    }
97    ?>
98    <script type="text/javascript">
99        _tkq = window._tkq || [];
100        _tkq.push( [ 'identifyUser', <?php echo (int) $current_user->ID; ?>, '<?php echo esc_js( $current_user->user_login ); ?>' ] );
101        _tkq.push( [ 'recordEvent', 'wpcom_customize_loaded_from_frontend' ] );
102    </script>
103    <?php
104}
105add_action( 'customize_controls_print_footer_scripts', __NAMESPACE__ . '\wpcom_maybe_track_customizer' );
106
107/**
108 * Determine if the current user should not be tracked.
109 *
110 * @return bool
111 */
112function do_not_track_a11ns() {
113    $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM;
114
115    if ( $is_simple_site ) {
116        return is_automattician() || is_network_admin();
117    }
118
119    return wpcom_atomic_maybe_is_a11n();
120}
121
122/**
123 * Check if the user might be an a11n on Atomic sites.
124 *
125 * @return bool
126 */
127function wpcom_atomic_maybe_is_a11n() {
128    $is_proxy_atomic    = defined( 'AT_PROXIED_REQUEST' ) && AT_PROXIED_REQUEST;
129    $is_support_session = WPCOMSH_Support_Session_Detect::is_probably_support_session();
130
131    return $is_proxy_atomic && ! $is_support_session;
132}
133
134/**
135 * Retrieves the user types for Atomic sites.
136 *
137 * @return array An array of user types.
138 */
139function wpcom_atomic_get_user_types() {
140    $user_types = array();
141
142    if ( get_account_age_in_days() <= 14 ) {
143        $user_types[] = 'New User';
144    }
145
146    $subscriptions = wpcomsh_get_wpcom_active_subscriptions();
147    if ( count( $subscriptions ) > 0 ) {
148        $user_types[] = 'Paid';
149    }
150
151    if ( wpcom_site_has_feature( \WPCOM_Features::PRIORITY_SUPPORT ) ) {
152        $user_types[] = 'Business';
153    }
154
155    return $user_types;
156}
157
158/**
159 * Calculate the number of days since the current user registered their account.
160 *
161 * @global WP_User $current_user The current user object.
162 *
163 * @return float The age of the account in days.
164 */
165function get_account_age_in_days() {
166    global $current_user;
167
168    return ( time() - strtotime( $current_user->user_registered ) ) / DAY_IN_SECONDS;
169}