-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (66 loc) · 2.03 KB
/
index.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
import { writeFileSync } from 'fs';
import { orderBy } from 'lodash-es';
import { formatISO, startOfYesterday, subDays } from 'date-fns';
import { BetaAnalyticsDataClient } from '@google-analytics/data';
if (process.env.NODE_ENV !== 'production') {
await import('dotenv/config');
}
const propertyId = process.env.GA4_PROPERTY_ID;
const analyticsDataClient = new BetaAnalyticsDataClient({
credentials: {
client_email: process.env.CLIENT_EMAIL,
private_key: process.env.PRIVATE_KEY?.split(String.raw`\n`)?.join('\n'),
},
});
async function runReport() {
const [response] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
dimensions: [{ name: 'eventName' }, { name: 'customEvent:ce_youtube' }],
metrics: [{ name: 'eventCount' }],
dateRanges: [
{
startDate: formatISO(subDays(startOfYesterday(), 7), {
representation: 'date',
}),
endDate: formatISO(startOfYesterday(), { representation: 'date' }),
},
],
dimensionFilter: {
filter: {
fieldName: 'eventName',
inListFilter: {
values: [
'ce_complete_queue_video',
'ce_loop_embedded_video',
'ce_view_embedded_video',
],
},
},
},
orderBys: [{ metric: { metricName: 'eventCount' } }],
});
return processTop25Bgm(response);
}
export function processTop25Bgm(response, n = 25) {
const bgmMap = new Map();
response.rows.forEach((row) => {
const id = row?.dimensionValues?.[1]?.value;
if (!id) return;
const count = Number(row?.metricValues?.[0]?.value ?? 0);
if (bgmMap.has(id)) {
bgmMap.set(id, bgmMap.get(id) + count);
} else {
bgmMap.set(id, count);
}
});
const sorted = orderBy(
Array.from(bgmMap, ([track, count]) => ({ track, count })),
'count',
'desc'
);
return { time: formatISO(new Date()), data: sorted.slice(0, n) };
}
export async function main() {
const result = await runReport();
writeFileSync('top25.json', JSON.stringify(result));
}