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 |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
8 | * @copyright Copyright (c) 2017 nystudio107 |
||
0 ignored issues
–
show
|
|||
9 | */ |
||
0 ignored issues
–
show
|
|||
10 | |||
11 | namespace nystudio107\instantanalyticsGa4\twigextensions; |
||
12 | |||
13 | use Br33f\Ga4\MeasurementProtocol\Dto\Event\BaseEvent; |
||
14 | use Craft; |
||
15 | use craft\helpers\Template; |
||
16 | use nystudio107\instantanalyticsGa4\ga4\events\PageViewEvent; |
||
17 | use nystudio107\instantanalyticsGa4\helpers\Analytics; |
||
18 | use nystudio107\instantanalyticsGa4\InstantAnalytics; |
||
19 | use Twig\Extension\AbstractExtension; |
||
20 | use Twig\Extension\GlobalsInterface; |
||
21 | use Twig\Markup; |
||
22 | use Twig\TwigFilter; |
||
23 | use Twig\TwigFunction; |
||
24 | use yii\base\Exception; |
||
25 | |||
26 | /** |
||
27 | * Twig can be extended in many ways; you can add extra tags, filters, tests, |
||
28 | * operators, global variables, and functions. You can even extend the parser |
||
29 | * itself with node visitors. |
||
30 | * |
||
31 | * http://twig.sensiolabs.org/doc/advanced.html |
||
0 ignored issues
–
show
|
|||
32 | * |
||
33 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
34 | * @package InstantAnalytics |
||
0 ignored issues
–
show
|
|||
35 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
36 | */ |
||
0 ignored issues
–
show
|
|||
37 | class InstantAnalyticsTwigExtension extends AbstractExtension implements GlobalsInterface |
||
38 | { |
||
39 | // Public Methods |
||
40 | // ========================================================================= |
||
41 | |||
42 | /** |
||
0 ignored issues
–
show
|
|||
43 | * @inheritdoc |
||
44 | */ |
||
0 ignored issues
–
show
|
|||
45 | public function getName(): string |
||
46 | { |
||
47 | return 'InstantAnalytics'; |
||
48 | } |
||
49 | |||
50 | /** |
||
0 ignored issues
–
show
|
|||
51 | * @inheritdoc |
||
52 | */ |
||
0 ignored issues
–
show
|
|||
53 | public function getGlobals(): array |
||
54 | { |
||
55 | $globals = []; |
||
56 | $view = Craft::$app->getView(); |
||
57 | if ($view->getIsRenderingPageTemplate()) { |
||
58 | $request = Craft::$app->getRequest(); |
||
59 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
60 | // Return our Analytics object as a Twig global |
||
61 | $globals = [ |
||
62 | 'instantAnalytics' => InstantAnalytics::$plugin->ga4->getAnalytics(), |
||
63 | ]; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return $globals; |
||
68 | } |
||
69 | |||
70 | /** |
||
0 ignored issues
–
show
|
|||
71 | * @inheritdoc |
||
72 | */ |
||
0 ignored issues
–
show
|
|||
73 | public function getFilters(): array |
||
74 | { |
||
75 | return [ |
||
76 | new TwigFilter('pageViewEvent', [$this, 'pageViewEvent']), |
||
77 | new TwigFilter('simpleAnalyticsEvent', [$this, 'simpleEvent']), |
||
78 | new TwigFilter('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']), |
||
79 | new TwigFilter('eventTrackingUrl', [$this, 'eventTrackingUrl']), |
||
80 | ]; |
||
81 | } |
||
82 | |||
83 | /** |
||
0 ignored issues
–
show
|
|||
84 | * @inheritdoc |
||
85 | */ |
||
0 ignored issues
–
show
|
|||
86 | public function getFunctions(): array |
||
87 | { |
||
88 | return [ |
||
89 | new TwigFunction('pageViewEvent', [$this, 'pageViewEvent']), |
||
90 | new TwigFunction('simpleAnalyticsEvent', [$this, 'simpleEvent']), |
||
91 | new TwigFunction('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']), |
||
92 | new TwigFunction('eventTrackingUrl', [$this, 'eventTrackingUrl']), |
||
93 | ]; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get a PageView analytics object |
||
98 | * |
||
99 | * @param string $url |
||
0 ignored issues
–
show
|
|||
100 | * @param string $title |
||
0 ignored issues
–
show
|
|||
101 | * |
||
102 | * @return PageViewEvent object |
||
103 | */ |
||
104 | public function pageViewEvent(string $url = '', string $title = ''): PageViewEvent |
||
105 | { |
||
106 | return InstantAnalytics::$plugin->ga4->getPageViewEvent($url, $title); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get an Event analytics object |
||
111 | * |
||
112 | * @param string $eventName |
||
0 ignored issues
–
show
|
|||
113 | * @return BaseEvent |
||
0 ignored issues
–
show
|
|||
114 | */ |
||
115 | public function simpleEvent(string $eventName = ''): BaseEvent |
||
116 | { |
||
117 | return InstantAnalytics::$plugin->ga4->getSimpleEvent($eventName); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Get a PageView tracking URL |
||
122 | * |
||
123 | * @param $url |
||
0 ignored issues
–
show
|
|||
124 | * @param $title |
||
0 ignored issues
–
show
|
|||
125 | * |
||
126 | * @return Markup |
||
127 | * @throws Exception |
||
128 | */ |
||
129 | public function pageViewTrackingUrl($url, $title): Markup |
||
130 | { |
||
131 | return Template::raw(Analytics::getPageViewTrackingUrl($url, $title)); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get an Event tracking URL |
||
136 | * |
||
137 | * @param string $url |
||
0 ignored issues
–
show
|
|||
138 | * @param string $eventName |
||
0 ignored issues
–
show
|
|||
139 | * @param array $params |
||
0 ignored issues
–
show
|
|||
140 | * @return Markup |
||
0 ignored issues
–
show
|
|||
141 | * @throws Exception |
||
0 ignored issues
–
show
|
|||
142 | */ |
||
143 | public function eventTrackingUrl( |
||
144 | string $url, |
||
145 | string $eventName, |
||
146 | array $params = [], |
||
147 | ): Markup { |
||
148 | return Template::raw(Analytics::getEventTrackingUrl($url, $eventName, $params)); |
||
149 | } |
||
150 | } |
||
151 |