Last active
January 11, 2024 12:04
-
-
Save souvikinator/dfbb56b732a719c55315ce75cba8c5fa to your computer and use it in GitHub Desktop.
GA4 (Google Analytics) report using api (using service account)
This file contains 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 credentials = require("./sa1.json"); | |
const fs = require("fs"); | |
const propertyId = "301145746"; | |
// console.log(credentials); | |
const numberWithCommas = (x) => { | |
try { | |
return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","); | |
} catch (err) { | |
throw err; | |
} | |
}; | |
/** | |
* | |
* @param {BetaAnalyticsDataClient} analyticsDataClient | |
* @param {*} startDate | |
* @param {*} endDate | |
* @returns | |
*/ | |
const getAnalyticsData = async ( | |
analyticsDataClient, | |
startDate = "", | |
endDate = "" | |
) => { | |
try { | |
let dateRange = { | |
startDate: startDate ? startDate : "2023-12-13", | |
endDate: endDate ? endDate : "yesterday", | |
}; | |
const [response] = await analyticsDataClient.runReport({ | |
property: `properties/${propertyId}`, | |
dateRanges: [dateRange], | |
dimensions: [{ name: "platform" }], | |
metrics: [ | |
{ name: "activeUsers" }, | |
// { name: "active1DayUsers" }, | |
// { name: "active7DayUsers" }, | |
{ name: "active28DayUsers" }, | |
// { name: "newUsers" }, | |
// { name: "totalUsers" }, | |
// { name: "crashAffectedUsers" }, | |
// { name: "averageSessionDuration" }, | |
], | |
}); | |
console.log(JSON.stringify(response, null, 2)); | |
let report = {}; | |
response.rows.forEach((row) => { | |
report.activeUsers = numberWithCommas(row.metricValues[0].value); | |
report.active1DayUsers = numberWithCommas(row.metricValues[1].value); | |
report.active7DayUsers = numberWithCommas(row.metricValues[2].value); | |
report.active28DayUsers = numberWithCommas(row.metricValues[3].value); | |
report.newUsers = numberWithCommas(row.metricValues[4].value); | |
report.totalUsers = numberWithCommas(row.metricValues[5].value); | |
report.crashAffectedUsers = numberWithCommas(row.metricValues[6].value); | |
let engagementDuration = parseFloat(row.metricValues[7].value); | |
engagementDuration = | |
engagementDuration >= 3600 | |
? `${(engagementDuration / 3600).toFixed(2)} hrs` | |
: engagementDuration < 3600 | |
? `${(engagementDuration / 60).toFixed(2)} mins` | |
: `${engagementDuration.toFixed(2)} secs`; | |
report.userEngagementDuration = engagementDuration; | |
}); | |
return report; | |
} catch (err) { | |
throw err; | |
} | |
}; | |
(async () => { | |
try { | |
const analyticsDataClient = new BetaAnalyticsDataClient({ | |
credentials, | |
}); | |
const report = await getAnalyticsData(analyticsDataClient); | |
// write to file | |
fs.writeFileSync("./report.json", JSON.stringify(report, null, 2)); | |
} catch (error) { | |
console.log(error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment