Skip to content

Commit

Permalink
Merge branch 'develop' into enhance/google#8176-rest-ds-for-audience-…
Browse files Browse the repository at this point in the history
…settings.
  • Loading branch information
hussain-t committed Mar 14, 2024
2 parents 68408f5 + 85e194a commit e48fba6
Show file tree
Hide file tree
Showing 10 changed files with 3,413 additions and 2,906 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ function ConsentModeSetupCTAWidget( { Widget, WidgetNull } ) {
'dismiss_notification'
);

showTooltip();

// For the first two dismissals, we show the notification again in two weeks.
if ( dismissCount < 2 ) {
showTooltip();

const twoWeeksInSeconds = WEEK_IN_SECONDS * 2;
await dismissPrompt( CONSENT_MODE_SETUP_CTA_WIDGET_SLUG, {
expiresInSeconds: twoWeeksInSeconds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ function AdBlockingRecoverySetupCTAWidget( { Widget, WidgetNull } ) {
'dismiss_notification'
);

showTooltip();

// For the first two dismissals, we show the notification again in two weeks.
if ( dismissCount < 2 ) {
showTooltip();

const twoWeeksInSeconds = WEEK_IN_SECONDS * 2;
await dismissPrompt( AD_BLOCKING_RECOVERY_MAIN_NOTIFICATION_KEY, {
expiresInSeconds: twoWeeksInSeconds,
Expand Down
2,924 changes: 2,924 additions & 0 deletions changelog.txt

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions includes/Core/Modules/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ function ( $recoverable, $slug ) {
2
);

add_filter(
'googlesitekit_is_module_connected',
function ( $connected, $slug ) {
return $this->is_module_connected( $slug );
},
10,
2
);

add_filter( 'option_' . Module_Sharing_Settings::OPTION, $this->get_method_proxy( 'populate_default_shared_ownership_module_settings' ) );
add_filter( 'default_option_' . Module_Sharing_Settings::OPTION, $this->get_method_proxy( 'populate_default_shared_ownership_module_settings' ), 20 );

Expand Down
17 changes: 10 additions & 7 deletions includes/Modules/Analytics_4.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use Google\Site_Kit\Modules\Analytics_4\Audience_Settings;
use Google\Site_Kit\Modules\Analytics_4\Custom_Dimensions_Data_Available;
use Google\Site_Kit\Modules\Analytics_4\Synchronize_Property;
use Google\Site_Kit\Modules\Analytics_4\Synchronize_AdSenseLinked;
use Google\Site_Kit\Modules\Analytics_4\GoogleAnalyticsAdmin\AccountProvisioningService;
use Google\Site_Kit\Modules\Analytics_4\GoogleAnalyticsAdmin\EnhancedMeasurementSettingsModel;
use Google\Site_Kit\Modules\Analytics_4\GoogleAnalyticsAdmin\PropertiesAdSenseLinksService;
Expand Down Expand Up @@ -182,15 +183,17 @@ public function register() {
);
$synchronize_property->register();

( new Advanced_Tracking( $this->context ) )->register();

add_action(
'admin_init',
function() use ( $synchronize_property ) {
$synchronize_property->maybe_schedule_synchronize_property();
}
$synchronize_adsense_linked = new Synchronize_AdSenseLinked(
$this,
$this->user_options,
$this->options
);
$synchronize_adsense_linked->register();

( new Advanced_Tracking( $this->context ) )->register();

add_action( 'admin_init', array( $synchronize_property, 'maybe_schedule_synchronize_property' ) );
add_action( 'admin_init', array( $synchronize_adsense_linked, 'maybe_schedule_synchronize_adsense_linked' ) );
add_action( 'admin_init', $this->get_method_proxy( 'handle_provisioning_callback' ) );

// For non-AMP and AMP.
Expand Down
155 changes: 155 additions & 0 deletions includes/Modules/Analytics_4/Synchronize_AdSenseLinked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/**
* Class Google\Site_Kit\Modules\Analytics_4\Synchronize_AdSenseLinked
*
* @package Google\Site_Kit\Modules\Analytics_4
* @copyright 2024 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Modules\Analytics_4;

use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Modules\Adsense;
use Google\Site_Kit\Modules\Analytics_4;
use Google\Site_Kit\Modules\AdSense\Settings as Adsense_Settings;

/**
* The base class for Synchronizing the adSenseLinked status.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Synchronize_AdSenseLinked {
const CRON_SYNCHRONIZE_ADSENSE_LINKED = 'googlesitekit_cron_synchronize_adsense_linked_data';

/**
* Analytics_4 instance.
*
* @since n.e.x.t
* @var Analytics_4
*/
protected $analytics_4;

/**
* User_Options instance.
*
* @since n.e.x.t
* @var User_Options
*/
protected $user_options;

/**
* Options instance.
*
* @since n.e.x.t
* @var Options
*/
protected $options;

/**
* Constructor.
*
* @since n.e.x.t
*
* @param Analytics_4 $analytics_4 Analytics 4 instance.
* @param User_Options $user_options User_Options instance.
* @param Options $options Options instance.
*/
public function __construct( Analytics_4 $analytics_4, User_Options $user_options, Options $options ) {
$this->analytics_4 = $analytics_4;
$this->user_options = $user_options;
$this->options = $options;
}

/**
* Registers functionality through WordPress hooks.
*
* @since n.e.x.t
*/
public function register() {
add_action(
self::CRON_SYNCHRONIZE_ADSENSE_LINKED,
function() {
$this->synchronize_adsense_linked_data();
}
);
}

/**
* Cron callback for synchronizing the adsense linked data.
*
* @since n.e.x.t
*/
protected function synchronize_adsense_linked_data() {
$owner_id = $this->analytics_4->get_owner_id();
$restore_user = $this->user_options->switch_user( $owner_id );

if ( user_can( $owner_id, Permissions::VIEW_AUTHENTICATED_DASHBOARD ) ) {
$this->synchronize_adsense_linked_status();
}

$restore_user();
}

/**
* Schedules single cron which will synchronize the adSenseLinked status.
*
* @since n.e.x.t
*/
public function maybe_schedule_synchronize_adsense_linked() {
$analytics_4_connected = apply_filters( 'googlesitekit_is_module_connected', false, Analytics_4::MODULE_SLUG );
$adsense_connected = apply_filters( 'googlesitekit_is_module_connected', false, AdSense::MODULE_SLUG );
$cron_already_scheduled = wp_next_scheduled( self::CRON_SYNCHRONIZE_ADSENSE_LINKED );

if ( $analytics_4_connected && $adsense_connected && ! $cron_already_scheduled ) {
wp_schedule_single_event(
// Schedule the task to run in 24 hours.
time() + ( DAY_IN_SECONDS ),
self::CRON_SYNCHRONIZE_ADSENSE_LINKED
);
}
}

/**
* Synchronize the AdSenseLinked status.
*
* @since n.e.x.t
*
* @return null
*/
protected function synchronize_adsense_linked_status() {
$settings_ga4 = $this->analytics_4->get_settings()->get();
$property_id = $settings_ga4['propertyID'];
$property_adsense_links = $this->analytics_4->get_data( 'adsense-links', array( 'propertyID' => $property_id ) );
$current_adsense_options = ( new AdSense_Settings( $this->options ) )->get();
$current_adsense_client_id = ! empty( $current_adsense_options['clientID'] ) ? $current_adsense_options['clientID'] : '';

if ( is_wp_error( $property_adsense_links ) || empty( $property_adsense_links ) ) {
return null;
}

$found_adsense_linked_for_client_id = false;

// Iterate over returned AdSense links and set true if one is found
// matching the same client ID.
foreach ( $property_adsense_links as $property_adsense_link ) {
if ( $current_adsense_client_id === $property_adsense_link['adClientCode'] ) {
$found_adsense_linked_for_client_id = true;
break;
}
}

// Update the AdSenseLinked status and timestamp.
$this->analytics_4->get_settings()->merge(
array(
'adSenseLinked' => $found_adsense_linked_for_client_id,
'adSenseLinkedLastSyncedAt' => time(),
)
);
}
}
Loading

0 comments on commit e48fba6

Please sign in to comment.