|
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\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 |
|
|
|
|
|
|
30
|
|
|
* @package InstantAnalytics |
|
|
|
|
|
|
31
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
32
|
|
|
*/ |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
44
|
|
|
* @param string $title |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
57
|
|
|
* @return BaseEvent |
|
|
|
|
|
|
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
|
|
|
/** |
|
|
|
|
|
|
75
|
|
|
* @param Product|Variant $productVariant the Product or Variant |
|
76
|
|
|
*/ |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
86
|
|
|
* @param $title |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
100
|
|
|
* @param string $eventName |
|
|
|
|
|
|
101
|
|
|
* @param array $params |
|
|
|
|
|
|
102
|
|
|
* @return Markup |
|
|
|
|
|
|
103
|
|
|
* @throws Exception |
|
|
|
|
|
|
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
|
|
|
|