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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
/*!
* Matomo - free/libre analytics platform
*
* Real time visitors map
* Using Kartograph.js http://kartograph.org/
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
(function () {
var UIControl = require('piwik/UI').UIControl;
var RealtimeMap = window.UserCountryMap.RealtimeMap = function (element) {
UIControl.call(this, element);
this._init();
this.run();
};
RealtimeMap.initElements = function () {
UIControl.initElements(this, '.RealTimeMap');
};
$.extend(RealtimeMap.prototype, UIControl.prototype, {
_init: function () {
var $element = this.$element;
this.config = JSON.parse($element.attr('data-config'));
// If the map is loaded from the menu, do a few tweaks to clean up the display
if ($element.attr('data-standalone') == 1) {
this._initStandaloneMap();
}
// handle widgetry
if ($('#dashboardWidgetsArea').length) {
var $widgetContent = $element.closest('.widgetContent');
var self = this;
$widgetContent.on('widget:maximise', function () {
self.resize();
}).on('widget:minimise', function () {
self.resize();
});
}
// set unique ID for kartograph map div
this.uniqueId = 'RealTimeMap_map-' + this._controlId;
$('.RealTimeMap_map', $element).attr('id', this.uniqueId);
// create the map
this.map = $K.map('#' + this.uniqueId);
$element.focus();
},
_initStandaloneMap: function () {
var $rootScope = piwikHelper.getAngularDependency('$rootScope');
$rootScope.$emit('hidePeriodSelector');
$('.realTimeMap_overlay').css('top', '0px');
$('.realTimeMap_datetime').css('top', '20px');
},
run: function () {
var self = this,
config = self.config,
_ = config._,
map = self.map,
maxVisits = config.maxVisits || 100,
changeVisitAlpha = typeof config.changeVisitAlpha === 'undefined' ? true : config.changeVisitAlpha,
removeOldVisits = typeof config.removeOldVisits === 'undefined' ? true : config.removeOldVisits,
doNotRefreshVisits = typeof config.doNotRefreshVisits === 'undefined' ? false : config.doNotRefreshVisits,
enableAnimation = typeof config.enableAnimation === 'undefined' ? true : config.enableAnimation,
forceNowValue = typeof config.forceNowValue === 'undefined' ? false : +config.forceNowValue,
lastTimestamp = -1,
lastVisits = [],
visitSymbols,
tokenAuth = '' + config.reqParams.token_auth,
oldest,
isFullscreenWidget = $('.widget').parent().get(0) == document.body,
now,
nextReqTimer,
symbolFadeInTimer = [],
colorMode = 'default',
currentMap = 'world',
yesterday = false,
userHasZoomed = false,
colorManager = piwik.ColorManager,
colors = colorManager.getColors('realtime-map', ['white-bg', 'white-fill', 'black-bg', 'black-fill', 'visit-stroke',
'website-referrer-color', 'direct-referrer-color', 'search-referrer-color',
'live-widget-highlight', 'live-widget-unhighlight', 'symbol-animate-fill', 'region-stroke-color']),
currentTheme = 'white',
colorTheme = {
white: {
bg: colors['white-bg'],
fill: colors['white-fill']
},
black: {
bg: colors['black-bg'],
fill: colors['black-fill']
}
},
visitStrokeColor = colors['visit-stroke'],
referrerColorWebsite = colors['referrer-color-website'],
referrerColorDirect = colors['referrer-color-direct'],
referrerColorSearch = colors['referrer-color-search'],
liveWidgetHighlightColor = colors['live-widget-highlight'],
liveWidgetUnhighlightColor = colors['live-widget-unhighlight'],
symbolAnimateFill = colors['symbol-animate-fill']
;
self.widget = $('#widgetRealTimeMaprealtimeMap').parent();
var preset = self.widget.dashboardWidget('getWidgetObject').parameters;
if (preset) {
currentTheme = preset.colorTheme;
colorMode = preset.colorMode;
currentMap = preset.lastMap;
}
/*
* returns the parameters for API calls, extended from
* self.reqParams which is set in template
*/
function _reportParams(firstRun) {
return $.extend(config.reqParams, {
module: 'API',
method: 'Live.getLastVisitsDetails',
filter_limit: maxVisits,
showColumns: ['latitude', 'longitude', 'actions', 'lastActionTimestamp',
'visitLocalTime', 'city', 'country', 'countryCode', 'referrerType', 'referrerName',
'referrerTypeName', 'browserIcon', 'operatingSystemIcon', 'deviceType', 'deviceModel',
'countryFlag', 'idVisit', 'actionDetails', 'continentCode',
'actions', 'searches', 'goalConversions', 'visitorId', 'userId'].join(','),
minTimestamp: firstRun ? 0 : lastTimestamp
});
}
/*
* wrapper around jQuery.ajax, moves token_auth parameter
* to POST data while keeping other parameters as GET
*/
function ajax(params) {
delete params['token_auth'];
return $.ajax({
url: 'index.php?' + $.param(params),
dataType: 'json',
data: { token_auth: tokenAuth, force_api_session: '1' },
type: 'POST'
});
}
/*
* updateMap is called by renderCountryMap() and renderWorldMap()
*/
function _updateMap(svgUrl, callback) {
if (svgUrl === undefined) return;
map.loadMap(config.svgBasePath + svgUrl, function () {
map.clear();
self.resize();
callback();
$('.ui-tooltip').remove(); // remove all existing tooltips
}, { padding: -3});
}
/*
* to ensure that onResize is not called a hundred times
* while resizing the browser window, this functions
* makes sure to only call onResize at the end
*/
function onResizeLazy() {
clearTimeout(self._resizeTimer);
self._resizeTimer = setTimeout(self.resize.bind(self), 300);
}
/*
* returns value betwddn 0..1, where 1 means that the
* visit is fresh, and 0 means the visit is almost gone
* from the map
*/
function age(r) {
var nowSecs = Math.floor(now);
var o = (r.lastActionTimestamp - oldest) / (nowSecs - oldest);
return Math.min(1, Math.max(0, o));
}
function relativeTime(ds) {
var val = function (val) { return '<strong>' + Math.round(val) + '</strong>'; };
return (ds < 90 ? _.seconds_ago.replace('%s', val(ds))
: ds < 5400 ? _.minutes_ago.replace('%s', val(ds / 60))
: ds < 129600 ? _.hours_ago.replace('%s', val(ds / 3600))
: _.days_ago.replace('%s', val(ds / 86400)));
}
/*
* returns the content of the tooltip displayed for each
* visitor on the map
*/
function visitTooltip(r) {
var ds = new Date().getTime() / 1000 - r.lastActionTimestamp,
ad = r.actionDetails,
ico = function (src) { return '<img height="16px" src="' + src + '" alt="" class="icon" /> '; };
return '<h3>' + (r.city ? $('<span>').text(r.city).html() + ' / ' : '') + $('<span>').text(r.country).html() + '</h3>' +
// icons
ico(r.countryFlag) + ico(r.browserIcon) + ico(r.operatingSystemIcon) + '<br/>' +
// device type, model, brand
r.deviceType + ' (' + r.deviceModel + ')<br/>' +
// User ID
(r.userId ? _pk_translate('General_UserId') + ': ' + $('<span>').text(r.userId).html() + '<br/>' : '') +
// last action
(ad && ad.length && ad[ad.length - 1].pageTitle ? '' + $('<span>').text(ad[ad.length - 1].pageTitle).html() + '<br/>' : '') +
// time of visit
'<div class="rel-time" data-actiontime="' + r.lastActionTimestamp + '">' + relativeTime(ds) + '</div>' +
// either from or direct
(r.referrerType == "direct" ? r.referrerTypeName :
_.from + ': ' + $('<span>').text(r.referrerName).html()) + '<br />' +
// local time
'<small>' + _.local_time + ': ' + r.visitLocalTime + '</small><br />' +
// goals, if available
(self.config.siteHasGoals && r.goalConversions ? '<small>' + _.goal_conversions.replace('%s', '<strong>' + r.goalConversions + '</strong>') +
(r.searches > 0 ? ', ' + _.searches.replace('%s', r.searches) : '') + '</small><br />' : '') +
// actions and searches
'<small>' + _.actions.replace('%s', '<strong>' + r.actions + '</strong>') +
(r.searches > 0 ? ', ' + _.searches.replace('%s', '<strong>' + r.searches + '</strong>') : '') + '</small>';
}
/*
* the radius of the symbol depends on the lastActionTimestamp
*/
function visitRadius(r) {
return Math.pow(age(r), 4) * (self.maxRad - self.minRad) + self.minRad;
}
/*
* defines the color of the map symbols.
* depends on colorMode, which is set to 'default'
* unless you type Shift+Alt+C
*/
function visitColor(r) {
var col,
engaged = self.config.siteHasGoals ? r.goalConversions > 0 : r.actions > 4;
if (colorMode == 'referrerType') {
col = ({
website: referrerColorWebsite,
direct: referrerColorDirect,
search: referrerColorSearch
})[r.referrerType];
}
// defu
else col = chroma.hsl(
42 * age(r), // hue
Math.sqrt(age(r)), // saturation
(engaged ? 0.65 : 0.5) - (1 - age(r)) * 0.45 // lightness
);
return col;
}
/*
* attributes of the map symbols
*/
function visitSymbolAttrs(r) {
var result = {
fill: visitColor(r).hex(),
stroke: visitStrokeColor,
'stroke-width': 1 * age(r),
r: visitRadius(r),
cursor: 'pointer'
};
if (changeVisitAlpha) {
result['fill-opacity'] = Math.pow(age(r), 2) * 0.8 + 0.2;
result['stroke-opacity'] = Math.pow(age(r), 1.7) * 0.8 + 0.2;
}
return result;
}
/*
* eventually highlights the row in LiveVisitors widget
* that corresponds to a visit on the map
*/
function highlightVisit(r) {
$('#visitsLive').find('li#' + r.idVisit + ' .datetime')
.css('background', liveWidgetHighlightColor);
}
/*
* removes the highlight after the mouse left
* the visit marker on the map
*/
function unhighlightVisit(r) {
$('#visitsLive').find('li#' + r.idVisit + ' .datetime')
.css({ background: liveWidgetUnhighlightColor });
}
/*
* create a nice popping animation for appearing
* visit symbols.
*/
function animateSymbol(s) {
// create a white outline and explode it
var c = map.paper.circle().attr(s.path.attrs);
c.insertBefore(s.path);
c.attr({ fill: false });
c.animate({ r: c.attrs.r * 3, 'stroke-width': 7, opacity: 0 }, 2500,
'linear', function () { c.remove(); });
// ..and pop the bubble itself
var col = s.path.attrs.fill,
rad = s.path.attrs.r;
s.path.show();
s.path.attr({ fill: symbolAnimateFill, r: 0.1, opacity: 1 });
s.path.animate({ fill: col, r: rad }, 700, 'bounce');
}
// default click behavior. if a visit is clicked, the visitor profile is launched,
// otherwise zoom in or out.
// TODO: visitor profile launching logic should probably be contained in
// visitorProfile.js. not sure how to do that, though...
this.$element.on('mapClick', function (e, visit, mapPath) {
var VisitorProfileControl = require('piwik/UI').VisitorProfileControl;
if (visit
&& VisitorProfileControl
&& !self.$element.closest('.visitor-profile').length
) {
VisitorProfileControl.showPopover(visit.visitorId);
} else {
var cont = UserCountryMap.cont2cont[mapPath.data.continentCode];
if (cont && cont != currentMap) {
updateMap(cont);
}
}
});
/*
* this function requests new data from Live.getLastVisitsDetails
* and updates the symbols on the map. Then, it sets a timeout
* to call itself after the refresh time set by Matomo
*
* If firstRun is true, the SymbolGroup is initialized
*/
function refreshVisits(firstRun) {
if (lastTimestamp != -1
&& doNotRefreshVisits
&& !firstRun
) {
return;
}
/*
* this is called after new visit reports came in
*/
function gotNewReport(report) {
// if the map has been destroyed, do nothing
if (!self.map || !self.$element.length || !$.contains(document, self.$element[0])) {
return;
}
// successful request, so set timeout for next API call
nextReqTimer = setTimeout(refreshVisits, config.liveRefreshAfterMs);
// hide loading indicator
$('.realTimeMap_overlay img').hide();
$('.realTimeMap_overlay .loading_data').hide();
// store current timestamp
now = forceNowValue || (new Date().getTime() / 1000);
if (firstRun) { // if we run this the first time, we initialiize the map symbols
visitSymbols = map.addSymbols({
data: [],
type: $K.Bubble,
/*title: function(d) {
return visitRadius(d) > 15 && d.actions > 1 ? d.actions : '';
},
labelattrs: {
fill: '#fff',
'font-weight': 'bold',
'font-size': 11,
stroke: false,
cursor: 'pointer'
},*/
sortBy: function (r) { return r.lastActionTimestamp; },
radius: visitRadius,
location: function (r) { return [r.longitude, r.latitude]; },
attrs: visitSymbolAttrs,
tooltip: visitTooltip,
mouseenter: highlightVisit,
mouseleave: unhighlightVisit,
click: function (visit, mapPath, evt) {
evt.stopPropagation();
self.$element.trigger('mapClick', [visit, mapPath]);
}
});
// clear existing report
lastVisits = [];
}
if (report.length) {
// filter results without location
report = $.grep(report, function (r) {
return r.latitude !== null;
});
// show warning if no visits w/ latitude
$('#realTimeMapNoVisitsInfo').toggle(!report.length);
}
// check whether we got any geolocated visits left
if (!report.length) {
$('.realTimeMap_overlay .showing_visits_of').hide();
$('.realTimeMap_overlay .no_data').show();
return;
} else {
$('.realTimeMap_overlay .showing_visits_of').show();
$('.realTimeMap_overlay .no_data').hide();
if (yesterday === false) {
yesterday = report[0].lastActionTimestamp - 24 * 60 * 60;
}
lastVisits = [].concat(report).concat(lastVisits).slice(0, maxVisits);
oldest = Math.max(lastVisits[lastVisits.length - 1].lastActionTimestamp, yesterday);
// let's try a different strategy
// remove symbols that are too old
var _removed = 0;
if (removeOldVisits) {
visitSymbols.remove(function (r) {
if (r.lastActionTimestamp < oldest) _removed++;
return r.lastActionTimestamp < oldest;
});
}
// update symbols that remain
visitSymbols.update({
radius: function (d) { return visitSymbolAttrs(d).r; },
attrs: visitSymbolAttrs
}, true);
// add new symbols
var newSymbols = [];
$.each(report, function (i, r) {
newSymbols.push(visitSymbols.add(r));
});
visitSymbols.layout().render();
if (enableAnimation) {
$.each(newSymbols, function (i, s) {
if (i > 10) return false;
s.path.hide(); // hide new symbol at first
var t = setTimeout(function () { animateSymbol(s); },
1000 * (s.data.lastActionTimestamp - now) + config.liveRefreshAfterMs);
symbolFadeInTimer.push(t);
});
}
lastTimestamp = report[0].lastActionTimestamp;
// show
var dur = lastTimestamp - oldest, d;
if (dur < 60) d = dur + ' ' + _.seconds;
else if (dur < 3600) d = Math.ceil(dur / 60) + ' ' + _.minutes;
else d = Math.ceil(dur / 3600) + ' ' + _.hours;
$('.realTimeMap_timeSpan').html(d);
if (!userHasZoomed) {
// we only apply auto zoom when user has not zoomed manually
var usedContinents = [];
var usedCountries = [];
var aSymbol;
for (var z = 0; z < visitSymbols.symbols.length; z++) {
aSymbol = visitSymbols.symbols[z];
if (aSymbol && aSymbol.data) {
if (aSymbol.data.continentCode && -1 === usedContinents.indexOf(aSymbol.data.continentCode)) {
usedContinents.push(aSymbol.data.continentCode);
}
if (aSymbol.data.countryCode && -1 === usedCountries.indexOf(aSymbol.data.countryCode)) {
usedCountries.push(aSymbol.data.countryCode);
}
}
}
if (usedCountries.length === 1 && usedCountries[0] && usedCountries[0] !== 'unk') {
updateMap(UserCountryMap.ISO2toISO3[usedCountries[0].toUpperCase()], false);
} else if (usedContinents.length === 1 && usedContinents[0] && usedContinents[0] !== 'unk') {
updateMap(UserCountryMap.cont2cont[usedContinents[0]], false);
}
}
}
firstRun = false;
}
if (firstRun && lastVisits.length) {
// zoom changed, use cached report data
gotNewReport(lastVisits.slice());
} else if (Visibility.hidden()) {
nextReqTimer = setTimeout(refreshVisits, config.liveRefreshAfterMs);
} else {
// request API for new data
$('.realTimeMap_overlay img').show();
ajax(_reportParams(firstRun)).done(gotNewReport);
}
}
/*
* Set up the base map after loading of the SVG. Adds a single layer
* that shows countries in gray with white outlines. Also this is where
* the zoom behaviour is initialized.
*/
function initMap() {
$('#widgetRealTimeMapliveMap .loadingPiwik, .RealTimeMap .loadingPiwik').hide();
map.addLayer(currentMap.length == 3 ? 'context' : 'countries', {
styles: {
fill: colorTheme[currentTheme].fill,
stroke: colorTheme[currentTheme].bg,
'stroke-width': 0.2
},
click: function (d, p, evt) {
evt.stopPropagation();
userHasZoomed = true;
if (currentMap.length == 2){ // zoom to country
updateMap(d.iso);
} else if (currentMap != 'world') { // zoom out if zoomed in
updateMap('world');
} else { // or zoom to continent view otherwise
updateMap(UserCountryMap.ISO3toCONT[d.iso]);
}
},
title: function (d) {
// return the country name for educational purpose
return d.name;
}
});
if (currentMap.length == 3){
map.addLayer('regions', {
styles: {
stroke: colors['region-stroke-color']
}
});
}
refreshVisits(true);
}
function storeSettings() {
self.widget.dashboardWidget('setParameters', {
lastMap: currentMap, theme: colorTheme, colorMode: colorMode
});
}
/*
* updates the map view (after changing the zoom)
* clears all existing timeouts
*/
function updateMap(_map, _storeSettings) {
if ('undefined' === typeof _storeSettings) {
_storeSettings = true;
}
if (_map && currentMap === _map && _map !== 'world') {
return;
}
clearTimeout(nextReqTimer);
$.each(symbolFadeInTimer, function (i, t) {
clearTimeout(t);
});
symbolFadeInTimer = [];
try {
map.removeSymbols();
} catch (e) {}
currentMap = _map;
_updateMap(currentMap + '.svg', initMap);
if (_storeSettings) {
storeSettings();
}
}
updateMap(location.hash && (location.hash == '#world' || location.hash.match(/^#[A-Z]{2,3}$/)) ? location.hash.substr(1) : 'world'); // TODO: restore last state
// clicking on map background zooms out
$('.RealTimeMap_map', this.$element).off('click').click(function () {
if (currentMap != 'world') {
userHasZoomed = true;
updateMap('world');
}
});
// secret gimmick shortcuts
this.$element.on('keydown', function (evt) {
// shift+alt+C changes color mode
if (evt.shiftKey && evt.altKey && evt.keyCode == 67) {
colorMode = ({
'default': 'referrerType',
referrerType: 'default'
})[colorMode];
storeSettings();
}
function switchTheme() {
self.$element.css({ background: colorTheme[currentTheme].bg });
if (isFullscreenWidget) {
$('body').css({ background: colorTheme[currentTheme].bg });
$('.widget').css({ 'border-width': 1 });
}
map.getLayer('countries')
.style('fill', colorTheme[currentTheme].fill)
.style('stroke', colorTheme[currentTheme].bg);
storeSettings();
}
// shift+alt+B: switch to black background
if (evt.shiftKey && evt.altKey && evt.keyCode == 66) {
currentTheme = 'black';
switchTheme();
}
// shift+alt+W: return to white background
if (evt.shiftKey && evt.altKey && evt.keyCode == 87) {
currentTheme = 'white';
switchTheme();
}
});
// make sure the map adapts to the widget size
$(window).on('resize.' + this.uniqueId, onResizeLazy);
// setup automatic tooltip updates
this._tooltipUpdateInterval = setInterval(function () {
$('.qtip .rel-time').each(function (i, el) {
el = $(el);
var ds = new Date().getTime() / 1000 - el.data('actiontime');
el.html(relativeTime(ds));
});
var d = new Date(), datetime = d.toTimeString().substr(0, 8);
$('.realTimeMap_datetime').html(datetime);
}, 1000);
},
/*
* resizes the map to widget dimensions
*/
resize: function () {
var ratio, w, h, map = this.map;
ratio = map.viewAB.width / map.viewAB.height;
w = map.container.width();
h = Math.min(w / ratio, $(window).height() - 30);
var radScale = Math.pow((h * ratio * h) / 130000, 0.3);
this.maxRad = 10 * radScale;
this.minRad = 4 * radScale;
map.container.height(h - 2);
map.resize(w, h);
if (map.symbolGroups && map.symbolGroups.length > 0) {
map.symbolGroups[0].update();
}
if (w < 355) $('.UserCountryMap .tableIcon span').hide();
else $('.UserCountryMap .tableIcon span').show();
},
_destroy: function () {
UIControl.prototype._destroy.call(this);
if (this._tooltipUpdateInterval) {
clearInterval(this._tooltipUpdateInterval);
}
$(window).off('resize.' + this.uniqueId);
this.map.clear();
$(this.map.container).html('');
delete this.map;
}
});
}());
|