-
Notifications
You must be signed in to change notification settings - Fork 5
/
IframeFigures.js
149 lines (130 loc) · 4.23 KB
/
IframeFigures.js
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
import Trans from '@/components/translation/Trans'
import Card from '@/design-system/layout/Card'
import { useUser } from '@/publicodes-state'
export default function IframeFigures(props) {
const { user } = useUser()
if (!props.pages.length || !props.activePages.length) return
const [iframes, activeIframes] =
props.pages &&
props.activePages &&
getIframeRate(props.pages, props.activePages)
const [iframePages, totalIframe] =
props.pages && getIdentifiedIframes(props.pages)
return (
<div className="mt-4">
<div className="mb-4 grid w-full grid-cols-1 justify-center gap-4 md:grid-cols-2">
<Card>
<p className="mb-0 text-3xl font-bold">
{' '}
{Math.round(iframes).toLocaleString(user?.region?.code)}
<small> %</small>
</p>
<p className="text-sm">
<Trans>des visites affichées en iframe</Trans>
</p>
</Card>
<Card>
<p className="mb-0 text-3xl font-bold">
{' '}
{Math.round(activeIframes).toLocaleString(user?.region?.code) ||
'-'}
<small> %</small>
</p>
<p className="text-sm">
<Trans>des visites en iframe sont actives</Trans>
</p>
</Card>
</div>
<table className="statistics-iframe-table">
<tbody>
<tr>
<th>
<Trans>Intégrateurs identifiés</Trans>
</th>
<th>
<Trans>Visites</Trans>
</th>
<th>%</th>
</tr>
{iframePages &&
iframePages.map(
(line, index) =>
index < 5 && (
<tr key={line.label + line.entry_nb_visits}>
<td>{line.label}</td>
<td>
{line.entry_nb_visits
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, '\u00A0')}
</td>
<td>
{totalIframe &&
Math.round(
(line.entry_nb_visits / totalIframe) * 10000
) / 100}
%
</td>
</tr>
)
)}
</tbody>
</table>
<p className="text-right text-sm font-light">
<Trans>Données valables pour les 30 derniers jours</Trans>
</p>
</div>
)
}
// for iframe rate we consider as iframe url pages which include 'iframe' and inactive landing pages
const getIframeRate = (pages, activePages) => {
const totalPages = pages.reduce((acc, cur) => acc + cur.entry_nb_visits, 0)
const iframePages = pages.filter((page) => page.label?.includes('iframe'))
const totalIframe = iframePages.reduce(
(acc, cur) => acc + cur.entry_nb_visits,
0
)
const activeIframePages = activePages.filter((page) =>
page.label?.includes('iframe')
)
const totalActiveIframe = activeIframePages.reduce(
(acc, cur) => acc + cur.entry_nb_visits,
0
)
// const landingPage = pages.find((obj) => obj.label === '/index')
// const exitLandingPage = landingPage.exit_nb_visits * 0.6 // 60% of exit are considered as iframes (arbitrary value)
// const approximatedTotalIframes = exitLandingPage + totalIframe
const iframes = (totalIframe / totalPages) * 100
const activeIframes = (totalActiveIframe / totalIframe) * 100
return [iframes, activeIframes]
}
const getIdentifiedIframes = (pages) => {
const iframePages = pages
.filter((page) => page.label?.includes('iframe'))
.map((page) => {
if (!page.url) {
return [...page.subtable]
} else {
page.label = page.label.split('/')[3]
return page
}
})
.flat()
const combined = iframePages.reduce((a, obj) => {
if (!a[obj.label]) {
a[obj.label] = obj
return a
} else {
Object.entries(obj).map(
([key, value]) =>
key !== 'label' &&
(a[obj.label][key] = (a[obj.label][key] || 0) + value)
)
return a
}
}, {})
const totalIframe = Object.values(combined).reduce(
(acc, cur) => acc + cur.entry_nb_visits,
0
)
return [iframePages, totalIframe]
}