|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Instant Analytics plugin for Craft CMS |
|
4
|
|
|
* |
|
5
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
6
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
|
|
7
|
|
|
* @link http://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @package InstantAnalytics |
|
|
|
|
|
|
9
|
|
|
* @since 1.0.0 |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace nystudio107\instantanalytics\helpers; |
|
13
|
|
|
|
|
14
|
|
|
use nystudio107\instantanalytics\InstantAnalytics; |
|
15
|
|
|
use nystudio107\instantanalytics\models\Settings; |
|
16
|
|
|
|
|
17
|
|
|
use Craft; |
|
18
|
|
|
|
|
19
|
|
|
use \TheIconic\Tracking\GoogleAnalytics\Analytics; |
|
|
|
|
|
|
20
|
|
|
use \TheIconic\Tracking\GoogleAnalytics\AnalyticsResponseInterface; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
|
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
24
|
|
|
* @package InstantAnalytics |
|
|
|
|
|
|
25
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
|
|
27
|
|
|
class IAnalytics extends Analytics |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
protected $shouldSendAnalytics = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
|
|
|
|
|
33
|
|
|
* @inheritdoc |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct($isSsl = false, $isDisabled = false, array $options = []) |
|
36
|
|
|
{ |
|
37
|
|
|
// Store whether or not we should be sending Analytics data |
|
38
|
|
|
$this->shouldSendAnalytics = InstantAnalytics::$settings->sendAnalyticsData; |
|
39
|
|
|
|
|
40
|
|
|
parent::__construct($isSsl, $isDisabled, $options); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Turn an empty value so the twig tags {{ }} can be used |
|
45
|
|
|
* |
|
46
|
|
|
* @return string '' |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __toString() |
|
49
|
|
|
{ |
|
50
|
|
|
return ''; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add a product impression to the Analytics object |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $productVariant |
|
|
|
|
|
|
57
|
|
|
* @param int $index |
|
|
|
|
|
|
58
|
|
|
* @param string $listName |
|
|
|
|
|
|
59
|
|
|
* @param int $listIndex |
|
|
|
|
|
|
60
|
|
|
*/ |
|
|
|
|
|
|
61
|
|
|
public function addCommerceProductImpression( |
|
62
|
|
|
$productVariant = null, |
|
63
|
|
|
$index = 0, |
|
64
|
|
|
$listName = 'default', |
|
65
|
|
|
$listIndex = 1 |
|
66
|
|
|
) { |
|
67
|
|
|
|
|
68
|
|
|
if (InstantAnalytics::$commercePlugin) { |
|
69
|
|
|
if ($productVariant) { |
|
70
|
|
|
InstantAnalytics::$plugin->commerce->addCommerceProductImpression( |
|
71
|
|
|
$this, |
|
72
|
|
|
$productVariant, |
|
|
|
|
|
|
73
|
|
|
$index, |
|
74
|
|
|
$listName, |
|
75
|
|
|
$listIndex |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} else { |
|
79
|
|
|
Craft::warning( |
|
80
|
|
|
Craft::t( |
|
81
|
|
|
'instant-analytics', |
|
82
|
|
|
'Craft Commerce is not installed' |
|
83
|
|
|
), |
|
84
|
|
|
__METHOD__ |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Add a product detail view to the Analytics object |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $productVariant |
|
|
|
|
|
|
93
|
|
|
*/ |
|
|
|
|
|
|
94
|
|
|
public function addCommerceProductDetailView($productVariant = null) |
|
95
|
|
|
{ |
|
96
|
|
|
if (InstantAnalytics::$commercePlugin) { |
|
97
|
|
|
if ($productVariant) { |
|
98
|
|
|
InstantAnalytics::$plugin->commerce->addCommerceProductDetailView($this, $productVariant); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
} else { |
|
101
|
|
|
Craft::warning( |
|
102
|
|
|
Craft::t( |
|
103
|
|
|
'instant-analytics', |
|
104
|
|
|
'Craft Commerce is not installed' |
|
105
|
|
|
), |
|
106
|
|
|
__METHOD__ |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Add a checkout step to the Analytics object |
|
113
|
|
|
* |
|
114
|
|
|
* @param $orderModel |
|
|
|
|
|
|
115
|
|
|
* @param int $step |
|
|
|
|
|
|
116
|
|
|
* @param string $option |
|
|
|
|
|
|
117
|
|
|
*/ |
|
|
|
|
|
|
118
|
|
|
public function addCommerceCheckoutStep($orderModel = null, $step = 1, $option = "") |
|
119
|
|
|
{ |
|
120
|
|
|
if (InstantAnalytics::$commercePlugin) { |
|
121
|
|
|
if ($orderModel) { |
|
122
|
|
|
InstantAnalytics::$plugin->commerce->addCommerceCheckoutStep($this, $orderModel, $step, $option); |
|
123
|
|
|
} |
|
124
|
|
|
} else { |
|
125
|
|
|
Craft::warning( |
|
126
|
|
|
Craft::t( |
|
127
|
|
|
'instant-analytics', |
|
128
|
|
|
'Craft Commerce is not installed' |
|
129
|
|
|
), |
|
130
|
|
|
__METHOD__ |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Override sendHit() so that we can prevent Analytics data from being sent |
|
137
|
|
|
* |
|
138
|
|
|
* @param $methodName |
|
|
|
|
|
|
139
|
|
|
* |
|
140
|
|
|
* @return AnalyticsResponseInterface|null |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function sendHit($methodName) |
|
143
|
|
|
{ |
|
144
|
|
|
$requestIp = $_SERVER['REMOTE_ADDR']; |
|
145
|
|
|
if ($this->shouldSendAnalytics) { |
|
146
|
|
|
if ($this->getClientId() !== null || $this->getUserId() !== null) { |
|
147
|
|
|
try { |
|
148
|
|
|
Craft::info( |
|
149
|
|
|
'Send hit for IAnalytics object: ' . print_r($this, true), |
|
|
|
|
|
|
150
|
|
|
__METHOD__ |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
return parent::sendHit($methodName); |
|
154
|
|
|
} catch (\Exception $e) { |
|
155
|
|
|
if (InstantAnalytics::$settings->logExcludedAnalytics) { |
|
156
|
|
|
Craft::info( |
|
157
|
|
|
'*** sendHit(): error sending analytics: ' . $e->getMessage(), |
|
158
|
|
|
__METHOD__ |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} elseif (InstantAnalytics::$settings->logExcludedAnalytics) { |
|
163
|
|
|
Craft::info( |
|
164
|
|
|
'*** sendHit(): analytics not sent for '.$requestIp. ' because no clientId or userId is set', |
|
165
|
|
|
__METHOD__ |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
} elseif (InstantAnalytics::$settings->logExcludedAnalytics) { |
|
169
|
|
|
Craft::info( |
|
170
|
|
|
'*** sendHit(): analytics not sent for '.$requestIp, |
|
171
|
|
|
__METHOD__ |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return null; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|