Completed
Push — add/nosara-tracks ( e9ab18...38c34f )
by
unknown
44:41 queued 28:39
created

JetpackTracking::record_user_event()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.5806
cc 4
eloc 15
nc 8
nop 2
1
<?php
2
/**
3
 * Nosara Tracks for Jetpack
4
 */
5
6
require_once( dirname( __FILE__ ) . '/_inc/lib/tracks/client.php' );
7
8
class JetpackTracking {
9
	static $product_name = 'jetpack';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $product_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
10
11
	static function track_jetpack_usage() {
12
		add_action( 'jetpack_activate_module',       array( __CLASS__, 'track_activate_module'), 1, 1 );
13
		add_action( 'jetpack_pre_deactivate_module', array( __CLASS__, 'track_deactivate_module'), 1, 1 );
14
		add_action( 'jetpack_user_authorized',       array( __CLASS__, 'track_user_linked' ) );
15
	}
16
17
	/* User has linked their account */
18
	static function track_user_linked() {
19
		$user_id = get_current_user_id();
20
		$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
21
22
		if ( $anon_id ) {
23
			self::record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) );
24
			delete_user_meta( $user_id, 'jetpack_tracks_anon_id' );
25
			if( ! headers_sent() ) {
26
				setcookie( 'tk_ai', 'expired', time() - 1000 );
27
			}
28
		}
29
30
		$wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
31
		update_user_meta( $user_id, $wpcom_user_data['ID'] );
32
33
		self::record_user_event( 'user_linked', array() );
34
	}
35
36
	/* Activated module */
37
	static function track_activate_module( $module ) {
38
		self::record_user_event( 'module_activated', array( 'module' => $module ) );
39
	}
40
41
	/* Deactivated module */
42
	static function track_deactivate_module( $module ) {
43
		self::record_user_event( 'module_deactivated', array( 'module' => $module ) );
44
	}
45
46
	static function record_user_event( $event_type, $data ) {
47
48
		$user = wp_get_current_user();
49
		$site_url = get_option( 'siteurl' );
50
51
		$data['_via_ua'] = $_SERVER['HTTP_USER_AGENT'];
52
		$data['_via_ip'] = $_SERVER['REMOTE_ADDR'];
53
		$data['_lg']     = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
54
		$data['blog_url'] = $site_url;
55
56
		$top_level_events = array( '_aliasUser' );
57
58
		$event_name = self::$product_name.'_'.$event_type;
59
60
		if ( in_array( $event_type, $top_level_events ) ) {
61
			$event_name = $event_type;
62
		}
63
64
		$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : "0";
65
66
		$response = tracks_record_event( $user, $event_name, $data );
67
68
		if ( is_wp_error( $response ) ) {
69
			error_log( "There was an error: ".$response->get_error_message() );
0 ignored issues
show
Bug introduced by
The method get_error_message cannot be called on $response (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
70
		}
71
	}
72
}
73
74
add_action( 'init',  array( 'JetpackTracking', 'track_jetpack_usage' ) );
75