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\variables; |
||
12 | |||
13 | use Br33f\Ga4\MeasurementProtocol\Dto\Event\BaseEvent; |
||
14 | use craft\commerce\elements\Product; |
||
15 | use craft\commerce\elements\Variant; |
||
16 | use craft\helpers\Template; |
||
17 | use nystudio107\instantanalyticsGa4\ga4\Analytics; |
||
18 | use nystudio107\instantanalyticsGa4\ga4\events\PageViewEvent; |
||
19 | use nystudio107\instantanalyticsGa4\helpers\Analytics as AnalyticsHelper; |
||
20 | use nystudio107\instantanalyticsGa4\InstantAnalytics; |
||
21 | use nystudio107\pluginvite\variables\ViteVariableInterface; |
||
22 | use nystudio107\pluginvite\variables\ViteVariableTrait; |
||
23 | use Twig\Markup; |
||
24 | use yii\base\Exception; |
||
25 | |||
26 | /** |
||
27 | * Instant Analytics Variable |
||
28 | * |
||
29 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
30 | * @package InstantAnalytics |
||
0 ignored issues
–
show
|
|||
31 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
32 | */ |
||
0 ignored issues
–
show
|
|||
33 | class InstantAnalyticsVariable implements ViteVariableInterface |
||
34 | { |
||
35 | use ViteVariableTrait; |
||
36 | |||
37 | // Public Methods |
||
38 | // ========================================================================= |
||
39 | |||
40 | /** |
||
41 | * Get a PageView Event |
||
42 | * |
||
43 | * @param string $url |
||
0 ignored issues
–
show
|
|||
44 | * @param string $title |
||
0 ignored issues
–
show
|
|||
45 | * |
||
46 | * @return PageViewEvent |
||
47 | */ |
||
48 | public function pageViewEvent(string $url = '', string $title = ''): PageViewEvent |
||
49 | { |
||
50 | return InstantAnalytics::$plugin->ga4->getPageViewEvent($url, $title); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Get a simple event |
||
55 | * |
||
56 | * @param string $eventName |
||
0 ignored issues
–
show
|
|||
57 | * @return BaseEvent |
||
0 ignored issues
–
show
|
|||
58 | */ |
||
59 | public function simpleEvent(string $eventName = ''): BaseEvent |
||
60 | { |
||
61 | return InstantAnalytics::$plugin->ga4->getSimpleEvent($eventName); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Return the GA4 Analytics object |
||
66 | * |
||
67 | * @return Analytics |
||
68 | */ |
||
69 | public function ga4(): Analytics |
||
70 | { |
||
71 | return InstantAnalytics::$plugin->ga4->getAnalytics(); |
||
72 | } |
||
73 | |||
74 | /** |
||
0 ignored issues
–
show
|
|||
75 | * @param Product|Variant $productVariant the Product or Variant |
||
76 | */ |
||
0 ignored issues
–
show
|
|||
77 | public function addCommerceProductView($productVariant): void |
||
78 | { |
||
79 | InstantAnalytics::$plugin->commerce->addCommerceProductImpression($productVariant); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get a PageView tracking URL |
||
84 | * |
||
85 | * @param $url |
||
0 ignored issues
–
show
|
|||
86 | * @param $title |
||
0 ignored issues
–
show
|
|||
87 | * |
||
88 | * @return Markup |
||
89 | * @throws Exception |
||
90 | */ |
||
91 | public function pageViewTrackingUrl($url, $title): Markup |
||
92 | { |
||
93 | return Template::raw(AnalyticsHelper::getPageViewTrackingUrl($url, $title)); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get an Event tracking URL |
||
98 | * |
||
99 | * @param string $url |
||
0 ignored issues
–
show
|
|||
100 | * @param string $eventName |
||
0 ignored issues
–
show
|
|||
101 | * @param array $params |
||
0 ignored issues
–
show
|
|||
102 | * @return Markup |
||
0 ignored issues
–
show
|
|||
103 | * @throws Exception |
||
0 ignored issues
–
show
|
|||
104 | */ |
||
105 | public function eventTrackingUrl( |
||
106 | string $url, |
||
107 | string $eventName = '', |
||
108 | array $params = [], |
||
109 | ): Markup { |
||
110 | return Template::raw(AnalyticsHelper::getEventTrackingUrl($url, $eventName, $params)); |
||
111 | } |
||
112 | } |
||
113 |