-
Notifications
You must be signed in to change notification settings - Fork 0
/
GAserver.js
59 lines (51 loc) · 1.73 KB
/
GAserver.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
const express = require('express');
const { google } = require('googleapis');
const app = express();
const PORT = process.env.PORT || 5000;
// Replace this with your GA4 Property ID
const GA4_PROPERTY_ID = 'YOUR_GA4_PROPERTY_ID';
async function getPageViews(req, res) {
try {
const auth = new google.auth.GoogleAuth({
keyFile: 'path-to-your-service-account.json', // Replace with the path to your service account key
scopes: ['https://www.googleapis.com/auth/analytics.readonly'],
});
const analyticsDataClient = google.analyticsdata({
version: 'v1beta', // GA4 uses 'v1beta'
auth: await auth.getClient(),
});
const result = await analyticsDataClient.properties.runReport({
property: `properties/${GA4_PROPERTY_ID}`, // This is the GA4 property format
requestBody: {
dateRanges: [
{
startDate: '30daysAgo',
endDate: 'today',
},
],
dimensions: [{ name: 'pagePath' }],
metrics: [{ name: 'screenPageViews' }],
dimensionFilter: {
filter: {
fieldName: 'pagePath',
stringFilter: {
matchType: 'EXACT',
value: req.query.pagePath || '/',
},
},
},
},
});
// Extract page views for the specified page path
const pageViews =
result.data.rows && result.data.rows.length > 0
? result.data.rows[0].metricValues[0].value
: 0;
res.json({ pageViews });
} catch (error) {
console.error('Error fetching page views:', error);
res.status(500).send('Error fetching page views');
}
}
app.get('/api/page-views', getPageViews);
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));