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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
// Tests Visits and Dimensions behavior which is a lot of logic so not in VisitTest
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Tracker\Request;
use Piwik\Tracker\Visitor;
use Piwik\Piwik;
use Piwik\EventDispatcher;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
class FakeTrackerVisitDimension1 extends VisitDimension
{
protected $columnName = 'custom_dimension_1';
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
return false;
}
public function onExistingVisit(Request $request, Visitor $visitor, $action)
{
return 'existing1';
}
}
class FakeTrackerVisitDimension2 extends VisitDimension
{
protected $columnName = 'custom_dimension_2';
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
return 'onNew2';
}
public function onExistingVisit(Request $request, Visitor $visitor, $action)
{
return false;
}
public function onConvertedVisit(Request $request, Visitor $visitor, $action)
{
return false;
}
}
class FakeTrackerVisitDimension3 extends VisitDimension
{
protected $columnName = 'custom_dimension_3';
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
return 'onNew3';
}
public function onExistingVisit(Request $request, Visitor $visitor, $action)
{
return 'existing3';
}
public function onConvertedVisit(Request $request, Visitor $visitor, $action)
{
return 'converted3';
}
}
class FakeTrackerVisitDimension4 extends VisitDimension
{
protected $columnName = 'custom_dimension_4';
public function onExistingVisit(Request $request, Visitor $visitor, $action)
{
return $visitor->getVisitorColumn('custom_dimension_3') . 'extended';
}
public function getRequiredFields()
{
return array('custom_dimension_3');
}
}
class FakeTrackerVisit extends \Piwik\Tracker\Visit
{
public function __construct($request)
{
$this->request = $request;
$this->visitorInfo['location_ip'] = $request->getIp();
$this->visitorInfo['idvisitor'] = 1;
}
public function handleExistingVisit($visitor, $action, $visitIsConverted)
{
parent::handleExistingVisit($visitor, $action, $visitIsConverted);
}
public function handleNewVisit($visitor, $action, $visitIsConverted)
{
parent::handleNewVisit($visitor, $action, $visitIsConverted);
}
public function getAllVisitDimensions()
{
return array(
new FakeTrackerVisitDimension1(),
new FakeTrackerVisitDimension2(),
new FakeTrackerVisitDimension3(),
new FakeTrackerVisitDimension4(),
);
}
public function getVisitorInfo()
{
return $this->visitorInfo;
}
protected function insertNewVisit($visit)
{
}
protected function updateExistingVisit($valuesToUpdate)
{
}
}
/**
* @group Core
* @group VisitTest
*/
class VisitTest extends IntegrationTestCase
{
public function setUp()
{
parent::setUp();
Fixture::createWebsite('2014-01-01 00:00:00');
Piwik::addAction('Tracker.Request.getIdSite', function (&$idSite) {
$idSite = 1;
});
}
public function tearDown()
{
EventDispatcher::getInstance()->clearObservers('Tracker.Request.getIdSite');
parent::tearDown();
}
public function test_handleNewVisitWithoutConversion_shouldTriggerDimensions()
{
$request = new \Piwik\Tracker\Request(array());
$visitor = new \Piwik\Tracker\Visitor($request, '');
$visit = new FakeTrackerVisit($request);
$visit->handleNewVisit($visitor, null, false);
$info = $visit->getVisitorInfo();
$this->assertEquals('onNew2', $info['custom_dimension_2']);
$this->assertEquals('onNew3', $info['custom_dimension_3']);
$this->assertArrayNotHasKey('custom_dimension_1', $info); // on new visit returns false and should be ignored
$this->assertArrayNotHasKey('custom_dimension_4', $info); // on new visit not defined
// make sure visitor gets updated as well
$this->assertEquals('onNew2', $visitor->getVisitorColumn('custom_dimension_2'));
$this->assertEquals('onNew3', $visitor->getVisitorColumn('custom_dimension_3'));
$this->assertFalse($visitor->getVisitorColumn('custom_dimension_1'));
$this->assertFalse($visitor->getVisitorColumn('custom_dimension_4'));
}
public function test_handleNewVisitWithConversion_shouldTriggerDimensions()
{
$request = new \Piwik\Tracker\Request(array());
$visitor = new \Piwik\Tracker\Visitor($request, '');
$visit = new FakeTrackerVisit($request);
$visit->handleNewVisit($visitor, null, true);
$info = $visit->getVisitorInfo();
$this->assertEquals('onNew2', $info['custom_dimension_2']); // on converted visit returns false and should be ignored
$this->assertEquals('converted3', $info['custom_dimension_3']); // a conversion should overwrite an existing value
$this->assertArrayNotHasKey('custom_dimension_1', $info);
$this->assertArrayNotHasKey('custom_dimension_4', $info);
$this->assertEquals('converted3', $visitor->getVisitorColumn('custom_dimension_3'));
}
public function test_handleExistingVisitWithoutConversion_shouldTriggerDimensions()
{
$request = new \Piwik\Tracker\Request(array());
$visitor = new \Piwik\Tracker\Visitor($request, '');
$visit = new FakeTrackerVisit($request);
$visit->handleNewVisit($visitor, null, false);
$visit->handleExistingVisit($visitor, null, false);
$info = $visit->getVisitorInfo();
$this->assertEquals('existing1', $info['custom_dimension_1']);
$this->assertEquals('onNew2', $info['custom_dimension_2']); // on existing visit returns false and should be ignored/ not overwrite on new value
$this->assertEquals('existing3', $info['custom_dimension_3']);
$this->assertEquals('existing3extended', $info['custom_dimension_4']); // accesses a previously set column
// make sure visitor gets updated as well
$this->assertEquals('existing1', $visitor->getVisitorColumn('custom_dimension_1'));
$this->assertEquals('onNew2', $visitor->getVisitorColumn('custom_dimension_2'));
$this->assertEquals('existing3', $visitor->getVisitorColumn('custom_dimension_3'));
$this->assertEquals('existing3extended', $visitor->getVisitorColumn('custom_dimension_4'));
}
public function test_handleExistingVisitWithConversion_shouldTriggerDimensions()
{
$request = new \Piwik\Tracker\Request(array());
$visitor = new \Piwik\Tracker\Visitor($request, '');
$visit = new FakeTrackerVisit($request);
$visit->handleNewVisit($visitor, null, false);
$visit->handleExistingVisit($visitor, null, true);
$info = $visit->getVisitorInfo();
$this->assertEquals('existing1', $info['custom_dimension_1']);
$this->assertEquals('onNew2', $info['custom_dimension_2']); // on converted visit returns false and should be ignored
$this->assertEquals('converted3', $info['custom_dimension_3']); // a conversion should overwrite an existing value
$this->assertEquals('existing3extended', $info['custom_dimension_4']);
$this->assertEquals('converted3', $visitor->getVisitorColumn('custom_dimension_3'));
}
}
|