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