Skip to content

Commit

Permalink
Merge branch 'develop' into enhancement/#8154-top-content-oauth-flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
hussain-t committed Aug 28, 2024
2 parents 30026a2 + e791db3 commit 406dff6
Show file tree
Hide file tree
Showing 17 changed files with 670 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ export default function ActionsCTALinkDismiss( {
event.preventDefault();
}

navigateTo( ctaLink );

await Promise.all( [
trackEvents.confirm(),
dismissNotification( id, { expiresInSeconds: dismissExpires } ),
] );

navigateTo( ctaLink );
};

return (
Expand Down
2 changes: 1 addition & 1 deletion bin/local-env/env-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -u
. "$(dirname "$0")/includes.sh"

# If the WordPress container isn't running, start the environment.
if ! dc ps -q $CONTAINER ; then
if ! container ls > /dev/null ; then
status_message "E2E environment is not running, starting it now..."
npm run env:start
fi
2 changes: 1 addition & 1 deletion includes/Modules/Ads.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected function setup_assets() {
'googlesitekit-ads-pax-integrator',
array(
// When updating, mirror the fixed version for google-pax-sdk in package.json.
'src' => 'https://www.gstatic.com/pax/1.0.9/pax_integrator.js',
'src' => 'https://www.gstatic.com/pax/1.0.12/pax_integrator.js',
'execution' => 'async',
'dependencies' => array(
'googlesitekit-ads-pax-config',
Expand Down
17 changes: 17 additions & 0 deletions includes/Modules/Analytics_4.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface;
use Google\Site_Kit\Core\REST_API\REST_Routes;
use Google\Site_Kit\Core\User\Audience_Settings;
use Google\Site_Kit\Modules\Analytics_4\Conversion_Reporting\Conversion_Reporting_Cron;
use Google\Site_Kit\Modules\Analytics_4\Conversion_Reporting\Conversion_Reporting_Events_Sync;
use Google\Site_Kit\Modules\Analytics_4\Conversion_Reporting\Conversion_Reporting_Provider;
use stdClass;
use WP_Error;

Expand Down Expand Up @@ -218,6 +221,15 @@ public function register() {
);
$synchronize_ads_linked->register();

if ( Feature_Flags::enabled( 'conversionReporting' ) ) {
$conversion_reporting_provider = new Conversion_Reporting_Provider(
$this->settings,
$this->user_options,
$this
);
$conversion_reporting_provider->register();
}

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

add_action( 'admin_init', array( $synchronize_property, 'maybe_schedule_synchronize_property' ) );
Expand Down Expand Up @@ -285,12 +297,17 @@ function ( $audience ) {
'adSenseLinkedLastSyncedAt' => 0,
'adsLinked' => false,
'adsLinkedLastSyncedAt' => 0,
'recentEvents' => array(),
'availableAudiencesLastSyncedAt' => 0,
)
);

if ( ! empty( $new_value['propertyID'] ) ) {
do_action( Synchronize_AdSenseLinked::CRON_SYNCHRONIZE_ADSENSE_LINKED );

if ( Feature_Flags::enabled( 'conversionReporting' ) ) {
do_action( Conversion_Reporting_Cron::CRON_ACTION );
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Class Google\Site_Kit\Modules\Analytics_4\Conversion_Reporting\Conversion_Reporting_Cron
*
* @package Google\Site_Kit
* @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\Conversion_Reporting;

/**
* Class providing cron implementation for conversion reporting.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Conversion_Reporting_Cron {

const CRON_ACTION = 'googlesitekit_cron_conversion_reporting_events';

/**
* Cron callback reference.
*
* @var callable
*/
private $cron_callback;

/**
* Constructor.
*
* @since n.e.x.t
*
* @param callable $callback Function to call on the cron action.
*/
public function __construct( callable $callback ) {
$this->cron_callback = $callback;
}

/**
* Registers functionality through WordPress hooks.
*
* @since 1.133.0
*/
public function register() {
add_action( self::CRON_ACTION, $this->cron_callback );
}

/**
* Schedules cron if not already set.
*
* @since n.e.x.t
*/
public function maybe_schedule_cron() {
if ( ! wp_next_scheduled( self::CRON_ACTION ) && ! wp_installing() ) {
wp_schedule_single_event( time() + DAY_IN_SECONDS, self::CRON_ACTION );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* Class Google\Site_Kit\Modules\Analytics_4\Conversion_Reporting\Conversion_Reporting_Events_Sync
*
* @package Google\Site_Kit
* @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\Conversion_Reporting;

use Google\Site_Kit\Modules\Analytics_4;
use Google\Site_Kit\Modules\Analytics_4\Settings;

/**
* Class providing report implementation for available events for conversion reporting.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Conversion_Reporting_Events_Sync {

const EVENT_NAMES = array(
'add_to_cart',
'purchase',
'submit_lead_form',
'generate_lead',
'contact',
);

/**
* Settings instance.
*
* @var Settings
*/
private $settings;

/**
* Analytics_4 instance.
*
* @var Analytics_4
*/
private $analytics;

/**
* Constructor.
*
* @since n.e.x.t
*
* @param Settings $settings Settings module settings instance.
* @param Analytics_4 $analytics Analytics 4 module instance.
*/
public function __construct(
Settings $settings,
Analytics_4 $analytics
) {
$this->settings = $settings;
$this->analytics = $analytics;
}

/**
* Checks for available events and save them in settings.
*
* @since n.e.x.t
*/
public function check_for_events() {
$report = $this->get_report();
$detected_events = array();

if ( is_wp_error( $report ) ) {
return;
}

// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( empty( $report->rowCount ) ) {
$this->settings->merge( array( 'recentEvents' => array() ) );

return;
}

foreach ( $report->rows as $row ) {
$detected_events[] = $row['dimensionValues'][0]['value'];
}

$this->settings->merge( array( 'recentEvents' => $detected_events ) );
}

/**
* Retrieves the GA4 report for filtered events.
*
* @since n.e.x.t
*/
protected function get_report() {
$options = array(
// The 'metrics' parameter is required. 'eventCount' is used to ensure the request succeeds.
'metrics' => array( array( 'name' => 'eventCount' ) ),
'dimensions' => array(
array(
'name' => 'eventName',
),
),
'startDate' => gmdate( 'Y-m-d', strtotime( '-90 days' ) ),
'endDate' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ),
'dimensionFilters' => array(
'eventName' => array(
'filterType' => 'inListFilter',
'value' => self::EVENT_NAMES,
),
),
'limit' => '20',
);

return $this->analytics->get_data( 'report', $options );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Class Google\Site_Kit\Modules\Analytics_4\Conversion_Reporting\Conversion_Reporting_Provider
*
* @package Google\Site_Kit
* @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\Conversion_Reporting;

use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Modules\Analytics_4;
use Google\Site_Kit\Modules\Analytics_4\Settings;

/**
* Class providing the integration of conversion reporting.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Conversion_Reporting_Provider {

/**
* User_Options instance.
*
* @var User_Options
*/
private $user_options;

/**
* Analytics_4 instance.
*
* @var Analytics_4
*/
private $analytics;

/**
* Conversion_Reporting_Cron instance.
*
* @var Conversion_Reporting_Cron
*/
private Conversion_Reporting_Cron $cron;

/**
* Conversion_Reporting_Events_Check instance.
*
* @var Conversion_Reporting_Events_Check
*/
private Conversion_Reporting_Events_Sync $events_check;

/**
* Constructor.
*
* @since n.e.x.t
*
* @param Settings $settings Settings instance.
* @param User_Options $user_options User_Options instance.
* @param Analytics_4 $analytics analytics_4 instance.
*/
public function __construct(
Settings $settings,
User_Options $user_options,
Analytics_4 $analytics
) {
$this->user_options = $user_options;
$this->analytics = $analytics;

$this->events_check = new Conversion_Reporting_Events_Sync( $settings, $this->analytics );
$this->cron = new Conversion_Reporting_Cron( fn() => $this->cron_callback() );
}

/**
* Registers functionality through WordPress hooks.
*
* @since n.e.x.t
*/
public function register() {
$this->cron->register();

add_action( 'load-toplevel_page_googlesitekit-dashboard', fn () => $this->on_dashboard_load() );
}

/**
* Handles the googlesitekit-dashboard page load callback.
*
* @since n.e.x.t
*/
protected function on_dashboard_load() {
$this->cron->maybe_schedule_cron();
}

/**
* Handles the cron callback.
*
* @since n.e.x.t
*/
protected function cron_callback() {
$owner_id = $this->analytics->get_owner_id();
$restore_user = $this->user_options->switch_user( $owner_id );

$this->events_check->check_for_events();

$restore_user();
}
}
1 change: 1 addition & 0 deletions includes/Modules/Analytics_4/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ protected function get_default() {
'adsLinkedLastSyncedAt' => 0,
'availableAudiences' => null,
'availableAudiencesLastSyncedAt' => 0,
'recentEvents' => array(),
);
}

Expand Down
Loading

0 comments on commit 406dff6

Please sign in to comment.