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:
![Screenshot 2023-12-28 at 15.15.42.png](../__attachments/10943299618/Screenshot%202023-12-28%20at%2015.15.42.png?inst-v=e770ba47-494b-488e-94fe-8369e8f7cb21)
![Screenshot 2023-12-28 at 15.16.15.png](../__attachments/10943299618/Screenshot%202023-12-28%20at%2015.16.15.png?inst-v=e770ba47-494b-488e-94fe-8369e8f7cb21)
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
![Screenshot 2024-01-12 at 12.03.56.png](../__attachments/10943299618/Screenshot%202024-01-12%20at%2012.03.56.png?inst-v=e770ba47-494b-488e-94fe-8369e8f7cb21)