I’m working on a Google Apps Script that pulls data from Google Analytics and puts it into a spreadsheet. The script keeps throwing an error saying the user doesn’t have enough permissions for the profile.
I’ve checked everything I can think of. I have admin access to both the Analytics property and the Google Sheet. I’m logged into the correct Google account. But the error keeps happening.
Here’s my current script:
function executeAnalytics() {
try {
var data = fetchAnalyticsReport();
writeToSheet(data);
} catch(err) {
Browser.msgBox(err.message);
}
}
function fetchAnalyticsReport() {
var viewId = 'xxxxxxxx';
var gaTable = 'ga:' + viewId;
var fromDate = getPreviousDays(7);
var toDate = getPreviousDays(0);
var params = {
'dimensions': 'ga:source',
'sort': '-ga:users,ga:source',
'segment': 'dynamic::ga:deviceCategory==mobile',
'filters': 'ga:medium==organic',
'start-index': '1',
'max-results': '100'
};
var response = Analytics.Data.Ga.get(
gaTable,
fromDate,
toDate,
'ga:users,ga:sessions',
params);
if (response.getRows()) {
return response;
} else {
throw new Error('No data found for this view');
}
}
What could be causing this permission issue?
hey, also check the permissions of the google account u used in the script. sometimes, even with admin access, there can be issues if u’ve recently changed access levels. it may also help to review the oauth consent screen settings in the google cloud console.
Had this exact headache a few months ago. You need to enable the Google Analytics API in Google Cloud Console for your Apps Script project. Having admin access to Analytics isn’t enough - the script needs separate API access. Go to console.cloud.google.com, find your Apps Script project, hit APIs & Services > Library, and enable Google Analytics Reporting API. Also check your view ID format - I screwed this up initially. Use just the numbers without ‘ga:’ prefix when assigning to viewId variable, since you’re adding that prefix later in the gaTable line.
It appears that the issue might stem from insufficient authorization scopes for your script. Initially, when you authorized the script, it may not have requested the appropriate permissions for accessing Google Analytics data. To resolve this, navigate to your Google Account settings, then go to Security and find ‘Third-party apps with account access.’ From there, revoke the script’s access. After doing so, re-run the script, which should trigger a new OAuth authorization where you can grant the necessary Analytics permissions. This step helped me overcome a similar issue when integrating Analytics into my project.