This is a sample script for retrieving “Users”, “Sessions” and “PageViews” of User Summary Report from Google Analytics using Google Apps Script. When you use this, please enable Analytics Reporting API at Advanced Google services.
Sample script
function myFunction() {
const viewId = "###";
const startDate = "2020-01-01";
const endDate = "2020-06-01";
const resource = {
reportRequests: [
{
viewId: viewId,
dateRanges: [{ startDate: startDate, endDate: endDate }],
metrics: [
{ expression: "ga:users" },
{ expression: "ga:sessions" },
{ expression: "ga:pageviews" },
],
},
],
};
const res = AnalyticsReporting.Reports.batchGet(resource, { fields: "*" });
console.log(res);
}
Result
{
"reports": [
{
"columnHeader": {
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:users",
"type": "INTEGER"
},
{
"type": "INTEGER",
"name": "ga:sessions"
},
{
"type": "INTEGER",
"name": "ga:pageviews"
}
]
}
},
"data": {
"maximums": [
{
"values": ["###", "###", "###"]
}
],
"rowCount": 1,
"totals": [
{
"values": ["###", "###", "###"]
}
],
"minimums": [
{
"values": ["###", "###", "###"]
}
],
"isDataGolden": true,
"rows": [
{
"metrics": [
{
"values": ["###", "###", "###"]
}
]
}
]
}
}
]
}