Created
March 13, 2023 04:26
-
-
Save wuriyanto48/6efe97760d7a4123d5d95835bef48360 to your computer and use it in GitHub Desktop.
Nodejs: Fetch Google Analytic 4 data with google-analytics/data module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { BetaAnalyticsDataClient } = require('@google-analytics/data'); | |
const PROPERTY_ID = '292104555'; | |
const analyticsDataClient = new BetaAnalyticsDataClient({ | |
keyFile: 'credentials.json' | |
}); | |
// Runs a simple report. | |
async function runReport() { | |
const [response] = await analyticsDataClient.runReport({ | |
property: `properties/${PROPERTY_ID}`, | |
dateRanges: [ | |
{ | |
startDate: '2023-01-01', | |
endDate: 'today', | |
}, | |
], | |
dimensions: [ | |
{ | |
name: 'city', | |
}, | |
{ | |
name: 'mobileDeviceBranding' | |
}, | |
{ | |
name: 'platform' | |
}, | |
{ | |
name: 'operatingSystem' | |
}, | |
{ | |
name: 'mobileDeviceModel' | |
}, | |
], | |
metrics: [ | |
{ | |
name: 'activeUsers', | |
}, | |
], | |
}); | |
console.log('Report result:'); | |
response.rows.forEach(row => { | |
console.log(row); | |
}); | |
// console.log(response); | |
} | |
runReport(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment