TL;DR
How to run report on GA4 property in GAS? Do I use AnalyticsData namespace or Analytics? or am I doing something wrong with properties?!
report = AnalyticsAdmin.Properties.runReport(request,
'properties/' + 'zxc');
Logger.log('We dont get here ' + report);
I've seen this oldish question (similar to this question) but can't seem to use that:
Objective
The script is designed to automate the process of retrieving visitor count data from Google Analytics on a weekly basis and updating a Google Sheet with the results. The main steps include:
- Identifying the Google Analytics Account and Property: The script finds the relevant Google Analytics account and property (specifically the 'new' GA4 properties) by matching the account name and id from a Google Sheet.
- Listing Web Data Streams: It lists all web data streams (equivalent to views in Universal Analytics) for the identified property.
- Running a Report: The script attempts to run a report to retrieve the total visitor count (sessions) for a specified date range, typically the past week.
- Updating Google Sheet: The report results are intended to be added to the working sheet in a new column with the date as the header. The total visitors for the week are summed up at the bottom of the rows.
Challenges
The main challenges faced in the script include:
- Transitioning from the older Google Analytics API to the new Google Analytics Admin API and Data API.
- Generating the request for the Google Analytics API Executing the report request via Google
- Analytics API Debugging issues related to permissions and API method calls.
Status
The script is in the development phase, and the current focus is on resolving the error encountered while running the report to retrieve the visitor count data.
The final implementation will enable automated, weekly updates to the Google Sheet with visitor count data from the selected Google Analytics property, streamlining the reporting process for the organization.
Note: When running this request inthe GA Query Explorer I get a valid Json response:
{
"metricHeaders":[
{
"name":"sessions"
"type":"TYPE_INTEGER"
}
]
"rows":[
{
"metricValues":[...]
}
]
"rowCount":1
"metadata":{
"currencyCode":"ZZZ"
"timeZone":"XX/YY"
}
"kind":"analyticsData#runReport"
}
function getReportDataForProperty(view, property) {
Logger.log(view);
Logger.log(property);
// Extract the property ID from the property object
const matchedViewId = view.name.split('/')[3];
const matchedPropertyId = property.name.split('/')[1];
Logger.log(matchedViewId);
Logger.log(matchedPropertyId);
try {
const metrics = [
{ "name": "sessions" }
//See avaiable metrics https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema?hl=en#metrics
]
const dateRange = AnalyticsData.newDateRange();
dateRange.startDate = getLastNdays(7);
dateRange.endDate = 'today';
const request = AnalyticsReporting.newReportRequest();
request.metrics = metrics;
request.dateRanges = dateRange;
//request.property = "properties/zxc";
Logger.log(request);
// --------------------- SUPPORT NEEDED HERE ---------------------
report = AnalyticsAdmin.Properties.runReport(request,
'properties/' + 'zxc');
// I also tried report = AnalyticsData.Properties.runReport(request,
'properties/' + 'zxc');
// I also tried
// request.property = 'properties/ + propertyId'
// report = AnalyticsAdmin.Properties.runReport(request);
Logger.log('We dont get here ' + report);
// --------------------- SUPPORT NEEDED HERE ---------------------
if (!report.rows) {
Logger.log('No rows returned.');
return;
}
} catch (e) {
Logger.log(e)
// TODO (Developer) - Handle exception
Logger.log('Failed with error: %s', e.error);
}
}
// EDIT: I've had run the Google example before posting, but not shared details about this. I'm receiving the following error when running the code. The only thing I changed was the Property Id. As you can see in the 6 I have added the "AnalyticsData" API namespace to the script.
Execution log
1:29:32 PM Notice Execution started
1:29:33 PM Info Failed with error: undefined
1:29:32 PM Notice Execution completed