Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/base/WidgetData.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace verbb\metrix\base;

use craft\helpers\Cp;
use verbb\metrix\Metrix;

use Craft;
Expand Down Expand Up @@ -72,6 +73,10 @@ private function _getCacheKey(): string
$this->dimension,
$this->period,
];

if ($this->widget?->getView()?->supportsMultiSite) {
$cacheKey[] = Cp::requestedSite()->id;
}

return implode('.', $cacheKey);
}
Expand Down
7 changes: 6 additions & 1 deletion src/controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
1 change: 1 addition & 0 deletions src/controllers/ViewsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
35 changes: 35 additions & 0 deletions src/migrations/m260512_191215_add_multisite_column_to_views.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace verbb\metrix\migrations;

use Craft;
use craft\db\Migration;

/**
* m260512_191215_add_multisite_column_to_views migration.
*/
class m260512_191215_add_multisite_column_to_views extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
$table = Craft::$app->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;
}
}
1 change: 1 addition & 0 deletions src/models/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class View extends Model
public ?DateTime $dateUpdated = null;
public ?string $uid = null;

public ?bool $supportsMultiSite = false;
}
2 changes: 2 additions & 0 deletions src/services/Views.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -201,6 +202,7 @@ private function _createViewQuery(): Query
'id',
'name',
'handle',
'supportsMultiSite',
'sortOrder',
'dateCreated',
'dateUpdated',
Expand Down
22 changes: 22 additions & 0 deletions src/sources/GoogleAnalytics.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace verbb\metrix\sources;

use craft\helpers\Cp;
use verbb\metrix\Metrix;
use verbb\metrix\base\OAuthSource;
use verbb\metrix\base\Period;
Expand Down Expand Up @@ -214,6 +215,27 @@ public function fetchData(WidgetDataInterface $widgetData): array
$payload['dimensions'] = [['name' => $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,
]);
Expand Down
17 changes: 17 additions & 0 deletions src/templates/dashboard/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
8 changes: 8 additions & 0 deletions src/templates/views/_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand Down