Skip to content

Commit

Permalink
Formatting the Google Analytics table and providing a warning message…
Browse files Browse the repository at this point in the history
… if the job has not been imported
  • Loading branch information
ivanboring committed Oct 25, 2024
1 parent c12a9af commit d5a1aa2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions modules/analyze_google_analytics/analyze_google_analytics.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
* Contains install and update functions for Google Analytics analyzer module.
*/

use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\google_analytics_reports_api\GoogleAnalyticsReportsApiFeed;

/**
* Implements hook_install().
*/
function analyze_google_analytics_install() {
// Message if the Google Analytics Reports import has not run.
// @phpstan-ignore-next-line
$account = GoogleAnalyticsReportsApiFeed::service();
$imported = \Drupal::configFactory()->get('google_analytics_reports.settings')->get('metadata_last_time');
if (!($account && $account->isAuthenticated() && $imported)) {
\Drupal::messenger()->addWarning(t('You both need to configure Google Analytics Reports and let the import run before you can use the Analyze Google Analytics plugin. %click_here for the both configuring and importing Google Analytics Reports.', [
'%click_here' => Link::fromTextAndUrl(t('Click here'), Url::fromRoute('google_analytics_reports_api.settings'))->toString(),
]));
}
}

/**
* Implements hook_uninstall().
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ public function renderFullReport(EntityInterface $entity): array {
if (strpos($key, '_') === 0 || $key === 'index') {
continue;
}

// If the value has a float precision over 2, we want to format it.


switch ($key) {
case 'sessionsPerUser':
$value = number_format((float) $value, 2);
break;

case 'averageSessionDuration':
case 'userEngagementDuration':
$value = number_format((float) $value, 2) . 's';
break;

case 'engagementRate':
case 'bounceRate':
$value = number_format((float) $value * 100, 2) . '%';
break;
}
// Transform every capital letter to a space and the letter.
$label = ucwords(strtolower(preg_replace('/(?<!^)[A-Z]/', ' $0', $key)));
$return['#rows'][] = [
Expand Down

0 comments on commit d5a1aa2

Please sign in to comment.