1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration\Tracker;
use Piwik\Common;
use Piwik\Db;
use Piwik\Plugins\Goals\API as GoalsAPI;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
class PingRequestTest extends IntegrationTestCase
{
const FIRST_VISIT_TIME = '2012-01-05 00:00:00';
const TEST_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36';
const TEST_BROWSER_LANGUAGE = 'en-gb';
const TEST_COUNTRY = 'nl';
const TEST_REGION = '06';
const CHANGED_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A';
const CHANGED_BROWSER_LANGUAGE = 'ja';
const CHANGED_COUNTRY = 'jp';
const CHANGED_REGION = '22';
public function setUp(): void
{
parent::setUp();
Fixture::createWebsite('2012-01-01 00:00:00');
GoalsAPI::getInstance()->addGoal(1, 'Goal 1 - Thank you', 'title', $matchPattern = 'pong', 'contains', $caseSensitive = false, $revenue = 10, $allowMultipleConversions = 1);
}
public function test_PingWithinThirtyMinutes_ExtendsExistingVisitAndLastAction_WithoutNewAction()
{
$tracker = $this->getTracker();
// track initial action
$response = $tracker->doTrackPageView('pong');
Fixture::checkResponse($response);
$this->assertInitialVisitIsCorrect();
// send a ping request within 30 minutes
$pingTime = '2012-01-05 00:20:00';
$this->doPingRequest($tracker, $pingTime, $setNewDimensionValues = false);
$this->assertInitialVisitIsNotExtended(self::FIRST_VISIT_TIME, $checkModifiedDimensions = false, 1201);
}
public function test_PingWithinThirtyMinutes_AndChangedDimensionValues_ExtendsExistingVisit_AndChangesAppropriateDimensions()
{
$tracker = $this->getTracker();
// track initial action
$response = $tracker->doTrackPageView('pong');
Fixture::checkResponse($response);
$this->assertInitialVisitIsCorrect();
// send a ping request within 30 minutes
$pingTime = '2012-01-05 00:20:00';
$this->doPingRequest($tracker, $pingTime, $setNewDimensionValues = true);
$this->assertInitialVisitIsNotExtended(self::FIRST_VISIT_TIME, $checkModifiedDimensions = true, 1201);
}
public function test_PingWithinThirtyMinutes_DoesNotTriggerGoalConversion()
{
$tracker = $this->getTracker();
// track initial action
$response = $tracker->doTrackPageView('pong');
Fixture::checkResponse($response);
$this->assertInitialVisitIsCorrect();
// send a ping request within 30 minutes
$pingTime = '2012-01-05 00:20:00';
// force trigger a goal within the ping request
$tracker->setDebugStringAppend('&idgoal=1');
$this->doPingRequest($tracker, $pingTime, $setNewDimensionValues = true);
$this->assertInitialVisitIsNotExtended(self::FIRST_VISIT_TIME, $checkModifiedDimensions = true, 1201);
$this->assertGoalConversionCount(1);
}
public function test_PingAfterThirtyMinutes_DoesNotCreateNewVisit()
{
$tracker = $this->getTracker();
// track initial action
$response = $tracker->doTrackPageView('pong');
Fixture::checkResponse($response);
$this->assertInitialVisitIsCorrect();
// send a ping request after 30 minutes
$pingTime = '2012-01-05 00:40:00';
$this->doPingRequest($tracker, $pingTime, $setNewDimensionValues = false);
$this->assertPingDidNotCreateNewVisit(self::FIRST_VISIT_TIME, $checkModifiedDimensions = false);
}
public function test_PingAfterThirtyMinutes_AndChangedDimensionValues_DoesNotCreateNewVisit()
{
$tracker = $this->getTracker();
// track initial action
$response = $tracker->doTrackPageView('pong');
Fixture::checkResponse($response);
$this->assertInitialVisitIsCorrect();
// send a ping request after 30 minutes
$pingTime = '2012-01-05 00:40:00';
$this->doPingRequest($tracker, $pingTime, $setNewDimensionValues = true);
$this->assertPingDidNotCreateNewVisit(self::FIRST_VISIT_TIME, $checkModifiedDimensions = true);
}
private function getTracker()
{
$tracker = Fixture::getTracker(1, self::FIRST_VISIT_TIME, $defaultInit = true, $useLocalTracker = true);
$tracker->setTokenAuth(Fixture::getTokenAuth());
// properties that cannot be changed on next action
$tracker->setUserAgent(self::TEST_USER_AGENT);
$tracker->setBrowserLanguage(self::TEST_BROWSER_LANGUAGE);
// properties that can be changed on next action
$tracker->setCountry(self::TEST_COUNTRY);
$tracker->setRegion(self::TEST_REGION);
return $tracker;
}
private function assertVisitCount($expected)
{
$visitCount = Db::fetchOne("SELECT COUNT(*) FROM " . Common::prefixTable('log_visit'));
$this->assertEquals($expected, $visitCount);
}
private function assertActionCount($expected)
{
$visitCount = Db::fetchOne("SELECT COUNT(*) FROM " . Common::prefixTable('log_link_visit_action'));
$this->assertEquals($expected, $visitCount);
}
private function assertGoalConversionCount($expected)
{
$visitCount = Db::fetchOne("SELECT COUNT(*) FROM " . Common::prefixTable('log_conversion'));
$this->assertEquals($expected, $visitCount);
}
private function getVisitLastActionTime($idVisit)
{
return $this->getVisitProperty('visit_last_action_time', $idVisit);
}
private function getLatestActionTime($idVisit)
{
return Db::fetchOne("SELECT MAX(server_time) FROM " . Common::prefixTable('log_link_visit_action') . " WHERE idvisit = ?", array($idVisit));
}
private function getVisitTotalTime($idVisit)
{
return Db::fetchOne("SELECT MAX(visit_total_time) FROM " . Common::prefixTable('log_visit') . " WHERE idvisit = ?", array($idVisit));
}
private function assertInitialVisitIsCorrect()
{
$this->assertVisitCount(1);
$this->assertActionCount(1);
$this->assertGoalConversionCount(1);
$this->assertEquals(0, $this->getVisitTotalTime($idVisit= 1));
$this->assertVisitPropertiesAreUnchanged($idVisit = 1);
}
private function getVisitProperty($columnName, $idVisit)
{
return Db::fetchOne("SELECT $columnName FROM " . Common::prefixTable('log_visit') . " WHERE idvisit = ?", array($idVisit));
}
private function doPingRequest(\MatomoTracker $tracker, $pingTime, $changeDimensionValues)
{
if ($changeDimensionValues) {
$tracker->setUserAgent(self::CHANGED_USER_AGENT);
$tracker->setBrowserLanguage(self::CHANGED_BROWSER_LANGUAGE);
$tracker->setCountry(self::CHANGED_COUNTRY);
$tracker->setRegion(self::CHANGED_REGION);
}
$tracker->setForceVisitDateTime($pingTime);
$response = $tracker->doPing();
Fixture::checkResponse($response);
return $response;
}
private function assertInitialVisitIsNotExtended($firstActionTime, $checkPropertiesModified, $expectedTotalTime)
{
$this->assertVisitCount(1);
$this->assertActionCount(1);
$this->assertGoalConversionCount(1);
$visitEndTime = $this->getVisitLastActionTime($idVisit = 1);
$this->assertEquals($firstActionTime, $visitEndTime);
$actionTime = $this->getLatestActionTime($idVisit = 1);
$this->assertEquals($firstActionTime, $actionTime);
$visitTotalTime = $this->getVisitTotalTime($idVisit = 1);
$this->assertEquals($expectedTotalTime, $visitTotalTime);
if ($checkPropertiesModified) {
$this->assertVisitPropertiesAreChanged($idVisit = 1, $checkUnchangeable = false);
} else {
$this->assertVisitPropertiesAreUnchanged($idVisit = 1);
}
}
private function assertVisitPropertiesAreUnchanged($idVisit)
{
$browserName = $this->getVisitProperty('config_browser_name', $idVisit);
$this->assertEquals('CH', $browserName);
$browserLanguage = $this->getVisitProperty('location_browser_lang', $idVisit);
$this->assertEquals(self::TEST_BROWSER_LANGUAGE, $browserLanguage);
$country = $this->getVisitProperty('location_country', $idVisit);
$this->assertEquals(self::TEST_COUNTRY, $country);
$region = $this->getVisitProperty('location_region', $idVisit);
$this->assertEquals(self::TEST_REGION, $region);
}
private function assertVisitPropertiesAreChanged($idVisit, $checkUnchangeableDimensions)
{
// browser name & browser language cannot be changed
if ($checkUnchangeableDimensions) {
$browserName = $this->getVisitProperty('config_browser_name', $idVisit);
$this->assertEquals('SF', $browserName);
$browserLanguage = $this->getVisitProperty('location_browser_lang', $idVisit);
$this->assertEquals(self::CHANGED_BROWSER_LANGUAGE, $browserLanguage);
}
// region and country cannot be changed
$country = $this->getVisitProperty('location_country', $idVisit);
$this->assertEquals(self::CHANGED_COUNTRY, $country);
$region = $this->getVisitProperty('location_region', $idVisit);
$this->assertEquals(self::CHANGED_REGION, $region);
}
private function assertPingDidNotCreateNewVisit($expectedFirstVisitTime, $checkPropertiesModified)
{
$this->assertVisitCount(1);
$this->assertActionCount(1);
$firstVisitEndTime = $this->getVisitLastActionTime($idVisit = 1);
$this->assertEquals($expectedFirstVisitTime, $firstVisitEndTime);
$firstVisitActionTime = $this->getLatestActionTime($idVisit = 1);
$this->assertEquals($expectedFirstVisitTime, $firstVisitActionTime);
}
protected static function configureFixture($fixture)
{
parent::configureFixture($fixture);
$fixture->createSuperUser = true;
}
}
|