1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Instant Analytics plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Instant Analytics brings full Google Analytics support to your Twig templates |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\instantanalytics\twigextensions; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\helpers\Template; |
15
|
|
|
use nystudio107\instantanalytics\helpers\IAnalytics; |
16
|
|
|
use nystudio107\instantanalytics\InstantAnalytics; |
17
|
|
|
use Twig\Extension\AbstractExtension; |
18
|
|
|
use Twig\Extension\GlobalsInterface; |
19
|
|
|
use Twig\Markup; |
20
|
|
|
use Twig\TwigFilter; |
21
|
|
|
use Twig\TwigFunction; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Twig can be extended in many ways; you can add extra tags, filters, tests, |
25
|
|
|
* operators, global variables, and functions. You can even extend the parser |
26
|
|
|
* itself with node visitors. |
27
|
|
|
* |
28
|
|
|
* http://twig.sensiolabs.org/doc/advanced.html |
|
|
|
|
29
|
|
|
* |
30
|
|
|
* @author nystudio107 |
|
|
|
|
31
|
|
|
* @package InstantAnalytics |
|
|
|
|
32
|
|
|
* @since 1.0.0 |
|
|
|
|
33
|
|
|
*/ |
|
|
|
|
34
|
|
|
class InstantAnalyticsTwigExtension extends AbstractExtension implements GlobalsInterface |
35
|
|
|
{ |
36
|
|
|
// Public Methods |
37
|
|
|
// ========================================================================= |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
|
|
|
|
42
|
|
|
public function getName() |
43
|
|
|
{ |
44
|
|
|
return 'InstantAnalytics'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** @noinspection ReturnTypeCanBeDeclaredInspection */ |
|
|
|
|
48
|
|
|
/** |
|
|
|
|
49
|
|
|
* @@inheritdoc |
50
|
|
|
*/ |
|
|
|
|
51
|
|
|
public function getGlobals() |
52
|
|
|
{ |
53
|
|
|
$globals = []; |
54
|
|
|
$view = Craft::$app->getView(); |
55
|
|
|
if ($view->getIsRenderingPageTemplate()) { |
56
|
|
|
$request = Craft::$app->getRequest(); |
57
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
58
|
|
|
// Return our Analytics object as a Twig global |
59
|
|
|
$globals = [ |
60
|
|
|
'instantAnalytics' => InstantAnalytics::$plugin->ia->getGlobals(InstantAnalytics::$currentTemplate), |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $globals; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
|
|
|
|
71
|
|
|
public function getFilters() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
new TwigFilter('pageViewAnalytics', [$this, 'pageViewAnalytics']), |
75
|
|
|
new TwigFilter('eventAnalytics', [$this, 'eventAnalytics']), |
76
|
|
|
new TwigFilter('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']), |
77
|
|
|
new TwigFilter('eventTrackingUrl', [$this, 'eventTrackingUrl']), |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
|
|
|
|
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
|
|
|
|
84
|
|
|
public function getFunctions() |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
new TwigFunction('pageViewAnalytics', [$this, 'pageViewAnalytics']), |
88
|
|
|
new TwigFunction('eventAnalytics', [$this, 'eventAnalytics']), |
89
|
|
|
new TwigFunction('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']), |
90
|
|
|
new TwigFunction('eventTrackingUrl', [$this, 'eventTrackingUrl']), |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get a PageView analytics object |
96
|
|
|
* |
97
|
|
|
* @param string $url |
|
|
|
|
98
|
|
|
* @param string $title |
|
|
|
|
99
|
|
|
* |
100
|
|
|
* @return null|IAnalytics object |
101
|
|
|
*/ |
102
|
|
|
public function pageViewAnalytics($url = '', $title = '') |
103
|
|
|
{ |
104
|
|
|
return InstantAnalytics::$plugin->ia->pageViewAnalytics($url, $title); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get an Event analytics object |
109
|
|
|
* |
110
|
|
|
* @param string $eventCategory |
|
|
|
|
111
|
|
|
* @param string $eventAction |
|
|
|
|
112
|
|
|
* @param string $eventLabel |
|
|
|
|
113
|
|
|
* @param int $eventValue |
|
|
|
|
114
|
|
|
* |
115
|
|
|
* @return null|IAnalytics |
116
|
|
|
*/ |
117
|
|
|
public function eventAnalytics($eventCategory = '', $eventAction = '', $eventLabel = '', $eventValue = 0) |
118
|
|
|
{ |
119
|
|
|
return InstantAnalytics::$plugin->ia->eventAnalytics($eventCategory, $eventAction, $eventLabel, $eventValue); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return an Analytics object |
124
|
|
|
* |
125
|
|
|
* @return null|IAnalytics |
126
|
|
|
*/ |
127
|
|
|
public function analytics() |
128
|
|
|
{ |
129
|
|
|
return InstantAnalytics::$plugin->ia->analytics(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get a PageView tracking URL |
134
|
|
|
* |
135
|
|
|
* @param $url |
|
|
|
|
136
|
|
|
* @param $title |
|
|
|
|
137
|
|
|
* |
138
|
|
|
* @return Markup |
139
|
|
|
* @throws \yii\base\Exception |
140
|
|
|
*/ |
141
|
|
|
public function pageViewTrackingUrl($url, $title): Markup |
142
|
|
|
{ |
143
|
|
|
return Template::raw(InstantAnalytics::$plugin->ia->pageViewTrackingUrl($url, $title)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get an Event tracking URL |
148
|
|
|
* |
149
|
|
|
* @param $url |
|
|
|
|
150
|
|
|
* @param string $eventCategory |
|
|
|
|
151
|
|
|
* @param string $eventAction |
|
|
|
|
152
|
|
|
* @param string $eventLabel |
|
|
|
|
153
|
|
|
* @param int $eventValue |
|
|
|
|
154
|
|
|
* |
155
|
|
|
* @return Markup |
156
|
|
|
* @throws \yii\base\Exception |
157
|
|
|
*/ |
158
|
|
|
public function eventTrackingUrl( |
159
|
|
|
$url, |
160
|
|
|
$eventCategory = '', |
161
|
|
|
$eventAction = '', |
162
|
|
|
$eventLabel = '', |
163
|
|
|
$eventValue = 0 |
164
|
|
|
): Markup |
165
|
|
|
{ |
|
|
|
|
166
|
|
|
return Template::raw(InstantAnalytics::$plugin->ia->eventTrackingUrl( |
|
|
|
|
167
|
|
|
$url, |
168
|
|
|
$eventCategory, |
169
|
|
|
$eventAction, |
170
|
|
|
$eventLabel, |
171
|
|
|
$eventValue |
172
|
|
|
)); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|