class-dashboard-switcher-tracking.php
4.91 KB
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
<?php
/**
* Quick switcher tracking file.
*
* @package automattic/jetpack
*/
namespace Automattic\Jetpack\Dashboard_Customizations;
use Automattic\Jetpack\Status\Host;
use Automattic\Jetpack\Terms_Of_Service;
use Automattic\Jetpack\Tracking;
use Jetpack_Plan;
/**
* Class Dashboard_Switcher_Tracking
*/
class Dashboard_Switcher_Tracking {
/**
* Jetpack Tracking library will prefix the event name with "jetpack_*" automatically.
*/
const JETPACK_EVENT_NAME = 'dashboard_quick_switch_link_clicked';
const WPCOM_EVENT_NAME = 'wpcom_dashboard_quick_switch_link_clicked';
/**
* Jetpack tracking object.
*
* @var Tracking
*/
private $tracking;
/**
* Current site plan.
*
* @var string
*/
private $plan;
/**
* The wpcom_tracks wrapper function.
*
* @var callable
*/
private $wpcom_tracking;
/**
* Dashboard_Switcher_Tracking constructor.
*
* @param Tracking $tracking Jetpack tracking object.
* @param callable $wpcom_tracking A wrapper over wpcom event record.
* @param string $plan The current site plan.
*/
public function __construct( Tracking $tracking, callable $wpcom_tracking, $plan ) {
$this->tracking = $tracking;
$this->plan = $plan;
$this->wpcom_tracking = $wpcom_tracking;
}
/**
* Create an event for the Quick switcher when the user changes it's preferred view.
*
* @param string $screen The screen page.
* @param string $view The new preferred view.
*/
public function record_switch_event( $screen, $view ) {
$event_props = array(
'current_page' => $screen,
'destination' => $view,
'plan' => $this->plan,
);
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
$event_props['blog_id'] = get_current_blog_id();
/**
* Callable injected in the constructor with the static::wpcom_tracks_record_event() static method.
*
* @see wpcom_tracks_record_event A static method from this class that executes the actual WPCOM event record.
*/
$wpcom_tracking = $this->wpcom_tracking;
$wpcom_tracking( $event_props );
} else {
$this->record_jetpack_event( $event_props );
}
}
/**
* Get the current site plan or 'N/A' when we cannot determine site's plan.
*
* @todo: This method can be reused as a wrapper over WPCOM and Atomic as way to get site's current plan (display name).
*
* @return string
*/
public static function get_plan() {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
if ( class_exists( '\WPCOM_Store_API' ) ) {
// @todo: Maybe introduce a wrapper for this since we are duplicating it from WPCOM_Admin_Menu:253
$products = \WPCOM_Store_API::get_current_plan( \get_current_blog_id() );
if ( ! empty( $products['product_slug'] ) ) {
return $products['product_slug'];
}
}
return 'N/A'; // maybe we should return free or null? At the moment it's safe to return 'N/A' since we use it only for passing it to the event.
}
// @todo: Maybe introduce a helper for this since we are duplicating it from Atomic_Admin_Menu:240
$products = Jetpack_Plan::get();
if ( ! empty( $products['product_slug'] ) ) {
return $products['product_slug'];
}
return 'N/A'; // maybe we should return free or null? At the moment we use it for passing it to the event.
}
/**
* Record the event with Jetpack implementation.
*
* For Atomic sites we mark the Jetpack ToS option temporary as read.
*
* @todo Remove the jetpack_options_tos_agreed filter for Atomic sites after the Tracking is properly working for AT sites.
*
* @param array $event_properties The event properties.
*/
private function record_jetpack_event( $event_properties ) {
$woa = ( new Host() )->is_woa_site();
if ( $woa ) {
add_filter( 'jetpack_options', array( __CLASS__, 'mark_jetpack_tos_as_read' ), 10, 2 );
}
$this->tracking->record_user_event( self::JETPACK_EVENT_NAME, $event_properties );
if ( $woa ) {
\remove_filter( 'jetpack_options', array( __CLASS__, 'mark_jetpack_tos_as_read' ) );
}
}
/**
* Trigger the WPCOM tracks_record_event.
*
* @param array $event_props Event props.
*/
public static function wpcom_tracks_record_event( $event_props ) {
require_lib( 'tracks/client' );
\tracks_record_event( \wp_get_current_user(), self::WPCOM_EVENT_NAME, $event_props );
}
/**
* Get the tracking product name for the Tracking library.
*
* The tracking product name is used by the Tracking as a prefix for the event name.
*
* @return string
*/
public static function get_jetpack_tracking_product() {
return ( new Host() )->is_woa_site() ? 'atomic' : 'jetpack';
}
/**
* Mark the Jetpack ToS as read for Atomic Sites.
*
* @param mixed $option_value The value of the Jetpack option.
* @param string $option_name The name of the Jetpack option.
*
* @return bool
*/
public static function mark_jetpack_tos_as_read( $option_value, $option_name ) {
if ( Terms_Of_Service::OPTION_NAME === $option_name ) {
return true;
}
return $option_value;
}
}