1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://dukt.net/analytics/ |
4
|
|
|
* @copyright Copyright (c) 2022, Dukt |
5
|
|
|
* @license https://github.com/dukt/analytics/blob/master/LICENSE.md |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dukt\analytics\fields; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\base\Field; |
12
|
|
|
use craft\base\ElementInterface; |
13
|
|
|
use craft\helpers\Json; |
14
|
|
|
use dukt\analytics\web\assets\reportfield\ReportFieldAsset; |
15
|
|
|
use dukt\analytics\Plugin as Analytics; |
16
|
|
|
use craft\web\View; |
17
|
|
|
|
18
|
|
|
class Report extends Field |
19
|
|
|
{ |
20
|
|
|
// Static |
21
|
|
|
// ========================================================================= |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public static function displayName(): string |
27
|
|
|
{ |
28
|
|
|
return Craft::t('analytics', 'Analytics Report'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// Public Methods |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
38
|
|
|
{ |
39
|
|
|
$view = Craft::$app->getView(); |
40
|
|
|
$name = $this->handle; |
41
|
|
|
|
42
|
|
|
if (!Analytics::$plugin->getAnalytics()->checkPluginRequirements()) { |
43
|
|
|
return $view->renderTemplate('analytics/_special/plugin-not-configured'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!Analytics::$plugin->getSettings()->enableFieldtype) { |
47
|
|
|
return $view->renderTemplate('analytics/_components/fieldtypes/Report/disabled'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$siteView = Analytics::$plugin->getViews()->getSiteViewBySiteId($element->siteId); |
|
|
|
|
51
|
|
|
|
52
|
|
|
if (!$siteView) { |
|
|
|
|
53
|
|
|
return $view->renderTemplate('analytics/_special/view-not-configured'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$reportingView = $siteView->getView(); |
57
|
|
|
|
58
|
|
|
if (!$reportingView) { |
|
|
|
|
59
|
|
|
return $view->renderTemplate('analytics/_special/view-not-configured'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$variables = [ |
63
|
|
|
'hasUrl' => false, |
64
|
|
|
'isNew' => false, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
if ($element) { |
68
|
|
|
// Reformat the input name into something that looks more like an ID |
69
|
|
|
$id = $view->formatInputId($name); |
70
|
|
|
|
71
|
|
|
// Figure out what that ID is going to look like once it has been namespaced |
72
|
|
|
$namespacedId = $view->namespaceInputId($id); |
73
|
|
|
|
74
|
|
|
if ($element->id && $element->uri) { |
|
|
|
|
75
|
|
|
$uri = Analytics::$plugin->getAnalytics()->getElementUrlPath($element->id, $element->siteId); |
76
|
|
|
|
77
|
|
|
$startDate = date('Y-m-d', strtotime('-1 month')); |
78
|
|
|
$endDate = date('Y-m-d'); |
79
|
|
|
$metrics = 'ga:pageviews'; |
80
|
|
|
$dimensions = 'ga:date'; |
81
|
|
|
$filters = 'ga:pagePath=='.$uri; |
82
|
|
|
|
83
|
|
|
$request = [ |
84
|
|
|
'startDate' => $startDate, |
85
|
|
|
'endDate' => $endDate, |
86
|
|
|
'metrics' => $metrics, |
87
|
|
|
'dimensions' => $dimensions, |
88
|
|
|
'filters' => $filters |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
// JS Options |
92
|
|
|
$jsOptions = [ |
93
|
|
|
'chartLanguage' => Analytics::$plugin->getAnalytics()->getChartLanguage(), |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
// Add locale definition to JS options |
97
|
|
|
$siteView = Analytics::$plugin->getViews()->getSiteViewBySiteId($element->siteId); |
98
|
|
|
|
99
|
|
|
if ($siteView) { |
|
|
|
|
100
|
|
|
$reportingView = $siteView->getView(); |
101
|
|
|
|
102
|
|
|
if ($reportingView) { |
|
|
|
|
103
|
|
|
// Currency definition |
104
|
|
|
$jsOptions['currencyDefinition'] = Analytics::$plugin->getAnalytics()->getCurrencyDefinition($reportingView->gaViewCurrency); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Add cached response to JS options if any |
109
|
|
|
$cacheId = ['reports.getElementReport', $request]; |
110
|
|
|
$response = Analytics::$plugin->cache->get($cacheId); |
111
|
|
|
|
112
|
|
|
if ($response) { |
113
|
|
|
$response = [ |
114
|
|
|
'type' => 'area', |
115
|
|
|
'chart' => $response |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
$jsOptions['cachedResponse'] = $response; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Register JS & Styles |
122
|
|
|
$view->registerAssetBundle(ReportFieldAsset::class); |
123
|
|
|
$view->registerJs('new AnalyticsReportField("'.$namespacedId.'-field", '.Json::encode($jsOptions).');'); |
124
|
|
|
|
125
|
|
|
// Variables |
126
|
|
|
$variables = [ |
127
|
|
|
'isNew' => false, |
128
|
|
|
'hasUrl' => true, |
129
|
|
|
'id' => $id, |
130
|
|
|
'uri' => $uri, |
131
|
|
|
'name' => $name, |
132
|
|
|
'value' => $value, |
133
|
|
|
'model' => $this, |
134
|
|
|
'element' => $element |
135
|
|
|
]; |
136
|
|
|
} elseif (!$element->id) { |
137
|
|
|
$variables = [ |
138
|
|
|
'hasUrl' => false, |
139
|
|
|
'isNew' => true, |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $view->renderTemplate('analytics/_components/fieldtypes/Report/input', $variables); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|