-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into enhancement/#8154-top-content-oauth-flow.
- Loading branch information
Showing
17 changed files
with
670 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
includes/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Cron.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
includes/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Events_Sync.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
includes/Modules/Analytics_4/Conversion_Reporting/Conversion_Reporting_Provider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.