diff --git a/src/base/WidgetData.php b/src/base/WidgetData.php index d886962..5c29896 100644 --- a/src/base/WidgetData.php +++ b/src/base/WidgetData.php @@ -1,6 +1,7 @@ dimension, $this->period, ]; + + if ($this->widget?->getView()?->supportsMultiSite) { + $cacheKey[] = Cp::requestedSite()->id; + } return implode('.', $cacheKey); } diff --git a/src/controllers/DashboardController.php b/src/controllers/DashboardController.php index 874edbc..9665c8e 100644 --- a/src/controllers/DashboardController.php +++ b/src/controllers/DashboardController.php @@ -151,9 +151,14 @@ public function actionWidgetData(): Response $this->requireAcceptsJson(); $id = $this->request->getParam('id'); - $widget = Metrix::$plugin->getWidgets()->getWidgetById($id); + $viewHandle = $this->request->getParam('view'); + $view = Metrix::$plugin->getViews()->getViewByHandle($viewHandle); + if ($view && $view->supportsMultiSite) { + $site = $this->request->getParam('site'); + } + return $this->asJson($widget->getWidgetData()); } diff --git a/src/controllers/ViewsController.php b/src/controllers/ViewsController.php index 133782d..753d0d1 100644 --- a/src/controllers/ViewsController.php +++ b/src/controllers/ViewsController.php @@ -61,6 +61,7 @@ public function actionSave(): ?Response $view->id = $this->request->getParam('id'); $view->name = $this->request->getParam('name'); $view->handle = $this->request->getParam('handle'); + $view->supportsMultiSite = $this->request->getParam('supportsMultiSite'); if (!Metrix::$plugin->getViews()->saveView($view)) { return $this->asModelFailure($view, modelName: 'view'); diff --git a/src/migrations/Install.php b/src/migrations/Install.php index 119d71e..08298ad 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -78,6 +78,7 @@ public function createTables(): void 'id' => $this->primaryKey(), 'name' => $this->string()->notNull(), 'handle' => $this->string()->notNull(), + 'supportsMultiSite' => $this->boolean()->defaultValue(false), 'settings' => $this->text(), 'sortOrder' => $this->smallInteger()->unsigned(), 'dateCreated' => $this->dateTime()->notNull(), diff --git a/src/migrations/m260512_191215_add_multisite_column_to_views.php b/src/migrations/m260512_191215_add_multisite_column_to_views.php new file mode 100644 index 0000000..90aa023 --- /dev/null +++ b/src/migrations/m260512_191215_add_multisite_column_to_views.php @@ -0,0 +1,35 @@ +db->schema->getTableSchema('metrix_views'); + if (!isset($table->columns['supportsMultisite'])) { + $this->addColumn('{{%metrix_views}}', 'supportsMultiSite', $this->boolean()->defaultValue(false)); + } + + return true; + } + + /** + * @inheritdoc + */ + public function safeDown(): bool + { + $this->dropColumn('{{%metrix_views}}', 'supportsMultiSite'); + + return false; + } +} diff --git a/src/models/View.php b/src/models/View.php index 9c90f98..c16d562 100644 --- a/src/models/View.php +++ b/src/models/View.php @@ -18,4 +18,5 @@ class View extends Model public ?DateTime $dateUpdated = null; public ?string $uid = null; + public ?bool $supportsMultiSite = false; } diff --git a/src/services/Views.php b/src/services/Views.php index e18ca87..84cbc0f 100644 --- a/src/services/Views.php +++ b/src/services/Views.php @@ -93,6 +93,7 @@ public function saveView(View $view, bool $runValidation = true): bool $viewRecord = $this->_getViewRecordById($view->id); $viewRecord->name = $view->name; $viewRecord->handle = $view->handle; + $viewRecord->supportsMultiSite = $view->supportsMultiSite; if ($isNewView) { $maxSortOrder = (new Query()) @@ -201,6 +202,7 @@ private function _createViewQuery(): Query 'id', 'name', 'handle', + 'supportsMultiSite', 'sortOrder', 'dateCreated', 'dateUpdated', diff --git a/src/sources/GoogleAnalytics.php b/src/sources/GoogleAnalytics.php index bbe0447..8b24edb 100644 --- a/src/sources/GoogleAnalytics.php +++ b/src/sources/GoogleAnalytics.php @@ -1,6 +1,7 @@ $intervalDimension]]; } + if ($widgetData->widget?->getView()?->supportsMultiSite) { + $pBaseUrl = Craft::$app->getSites()->primarySite->baseUrl; + + $url = str_replace($pBaseUrl, '', Cp::requestedSite()->baseUrl); + if (isset($url[0]) && $url[0] !== "/") { + $url = "/$url"; + } + + if ($url === '') $url = '/'; + + $payload['dimensionFilter'] = [ + 'filter' => [ + 'fieldName' => 'pagePath', + 'stringFilter' => [ + 'matchType' => 'BEGINS_WITH', + 'value' => $url + ] + ] + ]; + } + $response = $this->request('POST', 'https://analyticsdata.googleapis.com/v1beta/' . $this->getPropertyId() . ':runReport', [ 'json' => $payload, ]); diff --git a/src/templates/dashboard/index.html b/src/templates/dashboard/index.html index 04a419a..672c107 100644 --- a/src/templates/dashboard/index.html +++ b/src/templates/dashboard/index.html @@ -5,6 +5,23 @@ { label: 'Dashboard' | t('metrix'), url: url('metrix') } ] %} +{% set sites = siteMenuItems(null, requestedSite) %} +{% js %} +window.Craft.sites = JSON.parse(`{{ sites|json_encode|raw }}`); +{% endjs %} + +{% if craft.app.getIsMultiSite() and requestedSite %} + {% set crumbs = crumbs | unshift({ + id: 'site-crumb', + icon: 'world', + label: requestedSite.name | t('site'), + menu: { + items: siteMenuItems(null, requestedSite), + label: 'Select site' | t('site') + }, + }) %} +{% endif %} + {% set title = 'Dashboard' | t('metrix') %} {% set selectedSubnavItem = 'dashboard' %} {% set showHeader = false %} diff --git a/src/templates/views/_edit.html b/src/templates/views/_edit.html index 2d7fcc9..0c9d7a7 100644 --- a/src/templates/views/_edit.html +++ b/src/templates/views/_edit.html @@ -62,6 +62,14 @@ required: true, }) }} +{{ forms.checkbox({ + label: 'Supports multi-site data' | t('metrix'), + instructions: 'If this view will be retrieving site-specific data' | t('metrix'), + id: 'supportsMultiSite', + name: 'supportsMultiSite', + checked: (viewModel is defined ? viewModel.supportsMultiSite|string : null), + errors: (viewModel is defined ? viewModel.getErrors('supportsMultiSite') : null), +}) }} {% endblock %}