I have created a custom formula for one of my spreadsheets. It gets two Date parameters and returns the Total Unique Event count of a specific Google Analytics event. When I test my code, it works perfectly in the Google Apps Script. But when I try to use the same formula in the associated Spreadsheet, it causes the following error:
GoogleJsonResponseException: API call to analytics.data.ga.get failed with error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (line 13).
Here is my code:
var tableId = 'ga:78467590';
function test(){
USE_CLICK_COUNT("2020-08-31", "2020-09-06");
}
function USE_CLICK_COUNT(startDate, endDate){
var optArgs = {
'dimensions':'ga:eventLabel',
'filters':'ga:eventLabel=~^UseClick'
};
var results = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:uniqueEvents', optArgs);
Logger.log(results.getTotalsForAllResults());
return results.getTotalsForAllResults();
}
When I run the test() function, it returns and logs {ga:uniqueEvents=13} - this is the number I'm looking for.
I have checked APIs and configuration details over and over again and can't figure out what I'm missing. Please see the attachments.





I thought that your situation is the same with this thread. Unfortunately, in the current stage, Google APIs of Advanced Google services cannot be directly used with the custom function. Because the access token cannot be retrieved at the custom function. It seems that this is the current specification. So as the workaround for achieving your goal, it uses Web Apps created by Google Apps Script.
But I thought that the script of your question is a bit different from this thread. So when a sample script is proposed, it might be useful for you. So I would like to introduce the sample script for achieving your goal.
Please copy and paste the following script to the script editor and save it.
Sample script:// This is your script.
var tableId = 'ga:78467590';
function test(){
USE_CLICK_COUNT("2020-08-31", "2020-09-06");
}
function USE_CLICK_COUNT(startDate, endDate){
var optArgs = {
'dimensions':'ga:eventLabel',
'filters':'ga:eventLabel=~^UseClick'
};
var results = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:uniqueEvents', optArgs);
Logger.log(results.getTotalsForAllResults());
return results.getTotalsForAllResults();
}
// I added below script.
const key = "samplekey"; // This is used as the key for running the Web Apps.
// Web Apps using as the wrapper.
function doGet(e) {
if (e.parameter.key === key) {
const res = USE_CLICK_COUNT(e.parameter.startDate, e.parameter.endDate);
return ContentService.createTextOutput(JSON.stringify(res));
}
return ContentService.createTextOutput("Error.");
}
// This is used as the custom function.
function RUN_USE_CLICK_COUNT(startDate, endDate) {
const url = `https://script.google.com/macros/s/###/exec?startDate=${startDate}&endDate=${endDate}&key=${key}`; // Please set the URL of Web Apps after you set the Web Apps.
return UrlFetchApp.fetch(url).getContentText();
}
On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
Select "Me" for "Execute the app as:".
Select "Anyone, even anonymous" for "Who has access to the app:".
Click "Deploy" button as new "Project version".
Automatically open a dialog box of "Authorization required".
Click "OK".
Copy the URL of Web Apps. It's like https://script.google.com/macros/s/###/exec.
Please set the URL of https://script.google.com/macros/s/###/exec to url of above script. And please redeploy Web Apps. By this, the latest script is reflected to the Web Apps. So please be careful this.
Please put =RUN_USE_CLICK_COUNT("2020-08-31", "2020-09-06") to a cell. By this, the values of "2020-08-31", "2020-09-06" are sent to the Web Apps and the values from USE_CLICK_COUNT are returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With