Skip to content

Commit

Permalink
refactor: remove custom-events related code
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Sep 26, 2023
1 parent ceb4b9d commit 6d88b89
Showing 1 changed file with 0 additions and 79 deletions.
79 changes: 0 additions & 79 deletions includes/oauth/class-google-services-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,6 @@ public static function get_site_kit_oauth_client() {
return self::get_site_kit_authentication()->get_oauth_client();
}

/**
* Get Site Kit's Analytics module.
*
* @codeCoverageIgnore
* @return object Google OAuth2 client.
*/
public static function get_site_kit_analytics_module() {
if ( defined( 'GOOGLESITEKIT_PLUGIN_MAIN_FILE' ) ) {
$context = new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE );
return new SiteKitAnalytics( $context );
}
}

/**
* Get OAuth2 credentials.
*
Expand All @@ -70,70 +57,4 @@ public static function get_site_kit_analytics_module() {
public static function get_oauth2_credentials() {
return \Newspack\Google_OAuth::get_oauth2_credentials();
}

/**
* Send a custom event to GA.
*
* @param array $event_spec Event details.
*/
public static function send_custom_event( $event_spec ) {
Logger::log( 'Sending custom event of category "' . $event_spec['category'] . '" to GA.' );
try {
$analytics = self::get_site_kit_analytics_module();
if ( $analytics->is_connected() ) {
$tracking_id = $analytics->get_settings()->get()['propertyID'];
$analytics_ping_url = 'https://www.google-analytics.com/collect';

// Params docs: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters.
$analytics_ping_params = array(
'v' => 1,
'tid' => $tracking_id, // Tracking ID/ Web Property ID.
't' => 'event', // Hit type.
'an' => 'Newspack', // Application Name.
'ec' => $event_spec['category'], // Event Category.
'ea' => $event_spec['action'], // Event Action.
);

// Client ID.
if ( isset( $event_spec['cid'] ) ) {
$analytics_ping_params['cid'] = $event_spec['cid'];
} elseif ( isset( $_COOKIE['_ga'] ) ) {
$cookie_pieces = explode( '.', $_COOKIE['_ga'], 3 ); // phpcs:ignore WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___COOKIE, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( 1 === count( $cookie_pieces ) ) {
$cid = reset( $cookie_pieces );
} else {
list( $version, $domain_depth, $cid ) = $cookie_pieces;
}
$analytics_ping_params['cid'] = $cid;
} else {
$analytics_ping_params['cid'] = '555'; // Anonymous client.
}

if ( isset( $event_spec['label'] ) ) {
$analytics_ping_params['el'] = $event_spec['label']; // Event label.
}
if ( isset( $event_spec['value'] ) ) {
$analytics_ping_params['ev'] = $event_spec['value']; // Event value.
}
if ( isset( $event_spec['referer'] ) ) {
$analytics_ping_params['dr'] = $event_spec['referer']; // Document Referrer.
}
if ( isset( $event_spec['host'] ) ) {
$analytics_ping_params['dh'] = $event_spec['host']; // Document Host.
}
if ( isset( $event_spec['path'] ) ) {
$analytics_ping_params['dp'] = $event_spec['path']; // Document Page.
}

$ga_url = $analytics_ping_url . '?' . http_build_query( $analytics_ping_params );
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
return vip_safe_wp_remote_get( $ga_url );
} else {
return wp_remote_get( $ga_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
}
}
} catch ( \Throwable $th ) {
Logger::error( 'Failed sending custom event to GA: ' . $th->getMessage() );
}
}
}

0 comments on commit 6d88b89

Please sign in to comment.