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
|
/*!
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
(function () {
angular.module('piwikApp').component('piwikComparisons', {
templateUrl: 'plugins/CoreHome/angularjs/comparisons/comparisons.component.html?cb=' + piwik.cacheBuster,
bindings: {
// empty
},
controller: ComparisonsController
});
ComparisonsController.$inject = ['piwikComparisonsService', '$rootScope', 'piwikApi', '$element', 'piwikUrl'];
function ComparisonsController(comparisonsService, $rootScope, piwikApi, $element, piwikUrl) {
var vm = this;
var comparisonTooltips = null;
vm.comparisonsService = comparisonsService;
vm.$onInit = $onInit;
vm.$onDestroy = $onDestroy;
vm.comparisonHasSegment = comparisonHasSegment;
vm.getComparisonPeriodType = getComparisonPeriodType;
vm.getComparisonTooltip = getComparisonTooltip;
function $onInit() {
$rootScope.$on('piwikComparisonsChanged', onComparisonsChanged);
onComparisonsChanged();
setUpTooltips();
}
function $onDestroy() {
try {
$element.tooltip('destroy');
} catch (e) {
// ignore
}
}
function setUpTooltips() {
$element.tooltip({
track: true,
content: function() {
var title = $(this).attr('title');
return piwikHelper.escape(title.replace(/\n/g, '<br />'));
},
show: {delay: 200, duration: 200},
hide: false
});
}
function comparisonHasSegment(comparison) {
return typeof comparison.params.segment !== 'undefined';
}
function getComparisonPeriodType(comparison) {
var period = comparison.params.period;
if (period === 'range') {
return _pk_translate('CoreHome_PeriodRange');
}
var periodStr = _pk_translate('Intl_Period' + period.substring(0, 1).toUpperCase() + period.substring(1));
return periodStr.substring(0, 1).toUpperCase() + periodStr.substring(1);
}
function getComparisonTooltip(segmentComparison, periodComparison) {
if (!comparisonTooltips
|| !Object.keys(comparisonTooltips).length
) {
return undefined;
}
return comparisonTooltips[periodComparison.index][segmentComparison.index];
}
function onComparisonsChanged() {
comparisonTooltips = null;
if (!vm.comparisonsService.isComparing()) {
return;
}
var periodComparisons = comparisonsService.getPeriodComparisons();
var segmentComparisons = comparisonsService.getSegmentComparisons();
piwikApi.fetch({
method: 'API.getProcessedReport',
apiModule: 'VisitsSummary',
apiAction: 'get',
compare: '1',
compareSegments: piwikUrl.getSearchParam('compareSegments'),
comparePeriods: piwikUrl.getSearchParam('comparePeriods'),
compareDates: piwikUrl.getSearchParam('compareDates'),
format_metrics: '1',
}).then(function (report) {
comparisonTooltips = {};
periodComparisons.forEach(function (periodComp) {
comparisonTooltips[periodComp.index] = {};
segmentComparisons.forEach(function (segmentComp) {
comparisonTooltips[periodComp.index][segmentComp.index] = generateComparisonTooltip(report, periodComp, segmentComp);
});
});
});
}
function generateComparisonTooltip(visitsSummary, periodComp, segmentComp) {
if (!visitsSummary.reportData.comparisons) { // sanity check
return '';
}
var firstRowIndex = comparisonsService.getComparisonSeriesIndex(periodComp.index, 0);
var firstRow = visitsSummary.reportData.comparisons[firstRowIndex];
var comparisonRowIndex = comparisonsService.getComparisonSeriesIndex(periodComp.index, segmentComp.index);
var comparisonRow = visitsSummary.reportData.comparisons[comparisonRowIndex];
var firstPeriodRow = visitsSummary.reportData.comparisons[segmentComp.index];
var tooltip = '<div class="comparison-card-tooltip">';
var visitsPercent = ((comparisonRow.nb_visits / firstRow.nb_visits) * 100).toFixed(2) + '%';
tooltip += _pk_translate('General_ComparisonCardTooltip1', [
"'" + comparisonRow.compareSegmentPretty + "'",
comparisonRow.comparePeriodPretty,
visitsPercent,
comparisonRow.nb_visits,
firstRow.nb_visits
]);
if (periodComp.index > 0) {
tooltip += '<br/><br/>';
tooltip += _pk_translate('General_ComparisonCardTooltip2', [
comparisonRow.nb_visits_change,
firstPeriodRow.compareSegmentPretty,
firstPeriodRow.comparePeriodPretty
]);
}
tooltip += '</div>';
return tooltip;
}
}
})();
|