function google_analytics_reports_api_gafeed in Google Analytics Reports 8.3
Same name and namespace in other branches
- 7.3 google_analytics_reports_api/google_analytics_reports_api.module \google_analytics_reports_api_gafeed()
Instantiate a new GoogleAnalyticsReportsApiFeed object.
Return value
object GoogleAnalyticsReportsApiFeed object to authorize access and request data from the Google Analytics Core Reporting API.
5 calls to google_analytics_reports_api_gafeed()
- GoogleAnalyticsReportsAdminSettingsForm::buildForm in src/
Form/ GoogleAnalyticsReportsAdminSettingsForm.php - Form constructor.
- GoogleAnalyticsReportsApiAdminSettingsForm::buildForm in google_analytics_reports_api/
src/ Form/ GoogleAnalyticsReportsApiAdminSettingsForm.php - Form constructor.
- google_analytics_reports_api_profiles_list in google_analytics_reports_api/
google_analytics_reports_api.module - Google Analytics reports profiles for current authorized user.
- google_analytics_reports_api_report_data in google_analytics_reports_api/
google_analytics_reports_api.module - Request report data.
- google_analytics_reports_api_revoke in google_analytics_reports_api/
google_analytics_reports_api.module - Programmatically revoke token.
File
- google_analytics_reports_api/
google_analytics_reports_api.module, line 18 - Implements the API through which Google Analytics data can be accessed.
Code
function google_analytics_reports_api_gafeed() {
$config = \Drupal::configFactory()
->get('google_analytics_reports_api.settings');
$state = \Drupal::state();
// If the access token is still valid, return an authenticated
// GoogleAnalyticsReportsApiFeed.
$access_token = $state
->get('google_analytics_reports_api.access_token');
if ($access_token && time() < $state
->get('google_analytics_reports_api.expires_at')) {
return new GoogleAnalyticsReportsApiFeed($access_token);
}
// If the site has an access token and refresh token, but the access
// token has expired, authenticate the user with the refresh token.
$refresh_token = $state
->get('google_analytics_reports_api.refresh_token');
if ($refresh_token) {
try {
$google_analytics_reports_api_feed = new GoogleAnalyticsReportsApiFeed();
$google_analytics_reports_api_feed
->refreshToken($config
->get('client_id'), $config
->get('client_secret'), $refresh_token);
$state
->setMultiple([
'google_analytics_reports_api.access_token' => $google_analytics_reports_api_feed->accessToken,
'google_analytics_reports_api.expires_at' => $google_analytics_reports_api_feed->expiresAt,
]);
return $google_analytics_reports_api_feed;
} catch (\Exception $e) {
\Drupal::messenger()
->addMessage(t('There was an authentication error. Message: @message.', [
'@message' => $e
->getMessage(),
]), 'error', FALSE);
\Drupal::logger('google_analytics_reports_api')
->error('There was an authentication error. Message: @message.', [
'@message' => $e
->getMessage(),
]);
}
}
}