Skip to content

Instantly share code, notes, and snippets.

@wuriyanto48
Created March 13, 2023 04:26
Show Gist options
  • Save wuriyanto48/6efe97760d7a4123d5d95835bef48360 to your computer and use it in GitHub Desktop.
Save wuriyanto48/6efe97760d7a4123d5d95835bef48360 to your computer and use it in GitHub Desktop.
Nodejs: Fetch Google Analytic 4 data with google-analytics/data module
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