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
|
<?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\Fixtures;
use Piwik\Date;
use Piwik\Plugins\Goals\API as APIGoals;
use Piwik\Tests\Framework\Fixture;
use MatomoTracker;
/**
* Tracks custom events
*/
class ThreeVisitsWithCustomEvents extends Fixture
{
public $dateTime = '2010-01-03 11:22:33';
public $idSite = 1;
public static $idGoalTriggeredOnEventCategory = 3;
public function setUp(): void
{
$this->setUpWebsitesAndGoals();
$this->trackVisits();
}
private function setUpWebsitesAndGoals()
{
// tests run in UTC, the Tracker in UTC
if (!self::siteCreated($idSite = 1)) {
self::createWebsite($this->dateTime);
}
if (!self::goalExists($idSite = 1, $idGoal = 1)) {
// These two goals are to check events don't trigger for URL or Title matching
APIGoals::getInstance()->addGoal($this->idSite, 'triggered js', 'url', 'webradio', 'contains');
APIGoals::getInstance()->addGoal($this->idSite, 'triggered js', 'title', 'Music', 'contains');
$idGoalTriggeredOnEventCategory = APIGoals::getInstance()->addGoal($this->idSite, 'event matching', 'event_category', 'CategoryTriggersGoal', 'contains', false,
8, true, '', true);
$this->assertEquals($idGoalTriggeredOnEventCategory, self::$idGoalTriggeredOnEventCategory);
}
}
public function trackVisits()
{
$uselocal = false;
$vis = self::getTracker($this->idSite, $this->dateTime, $useDefault = true, $uselocal);
// $vis will start with a pageview, while $vis2 will directly start with the event
$vis->setUrl('http://example.org/webradio');
$vis->setGenerationTime(333);
self::checkResponse($vis->doTrackPageView('Welcome!'));
$this->trackMusicPlaying($vis);
$this->trackMusicRatings($vis);
$this->trackEventWithoutUrl($vis);
$this->trackMovieWatchingIncludingInterval($vis);
$this->dateTime = Date::factory($this->dateTime)->addHour(0.5);
$vis2 = self::getTracker($this->idSite, $this->dateTime, $useDefault = true, $uselocal);
$vis2->setUrl('http://example.com/piwik/');
$vis2->setIp('111.1.1.1');
$vis2->setPlugins($flash = false, $java = false, $director = true);
$this->trackMusicPlaying($vis2);
$this->trackMusicRatings($vis2);
$this->trackMovieWatchingIncludingInterval($vis2);
$this->dateTime = Date::factory($this->dateTime)->addDay(1);
$vis3 = self::getTracker($this->idSite, $this->dateTime, $useDefault = true, $uselocal);
$vis3->setUrl('http://example.com/piwik/');
$vis3->setIp('111.1.1.2');
$vis3->setPlugins($flash = false, $java = false, $director = true);
$this->trackMusicPlaying($vis3);
$this->trackMusicRatings($vis3);
$this->trackMovieWatchingIncludingInterval($vis3);
}
private function moveTimeForward(MatomoTracker $vis, $minutes)
{
$hour = $minutes / 60;
return $vis->setForceVisitDateTime(Date::factory($this->dateTime)->addHour($hour)->getDatetime());
}
protected function trackEventWithoutUrl(MatomoTracker $vis)
{
$url = $vis->pageUrl;
$vis->setUrl('');
// no value, use default revenue
$this->moveTimeForward($vis, 1);
self::checkResponse($vis->doTrackEvent('CategoryTriggersGoal here', 'This is an event without a URL', $name = false));
// event value
$this->moveTimeForward($vis, 2);
self::checkResponse($vis->doTrackEvent('CategoryTriggersGoal here', 'This is an event without a URL', $name = false, $value = 23));
// event value is 0, use 0
$this->moveTimeForward($vis, 3);
self::checkResponse($vis->doTrackEvent('CategoryTriggersGoal here', 'This is an event without a URL', $name = false, $value = 0));
$vis->setUrl($url);
}
protected function trackMusicPlaying(MatomoTracker $vis)
{
$this->moveTimeForward($vis, 1);
$this->setMusicEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Music', 'play', 'La fiancée de l\'eau'));
$this->moveTimeForward($vis, 2);
$this->setMusicEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Music', 'play25%', 'La fiancée de l\'eau'));
$this->moveTimeForward($vis, 3);
$this->setMusicEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Music', 'play50%', 'La fiancée de l\'eau'));
$this->moveTimeForward($vis, 4);
$this->setMusicEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Music', 'play75%', 'La fiancée de l\'eau'));
$this->moveTimeForward($vis, 4.5);
$this->setMusicEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Music', 'playEnd', 'La fiancée de l\'eau'));
}
protected function trackMusicRatings(MatomoTracker $vis)
{
$this->moveTimeForward($vis, 5);
$this->setMusicEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Music', 'rating', 'La fiancée de l\'eau', 9));
$this->moveTimeForward($vis, 5.02);
$this->setMusicEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Music', 'rating', 'La fiancée de l\'eau', 10));
}
protected function trackMovieWatchingIncludingInterval(MatomoTracker $vis)
{
// First a pageview so the time on page is tracked properly
$this->moveTimeForward($vis, 30);
$vis->setUrl('http://example.org/movies');
$vis->setGenerationTime(666);
self::checkResponse($vis->doTrackPageView('Movie Theater'));
$this->moveTimeForward($vis, 31);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'playTrailer', 'Princess Mononoke (もののけ姫)'));
$this->moveTimeForward($vis, 33);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'playTrailer', 'Ponyo (崖の上のポニョ)'));
$this->moveTimeForward($vis, 35);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'playTrailer', 'Spirited Away (千と千尋の神隠し)'));
$this->moveTimeForward($vis, 36);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'clickBuyNow', 'Spirited Away (千と千尋の神隠し)'));
$this->moveTimeForward($vis, 38);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'playStart', 'Spirited Away (千と千尋の神隠し)'));
$this->moveTimeForward($vis, 60);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'play25%', 'Spirited Away (千と千尋の神隠し)'));
// trackEvent without a name
$this->moveTimeForward($vis, 150);
self::checkResponse($vis->doTrackEvent('Movie', 'Search'));
// taking 2+ hours break & resuming this epic moment of cinema
$this->moveTimeForward($vis, 200);
$this->moveTimeForward($vis, 222);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'play50%', 'Spirited Away (千と千尋の神隠し)'));
$this->moveTimeForward($vis, 244);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'play75%', 'Spirited Away (千と千尋の神隠し)'));
$this->moveTimeForward($vis, 251);
self::checkResponse($vis->doTrackEvent('Movie', 'Search', 'Search query here'));
$this->moveTimeForward($vis, 266);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'playEnd', 'Spirited Away (千と千尋の神隠し)'));
// Test Events without a URL
$vis->setUrl('');
$this->moveTimeForward($vis, 268);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('Movie', 'rating', 'Spirited Away (千と千尋の神隠し)', 9.66));
// Test event with long names should be truncated
$vis->setUrl('http://example.org/finishedMovie');
$append = "Extremely long Extremely long Extremely long Extremely long Extremely long Extremely long Extremely long Extremely long Extremely long Extremely long";
$append .= " ---> SHOULD APPEAR IN TEST OUTPUT NOT TRUNCATED <--- ";
$this->moveTimeForward($vis, 280);
$this->setMovieEventCustomVar($vis);
self::checkResponse($vis->doTrackEvent('event category ' . $append, 'event action '.$append, 'event name '.$append, 9.66));
$this->moveTimeForward($vis, 352);
self::checkResponse($vis->doTrackEvent('Movie', 'Search'));
$this->moveTimeForward($vis, 453);
self::checkResponse($vis->doTrackEvent('Movie', 'Purchase'));
}
private function setMusicEventCustomVar(MatomoTracker $vis)
{
$vis->setCustomVariable($id = 1, $name = 'Page Scope Custom var', $value = 'should not appear in events report', $scope = 'page');
$vis->setCustomVariable($id = 1, $name = 'album', $value = 'En attendant les caravanes...', $scope = 'event');
$vis->setCustomVariable($id = 1, $name = 'genre', $value = 'World music', $scope = 'event');
}
private function setMovieEventCustomVar(MatomoTracker $vis)
{
$vis->setCustomVariable($id = 1, $name = 'country', $value = '日本', $scope = 'event');
$vis->setCustomVariable($id = 2, $name = 'genre', $value = 'Greatest animated films', $scope = 'event');
$vis->setCustomVariable($id = 4, $name = 'genre', $value = 'Adventure', $scope = 'event');
$vis->setCustomVariable($id = 5, $name = 'genre', $value = 'Family', $scope = 'event');
$vis->setCustomVariable($id = 5, $name = 'movieid', $value = 15763, $scope = 'event');
$vis->setCustomVariable($id = 1, $name = 'Visit Scope Custom var', $value = 'should not appear in events report Bis', $scope = 'visit');
}
public function tearDown(): void
{
}
}
|