Skip to content

Commit

Permalink
Support for Google Analytics 4
Browse files Browse the repository at this point in the history
  • Loading branch information
alekseybobkov committed May 2, 2023
1 parent cbee476 commit 4baf1dc
Show file tree
Hide file tree
Showing 16 changed files with 322 additions and 481 deletions.
6 changes: 5 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Upgrade guide

- [Upgrading to 1.1 from 1.0](#upgrade-1.1)
- [Upgrading to 2.0 from 1.3](#upgrade-2.0)

<a name="upgrade-1.1"></a>
## Upgrading To 1.1
Expand All @@ -19,4 +20,7 @@ The Profile ID number has also changed, follow these settings to find the new on
1. Click the **Admin** main menu tab again and select **View > View Settings** from the menu. *Copy to your clipboard* the Profile ID (should be a number).
1. Paste this number in the **Analytics View/Profile ID number** field in the October back-end settings form.

> **Note**: This plugin does not support GAv4. If you are having issues creating an account, use the **Show advanced options** link in the GA Account creation and the **Create a Universal Analytics property** switch.
<a name="upgrade-2.0"></a>
## Upgrading To 2.0

Google will stop processing data for Universal Google Analytics on July 1, 2023. You will need to create a Google Analytics 4 property, upgrade the plugin, and configure it to access the new property. Refer to the plugin documentation for a detailed description of the configuration process.
43 changes: 9 additions & 34 deletions classes/Analytics.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?php namespace RainLab\GoogleAnalytics\Classes;

use App;
use Config;
use Google_Client;
use Google_Cache_File;
use Google_Service_Analytics;
use Google_Auth_AssertionCredentials;

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use ApplicationException;
use RainLab\GoogleAnalytics\Models\Settings;

Expand All @@ -14,19 +11,14 @@ class Analytics
use \October\Rain\Support\Traits\Singleton;

/**
* @var Google_Client Google API client
* @var Google\Analytics\Data\V1beta\BetaAnalyticsDataClient
*/
public $client;

/**
* @var Google_Service_Analytics Google API analytics service
*/
public $service;

/**
* @var string Google Analytics View ID
* @var string Google Analytics Property ID
*/
public $viewId;
public $propertyId;

protected function init()
{
Expand All @@ -39,27 +31,10 @@ protected function init()
throw new ApplicationException(trans('rainlab.googleanalytics::lang.strings.keynotuploaded'));
}

$client = new Google_Client();

/*
* Set caching
*/
$cache = App::make(CacheItemPool::class);
$client->setCache($cache);

/*
* Set assertion credentials
*/
$auth = json_decode($settings->gapi_key->getContents(), true);
$client->setAuthConfig($auth);
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
$this->client = new BetaAnalyticsDataClient([
'credentials' => $settings->gapi_key->getLocalPath()
]);

$this->client = $client;
$this->service = new Google_Service_Analytics($client);
$this->viewId = 'ga:'.$settings->profile_id;
$this->propertyId = $settings->profile_id;
}
}
116 changes: 0 additions & 116 deletions classes/CacheItem.php

This file was deleted.

181 changes: 0 additions & 181 deletions classes/CacheItemPool.php

This file was deleted.

2 changes: 1 addition & 1 deletion components/tracker/default.htm
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
gtag('set', { 'anonymize_ip': true });
{% endif %}
gtag('config', '{{ __SELF__.trackingId }}');
</script>
</script>
Loading

0 comments on commit 4baf1dc

Please sign in to comment.