This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgc-segment.php
305 lines (228 loc) · 9.2 KB
/
cgc-segment.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
<?php
/**
* Plugin Name: CG Cookie Segment.io Tracking
* Description: Sends CGC data to segment.io
* Author: Jonathan Williamson
* Author URI: http://cgcookie.com
* Version: 1.5.1
*/
$plugin_url = WP_PLUGIN_URL . '/cgc-segment';
$options = array();
class cgcSegment {
function __construct(){
$options = get_option( 'cgc_segment' );
if( $options != '' ) {
$WRITE_KEY = $options['cgc_segment_write_key'];
}
require_once dirname( __FILE__ ) . "/analytics-php/lib/Segment.php";
if( class_exists( 'Easy_Digital_Downloads' ) ) {
require_once dirname( __FILE__ ) . "/includes/edd_events.php";
require_once dirname( __FILE__ ) . "/includes/edd_pageviews.php";
}
if( function_exists( 'rcp_is_active' ) ) {
require_once dirname( __FILE__ ) . "/includes/rcp.php";
}
require_once dirname( __FILE__ ) . "/includes/pageviews.php";
require_once dirname( __FILE__ ) . "/includes/user_actions.php";
require_once dirname( __FILE__ ) . "/includes/user_login.php";
require_once dirname( __FILE__ ) . "/includes/user_registration.php";
require_once dirname( __FILE__ ) . "/includes/user_tour.php";
require_once dirname( __FILE__ ) . "/includes/optimizely_js.php";
# Setup our Segment tracking and
# alias to Analytics for convenience
class_alias('Segment', 'Analytics');
Analytics::init( $WRITE_KEY );
}
public static function identify_user( $user_id = '', $traits = array() ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$user = get_userdata( $user_id );
$registered = ( $user->user_registered . "\n" );
$is_group_member = function_exists('cgc_group_accounts') ? cgc_group_accounts()->members->is_group_member( $user_id ) : false;
$group_role = function_exists('cgc_group_accounts') ? cgc_group_accounts()->members->get_role( $user_id ) : 'member';
$group_name = function_exists('cgc_group_accounts') ? cgc_group_accounts()->members->get_group_name( $user_id ) : false;
$student_flow = function_exists('cgc_flow_is_student_in_any_flows') ? cgc_flow_is_student_in_any_flows( $user_id ) : '';
$student_flow_name = get_the_title( $student_flow[0] );
# Check for traits
if( empty( $traits ) && is_user_logged_in() ) {
$traits = array(
"firstName" => $user->first_name,
"lastName" => $user->last_name,
"email" => $user->user_email,
"username" => $user->user_login,
'createdAt' => date( "n/j/Y", strtotime( $registered ) )
);
}
// Global traits for EDU
if( function_exists('rcp_is_active') ) {
$traits['status'] = ucwords( rcp_get_status( $user_id ) );
$traits['level'] = rcp_get_subscription( $user_id );
$traits['expiration'] = rcp_get_expiration_date( $user_id );
$traits['is_trialing'] = rcp_is_trialing( $user_id );
}
if( class_exists( 'cgcUserAPI') ) {
$traits['betaUser'] = cgcUserAPI::is_user_beta_user( $user_id );
}
if( function_exists( 'affwp_is_active_affiliate' ) ) {
$traits['affiliate'] = affwp_is_active_affiliate();
}
if( $is_group_member ) {
$traits['group'] = $group_name;
$traits['groupRole'] = $group_role;
}
if( $student_flow ) {
$traits['flow'] = $student_flow_name;
}
$context = array(
'ip' => $_SERVER['REMOTE_ADDR']
);
# User data to be passed
$args = array(
"userId" => $user_id,
"traits" => $traits,
"context" => $context
);
Analytics::identify( $args );
return $args;
}
public static function identify_group( $user_id = '', $traits= array() ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
// Group membership stuff
$group_id = function_exists('cgc_group_accounts') ? cgc_group_accounts()->members->get_group_id( $user_id ) : 'null';
$group_name = function_exists('cgc_group_accounts') ? cgc_group_accounts()->members->get_group_name( $user_id ) : false;
$traits['name'] = $group_name;
Analytics::group(array(
"userId" => $user_id,
"groupId" => $group_id,
"traits" => $traits,
)
);
}
public static function track( $event = '', $user_id = '', $properties = array(), $traits = array() ) {
# If no event name is passed, return
if( empty( $event ) ) {
return false;
}
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$user_data = get_userdata( $user_id );
// Global properties for EDU
$properties['email'] = $user_data->user_email;
$properties['username'] = $user_data->user_login;
if( function_exists( 'rcp_is_active' ) ) {
$properties['status'] = ucwords( rcp_get_status( $user_id ) );
$properties['level'] = rcp_get_subscription( $user_id );
$properties['expiration'] = rcp_get_expiration_date( $user_id );
$properties['is_trialing'] = rcp_is_trialing( $user_id );
}
if( class_exists( 'cgcUserAPI') ) {
$properties['betaUser'] = cgcUserAPI::is_user_beta_user( $user_id );
}
Analytics::track(array(
"userId" => $user_id,
"event" => $event,
"traits" => $traits,
"properties" => $properties
)
);
}
public static function page( $pagename = '', $properties = array(), $traits = array() ) {
Analytics::page(array(
"userId" => is_user_logged_in() ? get_current_user_id() : session_id(),
"name" => $pagename,
"properties" => $properties
)
);
}
}
function cgc_segment_menu() {
add_options_page(
'CG Cookie Segment Event Tracking',
'CGC Segment',
'manage_options',
'cgc-segment-options',
'cgc_segment_options_page'
);
}
add_action( 'admin_menu', 'cgc_segment_menu');
function cgc_segment_options_page() {
if( !current_user_can( 'manage_options' ) ) {
wp_die( 'You do not have sufficient permissions to access this page.' );
}
global $plugin_url;
global $options;
if( isset( $_POST['cgc_segment_write_key_form_submitted'] ) ) {
$hidden_field = esc_html( $_POST['cgc_segment_write_key_form_submitted'] );
if( $hidden_field == 'Y' ) {
$cgc_segment_write_key = esc_html( $_POST['cgc_segment_write_key'] );
$options['cgc_segment_write_key'] = $cgc_segment_write_key;
update_option( 'cgc_segment', $options );
}
}
require ( 'includes/options_page_wrapper.php');
}
function cgc_segment_load() {
$options = get_option( 'cgc_segment' );
$key = $options['cgc_segment_write_key'];
if( !empty( $key ) ) {
new cgcSegment;
}
}
add_action( 'plugins_loaded', 'cgc_segment_load' );
function cgc_segment_load_scripts() {
$options = get_option( 'cgc_segment' );
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
$student_flow_id = function_exists('cgc_flow_is_student_in_any_flows') ? cgc_flow_is_student_in_any_flows( $user_id ) : '';
$student_flow_name = !empty( $student_flow_id ) ? get_the_title( $student_flow_id[0] ) : '';
$local_vars = array(
'write_key' => $options['cgc_segment_write_key'],
);
if( is_user_logged_in() ) {
$registered = ($user->user_registered . "\n");
$local_vars['userId'] = $user_id;
$local_vars["firstName"] = $user->first_name;
$local_vars["lastName"] = $user->last_name;
$local_vars["email"] = $user->user_email;
$local_vars["username"] = $user->user_login;
$local_vars["createdAt"] = date("n/j/Y", strtotime($registered));
$local_vars["userRoles"] = implode( ', ', $user->roles);
$local_vars['flow'] = $student_flow_name;
}
# Get user's interests
if( class_exists( 'CGC_Core' ) ) {
$subjects = get_user_meta( $user_id, 'learning_interests', true );
$topics = get_user_meta( $user_id, 'learning_interests_secondary', true );
$local_vars['subjects'] = !empty( $subjects ) ? implode( ', ', $subjects ) : '';
$local_vars['topics'] = !empty( $topics ) ? implode( ', ', $topics ) : '';
$local_vars['betaUser'] = class_exists('cgcUserAPI') ? cgcUserAPI::is_user_beta_user( $user_id ) : false;
$local_vars['downloadCount'] = class_exists('cgcUserAPI') ? cgcUserAPI::get_download_count( $user_id ): 'null';
}
if( function_exists( 'affwp_is_active_affiliate' ) ) {
$local_vars['affiliate'] = affwp_is_active_affiliate();
}
if( function_exists( 'rcp_get_subscription' ) ) {
$subscription = rcp_get_subscription( $user_id );
$local_vars['status'] = ucwords( rcp_get_status( $user_id ) );
$local_vars['level'] = $subscription;
$local_vars['expiration'] = rcp_get_expiration_date( $user_id );
$local_vars['is_trialing'] = rcp_is_trialing( $user_id );
}
// Group membership stuff
$is_group_member = function_exists('cgc_group_accounts') ? cgc_group_accounts()->members->is_group_member( $user_id ) : false;
$group_id = function_exists('cgc_group_accounts') ? cgc_group_accounts()->members->get_group_id( $user_id ) : 'null';
$group_name = function_exists('cgc_group_accounts') ? cgc_group_accounts()->members->get_group_name( $user_id ) : false;
$group_role = function_exists('cgc_group_accounts') ? cgc_group_accounts()->members->get_role( $user_id ) : 'member';
if( $is_group_member ) {
$local_vars['groupId'] = $group_id;
$local_vars['groupName'] = $group_name;
$local_vars['groupRole'] = $group_role;
}
wp_enqueue_script( 'cgc_analytics', plugin_dir_url( __FILE__ ) . 'includes/cgc_analytics.js', array(), '3.0.1', true );
wp_localize_script( 'cgc_analytics', 'cgc_analytics_vars', $local_vars );
}
add_action( 'wp_enqueue_scripts', 'cgc_segment_load_scripts' );