How to set up Google Analytics REST API import to Google Analytics Data API v1
Documentation page from Google about migrating to “Google Analytics Data API v1” can be found here: https://developers.google.com/analytics/devguides/migration/api/reporting-ua-to-ga4
A REST API application definition example can be found here: Google Analytics Data API v1 application example.txt
After importing the definition adjust Property ID in the URL, Request body and Client ID and Client secret and the import should be working.
Application screenshots, example Request body and necessary Custom javascript code can be found below:
Example Request Body:
{
"dateRanges":[
{
"startDate":"2015-08-14",
"endDate":"today"
}],
"dimensions":[
{
"name":"date"
},
{
"name":"eventName"
}],
"metrics":[
{
"name":"sessions"
},
{
"name":"screenPageViews"
}
],
"offset": "{{offset}}",
"limit": "{{limit}}"
}
Custom javascript code:
function formatDate(s) {
return s.substr(0,4)+'-'+s.substr(4,2)+'-'+s.substr(6,2);
}
return _.map(doc.rows, function(row) {
var rowdoc = {};
for (var i = 0; i < doc.dimensionHeaders.length; i++) {
var name = doc.dimensionHeaders[i].name;
rowdoc[name] = name == 'date' ? formatDate(row.dimensionValues[i].value) : row.dimensionValues[i].value;
}
for (var i = 0; i < doc.metricHeaders.length; i++) {
var metric_name = doc.metricHeaders[i].name;
rowdoc[metric_name] = row.metricValues[i].value;
}
return rowdoc;
});
To find the Property ID that is required in the URL go to https://analytics.google.com/ → Admin section, select Property → Property Details and there should be PROPERTY ID displayed